[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How can I group these transactions?

Posted on 2016-10-24
7
Medium Priority
?
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1000 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 49

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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 41

Assisted Solution

by:Sharath
Sharath earned 1000 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 49

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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 …
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 video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

650 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