多个select语句的问题

时间:2015-11-17 09:14:15

标签: sql sql-server-2012

我在查询中使用多个select语句和case来获取结果。请查看我的查询,我正在尝试解决它。但它给我一个聚合函数的错误。我想要做的是,我有3个预算标准,结果应该是根据那个。他们是:

Budget 2015
Budget Jan-july2015
Budget July-dec2015

在第一个字段中我获得了整个2015年的预算,第二个字段我希望从july-dec2015获得预算费用作为当前预算'第三,jan-july2015july-dec2015的预算应添加为累计费用'。

SELECT CASE WHEN T1.[AcctName] LIKE '%Salaries%' THEN 'Salaries'
            WHEN T1.[AcctName] LIKE '%Travel%' THEN 'Travel'
            WHEN T1.[AcctName] LIKE '%Supplies%' THEN 'Supplies'
            WHEN T1.[AcctName] LIKE '%Consultants%' THEN 'Consultants'
            WHEN T1.[AcctName] LIKE '%Patient%' THEN 'Patient Care'
            WHEN T1.[AcctName] LIKE '%Equipment%' THEN 'Equipments' 
            WHEN T1.[AcctName] LIKE '%Expense%' THEN 'Other Expenses' 
            WHEN T1.[AcctName] LIKE '%Alteration%' THEN 'Alterations/Renovations' ELSE 'Alterations/Renovations' END,
    SUM(T0.[DebLTotal]/85) AS buget , (Select sum(T0.[CrdRLTotal]/85) where T2.[Name] = 'July-Dec2015' ) as 'CurrentBudget',
    (select sum(T0.[CrdRLTotal]/85) + 'CurrentBudget' where T2.[Name] = 'jan-july2015') as 'Cummulative' 
FROM OBGT 
T0 INNER JOIN OACT T1 ON T0.[AcctCode] = T1.[AcctCode] INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId] where T2.[Name] = 'Main Budget 2015'
GROUP BY CASE WHEN T1.[AcctName] LIKE '%Salaries%' THEN 'Salaries'
              WHEN T1.[AcctName] LIKE '%Travel%' THEN 'Travel'
              WHEN T1.[AcctName] LIKE '%Supplies%' THEN 'Supplies'
              WHEN T1.[AcctName] LIKE '%Consultants%' THEN 'Consultants' 
              WHEN T1.[AcctName] LIKE '%Patient%' THEN 'Patient Care' 
              WHEN T1.[AcctName] LIKE '%Equipment%' THEN 'Equipments'
              WHEN T1.[AcctName] LIKE '%Expense%' THEN 'Other Expenses'
              WHEN T1.[AcctName] LIKE '%Alteration%' THEN 'Alterations/Renovations' ELSE 'Alterations/Renovations' END

我不知道如何获得所需的所有字段。

1 个答案:

答案 0 :(得分:1)

这是 NOT 的答案,只是为了说明如何使用t1的派生表以更短的形式编写代码。

SELECT T1.[AcctName],
       SUM(T0.[DebLTotal]/85) AS buget,
       (Select sum(T0.[CrdRLTotal]/85) where T2.[Name] = 'July-Dec2015' ) as 'CurrentBudget',
       (select sum(T0.[CrdRLTotal]/85) + 'CurrentBudget' where T2.[Name] = 'jan-july2015') as 'Cummulative' 
FROM OBGT T0
INNER JOIN
    (
    SELECT CASE WHEN T1.[AcctName] LIKE '%Salaries%' THEN 'Salaries'
                WHEN T1.[AcctName] LIKE '%Travel%' THEN 'Travel'
                WHEN T1.[AcctName] LIKE '%Supplies%' THEN 'Supplies'
                WHEN T1.[AcctName] LIKE '%Consultants%' THEN 'Consultants'
                WHEN T1.[AcctName] LIKE '%Patient%' THEN 'Patient Care'
                WHEN T1.[AcctName] LIKE '%Equipment%' THEN 'Equipments' 
                WHEN T1.[AcctName] LIKE '%Expense%' THEN 'Other Expenses' 
                WHEN T1.[AcctName] LIKE '%Alteration%' THEN 'Alterations/Renovations'
                ELSE 'Alterations/Renovations'
           END as [AcctName],
           [AcctCode]
    from OACT
    ) as T1 ON T0.[AcctCode] = T1.[AcctCode]
INNER JOIN OBGS T2 ON T0.[Instance] = T2.[AbsId]
where T2.[Name] = 'Main Budget 2015'
GROUP BY T1.[AcctName]