我想从一个表组中按一列获取特定时间段内的记录数

时间:2017-11-02 10:52:29

标签: sql-server

这是我使用的查询:

select  
 (select COUNT(Id) from _Trans where cast(TDate as time) < '07:30:00.000' group by lid)  
,(select COUNT(Id) from _Trans where cast(TDate as time) > '07:30:00.000' and cast(TDate as time) < '09:30:00.000' group by lid)  
,(select COUNT(Id) from _Trans where cast(TDate as time) > '09:30:00.000' and cast(TDate as time) < '11:30:00.000' group by lid)  
,(select COUNT(Id) from _Trans where cast(TDate as time) > '11:30:00.000' and cast(TDate as time) < '13:30:00.000' group by lid)  
,(select COUNT(Id) from _Trans where cast(TDate as time) > '13:30:00.000' and cast(TDate as time) < '15:30:00.000' group by lid)  
,(select COUNT(Id) from _Trans where cast(TDate as time) > '15:30:00.000' and cast(TDate as time) < '17:30:00.000' group by lid)  
,(select COUNT(Id) from _Trans where cast(TDate as time) > '17:30:00.000' and cast(TDate as time) < '19:30:00.000' group by lid)  
,(select COUNT(Id) from _Trans where cast(TDate as time) > '19:30:00.000' group by lid)  

但我收到此错误: 子查询返回的值超过1。 ......时不允许这样做。

2 个答案:

答案 0 :(得分:1)

您是否厌倦了自己运行其中一个子查询?

select COUNT(Id) from _Trans where cast(TDate as time) < '07:30:00.000' group by lid

它告诉你这个问题究竟是什么:你不能把一行放在一个单元格中。

如果你想只看到一个盖子,那就行了 - 只需添加一个where子句:

`select COUNT(Id) from _Trans where cast(TDate as time) < '07:30:00.000' and lid =  5`

您希望此查询显示多行(如每个盖子可能有一行吗?)。如果是这样,你需要一种不同的方法......例如,某些方式:

select  
    coalesce(a.lid,b.lid) as lid
    "before 730"
    ,"before 930"
FROM
    (select 
        lid
        , COUNT(Id) as "before 730" 
    from 
        _Trans 
    where 
        cast(TDate as time) < '07:30:00.000' 
    group by 
        lid)  as a
full outer join 
    (select 
        lid
        , COUNT(Id) as "before 930"  
    from 
        _Trans 
    where cast(TDate as time) > '07:30:00.000' 
        and cast(TDate as time) < '09:30:00.000' 
    group by 
    lid) as b
on a.lid = b.lid

    **and so on for other metrics**

最好像这样把你所有的盖子放在前面..(但也请考虑Dheerendras方法,因为它可能是一种更安全的方式,认为输出格式不同。https://stackoverflow.com/a/47073831/359135

select  
    a.lid
    ,"before 730"
    ,"before 930"
    ,"before 1130"

FROM
    (select distinct 
        lid
    from 
        _Trans 
    )  as a
left outer join 
(select 
        lid
        , COUNT(Id) as "before 730" 
    from 
        _Trans 
    where 
        cast(TDate as time) < '07:30:00.000' 
    group by 
        lid)  as b
 on a.lid = b.lid
 left outer join
    (select 
        lid
        , COUNT(Id) as "before 930"  
    from 
        _Trans 
    where cast(TDate as time) > '07:30:00.000' 
        and cast(TDate as time) < '09:30:00.000' 
    group by 
    lid) as c
on a.lid = c.lid
left outer join
    (select 
        lid
        ,COUNT(Id) as "before 1130"
    from 
        _Trans 
    where 
        cast(TDate as time) > '09:30:00.000' and cast(TDate as time) < '11:30:00.000' 
    group by 
        lid) as d
on a.lid = d.lid

答案 1 :(得分:0)

您可以使用以下代码 ;WITH CTE AS ( SELECT ID,lid, CASE WHEN cast(TDate as time) < '07:30:00.000' THEN 'Block1' WHEN cast(TDate as time) > '07:30:00.000' and cast(TDate as time) < '09:30:00.000' THEN 'Block2' WHEN cast(TDate as time) > '09:30:00.000' and cast(TDate as time) < '11:30:00.000' THEN 'Block3' WHEN cast(TDate as time) > '11:30:00.000' and cast(TDate as time) < '13:30:00.000' THEN 'Block4' WHEN cast(TDate as time) > '13:30:00.000' and cast(TDate as time) < '15:30:00.000' THEN 'Block5' WHEN cast(TDate as time) > '15:30:00.000' and cast(TDate as time) < '17:30:00.000' THEN 'Block6' WHEN cast(TDate as time) > '17:30:00.000' and cast(TDate as time) < '19:30:00.000' THEN 'Block7' WHEN cast(TDate as time) > '19:30:00.000' THEN 'Block6'
END AS TimeFrame FROM _Trans ) SELECT COUNT(*) AS RowsCount ,TimeFrame ,lid FROM CTE GROUP BY TimeFrame,lid