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
Solved

How can I group these transactions?

Posted on 2016-10-24
7
47 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query with cast 38 53
This query failed in sql 2014 5 33
get_systemdrive info from tsql? 1 18
SQL Recursion 6 18
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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