2012-10-11 109 views
0

我需要在兩個單獨的表上執行各種聯接/聯合,以將我的結果組合到一個表中。首先,我將分享我擁有的和我需要的,然後我將分享我目前的非工作解決方案。使用不同的WHERE子句連接兩個表的數據

我有什麼:

  • 每個表由執行的操作的列表,每一個操作都有一個opPatient(所有患者有多個操作),opClassification(1,2,3或4), opDateopPrice

  • A StartEnd查詢的日期。

我需要什麼:

產生單個表:

  • 的所有不同opPatients
  • 對於每一個病人的列表:
    • 總的所有操作opprice
    • 合計用的4分類的所有操作的(分級4僅在table2
    • 所有操作總數
    • 操作的總數的3
    • 操作的總數的2
    • 一個分類的分類
  • 上述所有WHERE opDate >= @StartDate AND opDate <= EndDate

當前解決方案的(顯然不工作)

  • 設置startend日期(這裏沒有錯誤)

    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 
    

所有幫助,提示和指針將不勝感激!

謝謝! 此致敬禮編碼器。

+0

嗨,只是一些額外的信息:對於操作,數據必須開始日期和結束日期之間的檢索,以及。非常感謝你花時間查看答案!我真的很感激^^ – user1736733

回答

0

根據您的要求,似乎這是你所需要的。

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 
+0

Hi Prasanna, 謝謝你的回答! – user1736733

+0

這是你需要的嗎? – Prasanna

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