SQL Beginner:来自同一个表的多个where子句

时间:2015-08-27 13:28:14

标签: sql sql-server case-when

我对SQL很新,不知道如何完成以下操作。

我有一个仪表ID列表,这些仪表ID具有多年的Target值和分配的预算数据年数,但是它们都保存在同一个表中,并带有标识符(TargetType)以区分Target(0)和&预算(1)

ID   TARGETTYPE   VALUE01   VALUE02 ...(etc up to VALUE12)
123  0            1001      1100
123  1            9000      9100
456  0            5000      5100
456  1            8000      8100      

期望的结果还包括来自其他几个表的信息,以及

中添加的信息

到目前为止,我的查询可以带来一组数据:

PARAMETERS 
[Utility] Text;

SELECT 
Contacts.Group,
Contacts.Site,
Points.ID,
Points.Utility,
Points.MPAN1,
Target.Value_01 AS [Target JAN],
Target.Value_02 AS [Target FEB],
Target.Value_03 AS [Target MAR],
Target.Value_04 AS [Target APR],
Target.Value_05 AS [Target MAY],
Target.Value_06 AS [Target JUN],
Target.Value_07 AS [Target JUL],
Target.Value_08 AS [Target AUG],
Target.Value_09 AS [Target SEP],
Target.Value_10 AS [Target OCT],
Target.Value_11 AS [Target NOV],
Target.Value_12 AS [Target DEC]

FROM 
((Contacts INNER JOIN Points ON Contacts.[Id] = Points.[Contacts_Id]) 
                  INNER JOIN Contracts ON Points.[Id] = Contracts.[Point_Id]) 
                  INNER JOIN Target ON Points.Id = Target.DataSetId

WHERE 
Points.UtilityType =[Utility]

ORDER BY 
Contacts.ClientGroup;

期望的输出
(这些值会将TargetJan传递给TargetDec,然后是BudgetJan直到BudgetDec,但为了简洁起见我没有显示):

Group Site     ID   Utility MPAN1    TargetJan TargetFeb etc... BudgetJan  BudgetFeb etc...      
ABC   London   123  Gas     123456    1,000     1,100           9,000      9,100
ABC   NewYork  456  Gas     ABC123    5,000     5,100           8,000      8,100 

如何添加相同的字段,但根据Target.TargetType的值,我猜测它是一个Union查询,但我不知道。

任何指示从哪里开始都会感激不尽:)

更新#1
谢谢您的帮助。我想我理解了这个问题,但是仍然有一些奇怪的行为我无法弄清楚。

查询已使用

SELECT 
Points.ID,

  SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_01 else 0 end) AS [TGT JAN],
  SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_02 else 0 end) AS [TGT FEB],

FROM 
((Contacts INNER JOIN Points ON Contacts.[Id] = Points.[Contacts_Id]) 
              INNER JOIN Contracts ON Points.[Id] = Contracts.[Point_Id] )
              INNER JOIN Target ON Points.Id = Target.DataSetId

GROUP BY
Points.ID

ORDER BY
Points.ID;


我的数据行只有一个Target.Type,然后查询根据需要返回:
原始数据

ID     TARGETTYPE   VALUE_01   VALUE_02 
10079  0            7642      5735

RESULT

ID    TGTJAN   TGTFEB
10079 7642    5735

但是......如果我有一个包含2个或更多TargetTypes的ID,那么Value输出将乘以6
原始数据

ID     TARGETTYPE   VALUE_01   VALUE_02 
7423   0            58339     57441
7423   1            1663      1637

RESULT

ID    TGTJAN   TGTFEB
7423  350034   344646

我已经尝试并将INNER JOIN移到了联系人表格中(虽然我需要加入),然后一切按预期工作????那么为什么联系人JOIN会导致这个问题呢?
请把我从悲惨中解脱出来,因​​为我无法弄清楚发生了什么!

2 个答案:

答案 0 :(得分:0)

使用选择案例语法怎么样?

select
...
sum(case when Target.TargetType = 'Target' then Target.Value_01 else 0 end) AS [Target JAN],
sum(case when Target.TargetType = 'Budget' then Target.Value_01 else 0 end) AS [Budget JAN],
...
group by
--all fields in the select list which are not aggregates

答案 1 :(得分:0)

为了将来参考,我已经通过重写连接和更改数据的保存方式解决了这个问题,因此我不需要引用Contracts表,这是可以减少的并且可能导致我奇怪的x乘6问题

SELECT 
Points.ID,

  SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_01 else 0 end) AS [TGT kWh JAN],
  SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_02 else 0 end) AS [TGT kWh FEB],

  SUM(CASE WHEN Target.TargetType = '1' THEN Target.Value_01 else 0 end) AS [BUD kWh JAN],
  SUM(CASE WHEN Target.TargetType = '1' THEN Target.Value_02 else 0 end) AS [BUD kWh FEB]

FROM (Contacts INNER JOIN Points ON Contacts.Id = Points.Contacts_Id)  
                       INNER JOIN Target ON Points.Id = Target.DataSetId

GROUP BY
Points.ID

ORDER BY
Points.ID;