Question

I have a query that will takes a series of drug transactions, and prices each transaction based on a drugPrices table:

    SELECT      
        fi.facilityIndividualId,
        fi.lastName + ',' + fi.firstName as 'Patient Name',
        ddh.drugName,
        CAST((ddh.dosesGiven * a.drugPrice) as decimal(9,2)) as 'Price' 
    FROM
        drugDistributionHistory ddh 
        INNER JOIN facilityIndividuals fi on ddh.facilityIndividualId = fi.facilityIndividualId 
        INNER JOIN drugPrices ON ddh.drugName like '%' + a.drugName + '%'
    ORDER BY
        fi.lastName + ', ' + fi.firstName,
        ddh.drugName    

My results look something like this:

39491   Martinez,Spring Celexa      0.06
39491   Martinez,Spring Seroquel    0.16
16884   McDannell,Chris Glucophage  0.03
69275   Riemer,Joseph   Folic Acid  0.01
69275   Riemer,Joseph   Paxil       0.07
17070   Savage,William  Asprin EC   0.01
17070   Savage,William  Lopressor   0.02

What I'd like to be added to my query is a totals column for each row, grouped on the facilityIndividualId , so my results look like:

39491   Martinez,Spring Celexa      0.06   0.22
39491   Martinez,Spring Seroquel    0.16   0.22
16884   McDannell,Chris Glucophage  0.03   0.03
69275   Riemer,Joseph   Folic Acid  0.01   0.08
69275   Riemer,Joseph   Paxil       0.07   0.08
17070   Savage,William  Asprin EC   0.01   0.03
17070   Savage,William  Lopressor   0.02   0.03

How might this be accomplished?

Was it helpful?

Solution

SQL Server you can use an aggregate function (SUM, COUNT, etc...) followed by OVER and PARTITION BY to achieve inline results

    SELECT      
        fi.facilityIndividualId,
        fi.lastName + ',' + fi.firstName as 'Patient Name',
        ddh.drugName,
        CAST((ddh.dosesGiven * a.drugPrice) as decimal(9,2)) as 'Price',
        SUM(CAST((ddh.dosesGiven * a.drugPrice) as decimal(9,2))) OVER (PARTITION BY fi.facilityIndividualId) as 'Total'
    FROM
        drugDistributionHistory ddh 
        INNER JOIN facilityIndividuals fi on ddh.facilityIndividualId = fi.facilityIndividualId 
        INNER JOIN drugPrices ON ddh.drugName like '%' + a.drugName + '%'
    ORDER BY
        fi.lastName + ', ' + fi.firstName,
        ddh.drugName 

OTHER TIPS

So, what I would do is use a subquery or CTE to aggregate the unit prices for each customer ID and then join it on the customer ID to the rest of it.

CREATE TABLE #testcase(
p_id int,
a_description varchar(20),
unit_price float
)

INSERT INTO #testcase
VALUES (1, 'drug 1', .39)

INSERT INTO #testcase
VALUES (1, 'drug 2', .13)

INSERT INTO #testcase
VALUES (2, 'drug 44', 5.59)

INSERT INTO #testcase
VALUES (2, 'drug x', 1.39)

INSERT INTO #testcase
VALUES (3, 'drug x', 1.39)

INSERT INTO #testcase
VALUES (3, 'drug x12', 11.39)

INSERT INTO #testcase
VALUES (3, 'drug zz', 12.09)



SELECT 
t.p_id
,t.a_description
,t.unit_price
,x.unit_price
FROM #testcase t
JOIN (
    SELECT 
        p_id
        ,SUM(unit_price) AS [unit_price]
    FROM #testcase t 
    GROUP BY p_id
        ) AS x ON x.p_id = t.p_id

Aducci's answer is way better, using OVER PARTITION BY is a much neater way of doing it.

But since I bothered to do it I figured I'd just post mine anyway.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top