SQL Server 2014 - 在分组后返回具有非零值的行

时间:2015-07-07 17:41:37

标签: sql group-by sql-server-2014

数据如下所示:

eqId    date_   close_       high        low          open_   volume    bid          ask       totRtn      _rowId
4   2008-01-02  20.660004   21.100098   20.660004   21.100006   3650    20.649994   21.600006   113.229729  1827
4   2008-01-03  22.979996   22.990005   20.710007   21.100006   13200   21.779999   22.990005   125.944735  1828
4   2008-01-04  22.960007   22.979996   22.000000   22.729996   5600    22.100006   22.960007   125.835183  1829
4   2008-01-07  22.509995   22.949997   22.500000   22.699997   19800   22.520004   22.839996   123.368836  1830
4   2008-01-08  22.389999   22.850006   22.389999   22.500000   8500    22.270004   22.759995   122.711183  1831
4   2008-01-09  22.369995   22.949997   22.369995   22.389999   2100    22.300003   22.500000   122.601548  1832
4   2008-01-10  22.550003   22.619995   22.369995   22.369995   3000    22.399994   22.610001   123.588105  1833
4   2008-01-11  22.710007   22.729996   22.550003   22.729996   1400    22.559998   22.750000   124.465027  1834
4   2008-01-14  22.539993   22.750000   22.470001   22.479996   5700    22.529999   22.699997   123.533244  1835
4   2008-01-15  23.028000   23.028000   22.589996   22.750000   9249    22.750000   23.080002   126.207827  1836
4   2008-01-16  22.919998   23.100006   22.660004   23.000000   10600   22.669998   23.080002   125.615908  1837
4   2008-01-17  22.660004   23.149994   22.660004   23.149994   7325    22.660004   22.919998   124.190979  1838
4   2008-01-18  22.770004   23.089996   22.669998   22.949997   9200    22.770004   22.919998   124.793848  1839
4   2008-01-22  22.279999   22.809998   22.250000   22.509995   8900    22.300003   22.669998   122.108314  1840
4   2008-01-23  22.279999   22.889999   22.270004   22.500000   3900    22.300003   22.759995   122.108314  1841
4   2008-01-24  22.669998   22.910004   22.519897   22.520004   11800   22.550003   22.949997   124.245752  1842
4   2008-01-25  22.770004   22.880005   22.570007   22.690002   6700    22.619995   22.820007   124.793848  1843

我要做的是计算给定eqid在定义的日期范围内(使用where语句)的日期总数,该数量为零。因此,如果我提供1年作为日期范围并且有4天股票根本没有交易我想要显示eqid和zero_count。

我试过的脚本是:

SELECT eqId
      ,date_
      ,volume
      ,count(nullif(volume,0)) as hasvol
  FROM [CRC].[dbo].[eqPrice]
  where date_>'2007-12-31' and date_ < '2012-01-01'
  group by eqid,date_,volume
  order by eqid

但我得到了这个结果:

eqId    date_   volume  hasvol
4   2008-01-02 00:00:00 3650    1
4   2008-01-03 00:00:00 13200   1
4   2008-01-04 00:00:00 5600    1
4   2008-01-07 00:00:00 19800   1
4   2008-01-08 00:00:00 8500    1
4   2008-01-09 00:00:00 2100    1
4   2008-01-10 00:00:00 3000    1
4   2008-01-11 00:00:00 1400    1
4   2008-01-14 00:00:00 5700    1
4   2008-01-15 00:00:00 9249    1
4   2008-01-16 00:00:00 10600   1
4   2008-01-17 00:00:00 7325    1
4   2008-01-18 00:00:00 9200    1
4   2008-01-22 00:00:00 8900    1

我似乎无法弄清楚获取总天数的语法,只计算每天的计数(1)。

非常感谢任何帮助

编辑:移动OP的回复

我跑了这个:

SELECT top 10 eqId,
       date_,
       CASE WHEN SUM(COALESCE(volume, 0)) = 0 THEN 0 ELSE 1 END AS hasvol
  FROM [CRC].[dbo].[eqPrice]
  WHERE date_ > '2007-12-31'
  AND date_ < '2012-01-01'
  GROUP BY eqid, date_
  ORDER BY eqid, date_

得到了这个:

eqId    date_          hasvol
4    2008-01-02 00:00:00    1
4    2008-01-03 00:00:00    1
4    2008-01-04 00:00:00    1
4    2008-01-07 00:00:00    1
4    2008-01-08 00:00:00    1
4    2008-01-09 00:00:00    1
4    2008-01-10 00:00:00    1
4    2008-01-11 00:00:00    1
4    2008-01-14 00:00:00    1
4    2008-01-15 00:00:00    1

所以我仍然没有在整个日期范围内为每个eqid获得一个数字: - (

编辑2

我认为我很乐意去,但是,缺少的是任何整个日期范围都没有零天的情况。当范围内每天报告的非零音量时,我需要将DayCntWhereVolumeIsZero显示为0。抱歉来回。 。再次感谢

2 个答案:

答案 0 :(得分:1)

以下假设SELECT eqId, date_, volume, CASE WHEN COALESCE(volume, 0) = 0 THEN 0 ELSE 1 END AS hasvol FROM [CRC].[dbo].[eqPrice] WHERE date_ > '2007-12-31' AND date_ < '2012-01-01' ORDER BY eqid, date_ eqId组合是唯一的,正如您在评论中所述。

date_

如果您可以包含GROUP BYSUM的重复条目,那么这可能是一个可以获得您想要的内容的查询(使用SELECT eqId, date_, CASE WHEN SUM(COALESCE(volume, 0)) = 0 THEN 0 ELSE 1 END AS hasvol FROM [CRC].[dbo].[eqPrice] WHERE date_ > '2007-12-31' AND date_ < '2012-01-01' GROUP BY eqid, date_ ORDER BY eqid, date_ eqId):

volume

修改

重新阅读您的帖子后,您觉得每个0只需要一行,而对于每一行,您需要SELECT eqId, COUNT(CASE WHEN COALESCE(volume, 0) = 0 THEN 'X' END) AS DayCntWhereVolumeIsZero FROM [CRC].[dbo].[eqPrice] WHERE date_ > '2007-12-31' AND date_ < '2012-01-01' GROUP BY eqid ORDER BY eqid 的值为{{1 }}

如果这是你想要的,那么这就是查询:

(编辑以确保即使没有任何日期的音量为零,您仍然会得到一个计数为零的行)

{{1}}

答案 1 :(得分:0)

您正在删除计数前的NULL。记住count(<colname>)计算非NULL值的数量。只是做:

  count(volume) as hasvol

 coalesce(sum(volume), 0) as hasvol