Solved

How can I group these transactions?

Posted on 2016-10-24
7
34 Views
Last Modified: 2016-10-27
Here's my SELECT:

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) 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 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, -365, GETDATE()) ) 
AND 
reversedref is null

Open in new window


The results are attached.

What I need is included in the attached spreadsheet. You'll see it represented beneath the "Here's what I need" row. Bottom line: I need to group the "C" transaction type as a SUM of whatever it is that is categorized under the same "practclaimid." Ultimately, I need to display these row with the charges (sum of the charges) displayed at the top and I'm thinking I can do that by ordering by "amount," give the fact that a "charge" is always going to be a positive value.

How would I do this?
0
Comment
Question by:brucegust
  • 3
  • 3
7 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
Comment Utility
weird-grouping.png
You want to ignore 82784 - Gammaglobulin Iga Igd Ig G Igm Each
"for some reason"

but is it only "82784 - Gammaglobulin Iga Igd Ig G Igm Each" you need to ignore?
to meet "that reason"

i.e. what is the logic of choosing just one value like "80053 - Comprehensive Metabolic Panel"?

More information would help

=========
by the way

FROM txn t
LEFT JOIN chargegroup cg ON t.chargegroupid = cg.id
WHERE t.accountid = 9949388

You have created the conditions of an inner join through that where clause.
So use an INNER JOIN because that LEFT JOIN is not effective.
0
 

Author Comment

by:brucegust
Comment Utility
Hey, Paul!

The reason for eliminating "82784 - Gammaglobulin Iga Igd Ig G Igm" is that, for the sake of how this will all appear on the user's statement, all of the charges need to be represented as one value - the total amount charged in the context of the practclaimid (MCE467391).

I don't want to ignore it, I just want to represent it as well as every other charge as one value - the sum total of all the charges.

Holler at me if that still doesn't make sense.

Thanks!
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
Not makng sense to me.

Imagine you are sql and not human for a while. You can only do what you are told to do with the data you encounter. Logic is of paramount importance to you when you think like sql. Eg
Why do I  add up all the positive numbers but not include the negative numbers in the addition?. Why are negatives treated differently?

Sql needs something mechanistic. The logic has to be repeatable.

Note I can only see the data you have revealed on this page. Could there be some other code in the rables that unlocks the logic needed?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:brucegust
Comment Utility
The reason you're treating negative values differently is because they're either payments or adjustments. I need a total of the charges. Like this:

(SELECT sum(amount) from txn where t.chargegroupid = txn.chargegroupid and t.accountid = txn.accountid and type='C') as total_charge

(I just figured that piece out).

The problem - and where I'm needing your ninja-ness - is how do I group the "C" charges in a way where they show up as one row?

I've attached another spreadsheet that provides a picture of where I've got, what I need and where I'm at.

Also, it may be that I don't need to group the charges together. Maybe I create an entirely different row that's the sum total of the charges. That could work as well. How?

Thanks!
experts_exchange.xlsx
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
Comment Utility
give a try.
;WITH CTE AS (
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) 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 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, -365, GETDATE()) ) 
AND 
reversedref is null),
CTE2 AS (
SELECT *,
       row_number() over (partition by practclaimid, [type] order by (select 1)) rn,
       sum(amount) over (partition by practclaimid, [type]) sum_amount
  FROM CTE)
SELECT paymentmethod, 
    payerdesc, 
    [type], 
    [date],
    patientname,
    description,
    case [type] when 'C' then sum_amount else amount end amount,
    physicianid,
    practclaimid,
    priority,
    cptdesc,
    cpt,
    balance,
    firstdos
  FROM CTE2
 WHERE ([type] = 'C' and rn = 1) 
   or [type] <> 'C' 
   or [type] is null

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
The left join is overriden by the where clause
0
 

Author Comment

by:brucegust
Comment Utility
Guys! Here's what I did: I took a hybrid of what you suggested, but a much simpler version and then did my "grouping" at the PHP level. Bottom line: Success!

Thank you!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

10 Experts available now in Live!

Get 1:1 Help Now