0

I need to perform a variety of Joins / Unions on two separate tables to combine my results into a single table. First I will share what I have and what I need, then I'll share my current non-working solution.

What I have:

  • Each table consists of a list of operations performed, each operation has a opPatient (all patients have had multiple operations), opClassification (1, 2, 3 or 4), opDate, opPrice.

  • A Start and End date of the query.

What I need:

To produce a single table with:

  • A list of all distinct opPatients
  • For each patient:
    • Total opprice of all operations
    • Total opPrice of all operations with classification of 4 (Classification 4 is only in table2)
    • Total number of all operations
    • Total number of operations with a classification of 3
    • Total number of operations with a classification of 2
  • All of the above WHERE opDate >= @StartDate AND opDate <= EndDate

Current solution (not working obviously)

  • Sets the start and end dates (no errors here)

    SET @StartDate = DATEADD(DAY, -(DATEPART(Day, @Date)-1), @Date);    
    SET @EndDate = DATEADD(DAY, -1, (DATEADD(MONTH, 1, @StartDate)));
    
    SELECT
       Operation = (SELECT DISTINCT operation FROM table1
                    UNION
                    SELECT DISTINCT operation FROM table2),
       TotalPrice = CAST(opPrice) AS MONEY),
       TotalOps = COUNT(*),
       opC4 = COUNT(CASE
                WHEN opClassification = 4 THEN 1 
                ELSE Null                                  
                END) 
                WHERE opDate <= @EndDate AND opDate >= @StartDate 
                FROM table2,            
       opC3 = COUNT (CASE WHEN opClassification = 3 THEN 1 ELSE Null END),
       opC2 = COUNT (CASE WHEN opClassification = 2 THEN 1 ELSE Null END)
     FROM 
        table1, table2
     WHERE 
         table1.opDate <= @EndDate AND table1.opDate >= @StartDate 
     ORDER BY 
         Operation
    

All help, tips and pointers would be greatly appreciated!

Thanks!! Sincerely, A nooby coder.

1
  • Hi, Just some extra information: For operation, the data must be retrieved between StartDate and Enddate as well. Thank you all so much for taking your time to view + answer! I really appreciate it ^.^ Oct 11, 2012 at 4:22

2 Answers 2

0

According to your requirements, seems this is what you need.

SELECT opPatient
, SUM(CASE WHEN opClassification = 1 THEN 1 ELSE 0 END) opC1
, SUM(CASE WHEN opClassification = 2 THEN 1 ELSE 0 END) opC2
, SUM(CASE WHEN opClassification = 3 THEN 1 ELSE 0 END) opC3
, SUM(CASE WHEN opClassification = 4 THEN 1 ELSE 0 END) opC4
, SUM(opPrice) AS TotalPrice
FROM
(
    SELECT * FROM Table1
    UNION ALL
    SELECT * FROM Table2
) AS NewTable
WHERE opDate >= @StartDate AND opDate <= @EndDate 
GROUP BY opPatient
1
  • Hi Prasanna, Thank you for your answer! Oct 11, 2012 at 5:36
0
SET @StartDate = DATEADD(DAY, -(DATEPART(Day, @Date)-1), @Date);
SET @EndDate = DATEADD(DAY, -1, (DATEADD(MONTH, 1, @StartDate)));  

SELECT opPatient, 
    -- Totals
    COUNT(opPrice) AS OpsTotal, 
    SUM(opPrice) AS PriceTotal,
    SUM(CASE WHEN opDate >= @StartDate AND opDate <= EndDate THEN opPrice END) AS PriceThisMonth,
    -- Op 4
    SUM(CASE WHEN opClassification = 4 THEN 1 END) AS OpsOp4,
    SUM(CASE WHEN opClassification = 4 THEN opPrice END) AS PriceOp4,
    SUM(CASE WHEN opClassification = 4 AND opDate >= @StartDate AND opDate <= EndDate THEN opPrice END) AS Op4PriceThisMonth,
    -- Op 3
    SUM(CASE WHEN opClassification = 3 THEN 1 END) AS OpsOp4,
    SUM(CASE WHEN opClassification = 3 THEN opPrice END) AS PriceOp4,
    SUM(CASE WHEN opClassification = 3 AND opDate >= @StartDate AND opDate <= EndDate THEN opPrice END) AS Op4PriceThisMonth,
    -- Op 2
    SUM(CASE WHEN opClassification = 2 THEN 1 END) AS OpsOp4,
    SUM(CASE WHEN opClassification = 2 THEN opPrice END) AS PriceOp4,
    SUM(CASE WHEN opClassification = 2 AND opDate >= @StartDate AND opDate <= EndDate THEN opPrice END) AS Op4PriceThisMonth,

FROM (
    SELECT opPatient, opClassification, opDate, opPrice
    FROM Table1
    UNION ALL
    SELECT opPatient, opClassification, opDate, opPrice
    FROM Table1
)
GROUP BY opPatient

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.