Solved

How can I group these transactions?

Posted on 2016-10-24
7
43 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
ID: 41857668
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
ID: 41858445
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
ID: 41858461
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:brucegust
ID: 41858528
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
ID: 41859786
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
ID: 41859824
The left join is overriden by the where clause
0
 

Author Comment

by:brucegust
ID: 41862395
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

813 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

19 Experts available now in Live!

Get 1:1 Help Now