SQL SERVER计算一天的一条记录

时间:2014-04-24 10:38:33

标签: sql sql-server

我有一个以下查询,它会计算七个网站上周的订单数量。 如果上周没有给定站点的订单,它显示0而不是根本不显示该站点,因此我使用Case语句的原因。 一切都很好,挑战是,对于给定的日期可以说21/04/2014如果有多个网站订单,我的查询shoudl只考虑一个记录 ,所以对于site4,计数值是4但它应该是3,因为它的4是因为本周21日它有两个订单条目。 有没有办法,如果在那个日期找到两个以上的订单日期,我只能在给定的一天添加1个网站? 我正在考虑使用min(orderdate),但是sql给了我错误,我不能在子查询或聚合函数中使用聚合函数。

select  SITENAME
    ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
              Then 1
            Else 0 
            End) as COMPLETED
from TABLE1 where CLIENT in ('SITE1','SITE2','SITE3','SITE4','SITE5','SITE6','SITE7')
GROUP BY SITENAME
order by SITENAME

SITENAME...............COMPLETED

SITE1.....................0

SITE2.....................0

SITE3.....................0

SITE4.....................4 *(the count should be 3 if you only consider unique dates for that site)

SITE5.....................2

SITE6.....................3

SITE7.....................2

4 个答案:

答案 0 :(得分:2)

您可以在内部查询中获取不同的日期,然后在顶部运行查询:

select SITENAME
    ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
              Then 1
            Else 0 
            End) as COMPLETED
from 
( 
   SELECT DISTINCT SITENAME, OrderDate FROM TABLE1
   where CLIENT in ('SITE1','SITE2','SITE3','SITE4','SITE5','SITE6','SITE7') 
) X
GROUP BY SITENAME
order by SITENAME

注意,当我在内部查询中使用distinct时,现在不需要按地址分组。

答案 1 :(得分:1)

一种简单易读的方法是使用CTE:

;WITH data AS (
    SELECT DISTINCT Sitename, 
    DATEADD(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690) Date,
    ORDERDATE,
    FROM TABLE1 
    WHERE CLIENT in ('SITE1','SITE2','SITE3','SITE4','SITE5','SITE6','SITE7')
)
select  SITENAME
    ,SUM(Case When  OrderDate >= Date Then 1 Else 0 End) as COMPLETED
from data 
GROUP BY SITENAME 
order by SITENAME

答案 2 :(得分:1)

使用count(distinct)

select SITENAME,
       COUNT(DISTINCT Case When OrderDate >= dateadd(dd,(datediff(dd, -53690, getdate()-1)/7)*7,-53690)
                           Then OrderDate
             End) as COMPLETED
from TABLE1
where CLIENT in ('SITE1','SITE2','SITE3','SITE4','SITE5','SITE6','SITE7')
GROUP BY SITENAME
order by SITENAME;

这假设OrderDate上没有时间组件。如果是,那么您可以将Then OrderDate替换为Then cast(OrderDate as Date)

答案 3 :(得分:1)

数据结构似乎不正确,例如,如果TABLE1是Order表,那么您在整个表上执行Group By

一种好方法是使用SITE的主表,然后对过滤后的记录进行计数查询,例如,

DECLARE @tblAddress table(AddressID varchar(50));
DECLARE @tblOrders table(OrderAddress varchar(50), OrderID varchar(10), OrderDate date);

INSERT INTO @tblAddress(AddressID)
    SELECT    'SITE1'
    UNION
    SELECT    'SITE2'
    UNION
    SELECT    'SITE3'
    UNION
    SELECT    'SITE4'
    UNION
    SELECT    'SITE5'
    UNION
    SELECT    'SITE6'
    UNION
    SELECT    'SITE7'


INSERT INTO @tblOrders(OrderAddress, OrderID, OrderDate)
    SELECT    'SITE4', 'SITE4-01', '21-Apr-2014'        -- Site-4, order count = 1
    UNION
    SELECT    'SITE5', 'SITE5-01', '21-Apr-2014'
    UNION
    SELECT    'SITE6', 'SITE6-01', '21-Apr-2014'
    UNION
    SELECT    'SITE7', 'SITE7-01', '21-Apr-2014'
    UNION
    SELECT    'SITE4', 'SITE4-02', '22-Apr-2014'        -- Site-4, order count = 2
    UNION
    SELECT    'SITE5', 'SITE5-02', '22-Apr-2014'
    UNION
    SELECT    'SITE5', 'SITE5-03', '22-Apr-2014'
    UNION
    SELECT    'SITE6', 'SITE6-02', '22-Apr-2014'
    UNION
    SELECT    'SITE7', 'SITE7-02', '22-Apr-2014'
    UNION
    SELECT    'SITE4', 'SITE4-03', '23-Apr-2014'        -- Site-4, order count = 3
    UNION
    SELECT    'SITE7', 'SITE7-03', '23-Apr-2014'
    UNION
    SELECT    'SITE4', 'SITE4-04', '24-Apr-2014'        -- Site-4, order count = 4
    UNION
    SELECT    'SITE4', 'SITE4-05', '24-Apr-2014'        -- Site-4, order count = 5
    UNION
    SELECT    'SITE4', 'SITE4-06', '24-Apr-2014'        -- Site-4, order count = 6
    UNION
    SELECT    'SITE4', 'SITE4-07', '24-Apr-2014'        -- Site-4, order count = 7


SELECT    AddressID,
        (
        SELECT    COUNT(DISTINCT O.OrderDate)
        FROM    @tblOrders AS O    
        WHERE    OrderAddress = A.AddressID
                -- put your order filtering here
        ) AS TotalOrders
FROM    @tblAddress AS A