如何计算一个或多个值并将其显示在列中。

时间:2016-11-24 22:37:36

标签: sql sql-server

我刚刚开始掌握SQL,但我试图找到答案,但无济于事。

我有一张看起来像这样的表:

 date         base     version      prod_id      place
 2016-01-02   1        1            22           home
 2016-01-02   1        1            22           home 
 2016-01-02   1        1            1            home
 2016-01-02   1        1            1            store
 2016-01-02   1        1            22           store
 2016-01-02   1        1            2            store
 2016-01-02   1        1            2            web
 2016-01-02   1        1            24           web
 2016-01-02   1        1            1            web
 2016-01-02   1        2            24           home
 2016-01-02   1        2            22           home
 2016-01-02   1        2            22           store
 2016-01-02   1        2            1            store
 2016-01-02   1        2            2            web
 2016-01-03   1        1            22           home
 2016-01-03   1        1            22           home
 2016-01-03   1        1            1            home
 2016-01-03   1        1            24           store
 2016-01-03   1        2            24           store
 2016-01-03   1        2            22           web
 2016-01-03   1        2            1            web
 2016-01-03   1        2            2            web
 2016-01-03   1        2            1            web 

我试图做一个给我这个

的查询
 date        base      version      place      1,2       22,24   Total
 2016-01-02  1         1            home       1         2       3
 2016-01-02  1         1            store      2         1       3
 2016-01-02  1         1            web        2         1       3
 2016-01-02  1         2            home       2         0       2
 2016-01-02  1         2            store      1         1       2
 2016-01-02  1         2            web        1         0       1
 2016-01-03  1         1            home       2         1       3
 2016-01-03  1         1            store      0         1       1
 2016-01-03  1         2            store      0         1       1
 2016-01-03  1         2            web        3         0       3

所以用文字说:我试图对prod_id中每个值的出现进行分组和计数,并将它们放在一列中。并且它不仅需要计算和组合在一起的一个值,有时两个或更多。所以在这个例子中,我已经在特定的日期,版本和地点上添加了1和2的所有出现,所以22和24也是如此。我有意义吗?

3 个答案:

答案 0 :(得分:1)

 Select [Date]
       ,Base 
       ,[version]
       ,place
       ,SUM(Case when prod_id IN (1,2)   THEN 1 ELSE 0 END) AS [1,2]
       ,SUM(Case when prod_id IN (22,24) THEN 1 ELSE 0 END) AS [22,24]
       ,Count(*) AS Total
 From TableName
 GROUP BY [Date] ,Base ,[version],place

答案 1 :(得分:1)

您可以通过将sum aggregate函数与case表达式相结合来根据prod_id具有的值来计算。此技术有时称为条件聚合

试试这个:

select 
 date, base, version, place, 
 sum(case when prod_id in (1,2) then 1 else 0 end) as "1,2", 
 sum(case when prod_id in (22,24) then 1 else 0 end) as "22,24",
 count(prod_id) as Total
from your_table
group by date, base, version, place 
order by date, base, version;

答案 2 :(得分:1)

好的问题。你可以算一个复杂的CASE。 查询

 SELECT 
   prod_id,
   CASE WHEN prod_id IN (1,2)   THEN 1 ELSE 0 AS v1Or2,
   CASE WHEN prod_id IN (22,24) THEN 1 ELSE 0 AS v22or24
  FROM table1

这为您提供了一些有用的东西

prod_id      v1or2       v22or24
22           0           1
22           0           1
1            1           0
1            1           0
22           0           1
2            1           0
2            1           0

使用适当的GROUP BY,你有......

 SELECT date,place,base,version,
   SUM(CASE WHEN prod_id IN (1,2)   THEN 1 ELSE 0) AS v1Or2,
   SUM(CASE WHEN prod_id IN (22,24) THEN 1 ELSE 0) AS v22or24
  FROM table1
 GROUP BY date,place,base,version