0

我需要在两个单独的表上执行各种联接/联合,以将我的结果合并到一个表中。首先,我将分享我所拥有的和我需要的,然后我将分享我当前的非工作解决方案。

我有的:

  • 每个表都包含一个执行的操作列表,每个操作都有一个opPatient(所有患者都进行过多次手术),opClassification(1、2、3 或 4)opDate,,,opPrice

  • AStartEnd查询日期。

我需要的:

生成一个带有以下内容的表:

  • 所有不同的列表opPatients
  • 对于每位患者:
    • opprice所有操作的总和
    • 分类为 4 的所有操作的总和opPrice(分类 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
    

所有帮助、提示和指示将不胜感激!

谢谢!!此致, Nooby 编码器。

4

2 回答 2

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
于 2012-10-11T05:23:55.477 回答
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
于 2012-10-11T23:17:55.473 回答