Solved

How Can I Add a Row to this SELECT?

Posted on 2016-11-01
2
31 Views
Last Modified: 2016-11-02
Here's the SELECT...

FYI: This is a mess, but I figured it would be wise to start with the whole thing and then break it down to that place where I want to make a change.

SELECT
LTRIM( case when type = 'a' then 'Adjustment' when type = 'p' then 'Payment' when type = 'c' then 'Charge' end ) as txntype, 
[date], 
patientname, 
description, 
amount, 
physicianid, 
practclaimid, 
priority, 
cpt, case when practclaimid = '' then 1 else 0 end as toppriority, 
balance, 
firstdos, 
total_charge 
FROM 
	( 
	SELECT 
	paymentmethod, 
	payerdesc, 
	type, 
	[date] = case when type = 'c' then dos else posted end, 
	pfirst+' '+plast as patientname, 
	LTRIM( case when type = 'a' then payerdesc when type = 'p' then payerdesc when type = 'c' then ISNULL(cpt, '') +' - '+ ISNULL(left(cptdesc,100),'')	end ) as description, 
	amount,
	'Medical Oncology' as physicianid, 
	coalesce(cg.encountercode, '') as practclaimid, 
	case when type = 'c' then 0 else 1 end as priority, 
	left(cptdesc,100) as cptdesc, cpt as cpt, 
	(SELECT sum(amount) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid and type='C' GROUP by type) as total_charge, 
	(select sum(amount) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid) as balance, 
	(select min(dos) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid and type = 'c') as firstdos 
	 FROM 
	 txn t left join chargegroup cg on t.chargegroupid = cg.id 
	 WHERE
	 --type<>'C'
	 --AND
	 t.accountid = 9949388 and (cg.status1 not in ('rt','hd','rp','fc') or cg.status1 is null) 
	 AND cg.id in ( select chargegroupid from txn where accountid = 9949388 
	 GROUP BY chargegroupid having max(txn.created) > DATEADD(DAY, -45, GETDATE()) ) and reversedref is null 
	 ) u 
ORDER BY firstdos, practclaimid, priority,[date], amount 

Open in new window


The results look like the spreadsheet that I have attached.

When you look at the spreadsheet, you'll see how I've got "charges" highlighted in grey...

charges
Those same rows are "combined" in the second tab of the same spreadsheet that's entitled "new and improved."

Smell that?

That's the aroma of a different kind of SELECT!

Now, here' what I've got planned:

I want to take the original SELECT and get everything except those rows that have a "C" type. Like this:

SELECT
LTRIM( case when type = 'a' then 'Adjustment' when type = 'p' then 'Payment' when type = 'c' then 'Charge' end ) as txntype, 
[date], 
patientname, 
description, 
amount, 
physicianid, 
practclaimid, 
priority, 
cpt, case when practclaimid = '' then 1 else 0 end as toppriority, 
balance, 
firstdos, 
total_charge 
FROM 
	( 
	SELECT 
	paymentmethod, 
	payerdesc, 
	type, 
	[date] = case when type = 'c' then dos else posted end, 
	pfirst+' '+plast as patientname, 
	LTRIM( case when type = 'a' then payerdesc when type = 'p' then payerdesc when type = 'c' then ISNULL(cpt, '') +' - '+ ISNULL(left(cptdesc,100),'')	end ) as description, 
	amount,
	'Medical Oncology' as physicianid, 
	coalesce(cg.encountercode, '') as practclaimid, 
	case when type = 'c' then 0 else 1 end as priority, 
	left(cptdesc,100) as cptdesc, cpt as cpt, 
	(SELECT sum(amount) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid and type='C' GROUP by type) as total_charge, 
	(select sum(amount) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid) as balance, 
	(select min(dos) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid and type = 'c') as firstdos 
	 FROM 
	 txn t left join chargegroup cg on t.chargegroupid = cg.id 
	 WHERE
	 type<>'C'
	 AND
	 t.accountid = 9949388 and (cg.status1 not in ('rt','hd','rp','fc') or cg.status1 is null) 
	 AND cg.id in ( select chargegroupid from txn where accountid = 9949388 
	 GROUP BY chargegroupid having max(txn.created) > DATEADD(DAY, -45, GETDATE()) ) and reversedref is null 
	 ) u 
ORDER BY firstdos, practclaimid, priority,[date], amount 

Open in new window


I still have my "total_charge," even though I'm not listing any of the "C" transaction types. Not sure if that makes a difference, but I was glad to see that when I first started playing with this.

What I'm thinking would be good now would be to add a row for every for every pactclaimed so the end result is what I have on the tab on my spreadsheet entitled "new and improved."

What I'm envisioning (I think) is a UNION of two SELECT statements into one, glorious recordset.

How?
experts_exchange.xlsx
0
Comment
Question by:brucegust
2 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 41869134
I can't tell how you decided which charges to combine as "charge to patient" and which you left separate.

But you can reduce overhead and get all the two sums and the min in a single query:

...
FROM
      (
      SELECT
      paymentmethod,
      payerdesc,
      type,
      [date] = case when type = 'c' then dos else posted end,
      pfirst+' '+plast as patientname,
      LTRIM( case when type = 'a' then payerdesc when type = 'p' then payerdesc when type = 'c' then ISNULL(cpt, '') +' - '+ ISNULL(left(cptdesc,100),'')      end ) as description,
      amount,
      'Medical Oncology' as physicianid,
      coalesce(cg.encountercode, '') as practclaimid,
      case when type = 'c' then 0 else 1 end as priority,
    left(cptdesc,100) as cptdesc, cpt as cpt,
    txn_totals.total_charge, txn_totals.balance, txn_totals.firstdos
    FROM
    txn t inner join (
           SELECT chargegroupid, accountid,
               sum(case when type = 'C' then amount else 0 end) as total_charge,
               sum(amount) as balance,
               min(case when type = 'C' then dos end) as firstdos
           FROM txn
           GROUP BY chargegroupid, accountid
       ) as txn_totals on txn_totals.chargegroupid = t.chargegroupid and txn_totals.accountid = t.accountid
       left join chargegroup cg on t.chargegroupid = cg.id
      
       WHERE
       t.accountid = 9949388 and (cg.status1 not in ('rt','hd','rp','fc') or cg.status1 is null)
       AND cg.id in ( select chargegroupid from txn where accountid = 9949388
       GROUP BY chargegroupid having max(txn.created) > DATEADD(DAY, -45, GETDATE()) )
       AND reversedref is null
       ) u
ORDER BY firstdos, practclaimid, priority,[date], amount
0
 

Author Closing Comment

by:brucegust
ID: 41870349
Thanks, Scott!

I've got another question - would love your expertise on it!
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now