Solved

How can I group these transactions?

Posted on 2016-10-24
7
38 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

15 Experts available now in Live!

Get 1:1 Help Now