TSQL:指定时间段内每天不同行数的总和

时间:2014-01-29 09:55:38

标签: sql sql-server-2008 subquery

我想让每天的客户数量, POS机的账单号码相同。

TSQL查询

 SELECT sum(tot) as Customer_Count
    FROM 
       (select count(distinct (slip_no)) as tot 
        from rem_upload r
        where  (r.location_id like '001') and (r.tran_type <>'Z' and r.tran_type <> 'X') and (r.tran_date >= '2014-01-02' and r.tran_date  <= '2014-01-03') 
        group by mech_no)   
as C_count

输出:

Customer_Count
379

我需要在指定的时间段内获得每天的总客户数。

现在我在输出中得到的是给定时期的总客户数。

谢谢,

2 个答案:

答案 0 :(得分:0)

未经测试..但这个想法是这样的。

尝试替换以下内容:

您的自定义日期

<your_specified_date>

如果您有时间戳列类型

,请

r.<timestamp_column>

<your_specified_time_range>按您的自定义时间。

SELECT sum(tot) as Customer_Count
FROM 
   (select count(distinct (slip_no)) as tot 
    from rem_upload r
    where (r.location_id like '001') 
      and (r.tran_type <>'Z' and r.tran_type <> 'X') 
      and (r.tran_date >= '2014-01-02' and r.tran_date  <= '2014-01-03')
      and r.tran_date = <your_specified_date>
      and r.<timestamp_column> = <your_specified_time_range>
    group by mech_no)   
as C_count

答案 1 :(得分:0)

沿着这些方向:

 SELECT C_count.tran_date
        ,SUM(C_count.tot) as Customer_Count
 FROM 
       (SELECT r.tran_date
       ,COUNT(distinct (slip_no)) as tot 
        FROM rem_upload r
        WHERE  (r.location_id like '001') and (r.tran_type <>'Z' and r.tran_type <> 'X') and (r.tran_date >= '2014-01-02' and r.tran_date  <= '2014-01-03') 
        GROUP BY r.mech_no
                 ,r.tran_date)as C_count
  GROUP BY tran_date