0

嗨,我有一张如下所示的表格:

                  Table Name: Emails
    ID        |CreatedDate             |finalStatus
    115173922 |2013-04-09 12:33:23.234 |DELIVERED
    115123432 |2013-04-09 08:21:23.234 |FAILED
    115143212 |2013-04-09 12:24:23.234 |DELIVERED
    115173922 |2013-04-09 05:05:23.234 |DELIVERED
    111233922 |2013-04-10 12:44:23.234 |PENDING
    115123912 |2013-04-10 12:05:23.234 |DELIVERED
    115173922 |2013-04-11 22:09:23.234 |DELIVERED
    111233922 |2013-04-11 13:05:23.234 |PENDING
    115123912 |2013-04-11 05:23:23.234 |DELIVERED

我需要做的是获得该月每天交付、失败和待处理的 finalStatus 的总量。我试图修改人们在以前的答案中给出的 MySQL 代码,例如:SQL query for Calculating Total No. of Orders per Day?但一直无法让它工作。

这是我到目前为止的代码:

    SELECT DISTINCT  (CAST(CreatedDate as DATE)) as Date,

    (SELECT COUNT(finalStatus)
    FROM [Emails]
    WHERE finalStatus = 'DELIVERED') AS Delivered,

    (SELECT COUNT(finalStatus)
    FROM [Emails]
    WHERE finalStatus = 'FAILED') AS Failed,

    (SELECT COUNT(finalStatus)
    FROM [Emails]
    WHERE finalStatus = 'PENDING') AS Pending

    FROM [Emails]
    GROUP BY (CAST(CreatedDate as DATE))

如果有人可以帮助我,那就太棒了。我已经坚持了几个小时了,可能很快就会发疯......

4

6 回答 6

4

既然是这样,就只用SQL Server 2008使用铸造CREATEDDATE成,DATECAST()

SELECT CAST(E.CreatedDate AS DATE) DateCreated,
       COUNT(case when E.finalStatus = 'DELIVERED' then 1 end) as DELIVERED,
       COUNT(case when E.finalStatus = 'FAILED' then 1 end) as FAILED,
       COUNT(case when E.finalStatus = 'PENDING' then 1 end) as PENDING
FROM    TableName E
GROUP   BY CAST(E.CreatedDate AS DATE)

http://www.sqlfiddle.com/#!3/dc195/4

于 2013-04-15T11:01:26.953 回答
1

我认为你需要这样的东西:

日期名称

select CAST(CreatedDate as DATE),
       sum(case when E.finalStatus = 'DELIVERED' then 1 else 0 end) as DELIVERED,
       sum(case when E.finalStatus = 'FAILED' then 1 else 0 end) as FAILED,
       sum(case when E.finalStatus = 'PENDING' then 1 else 0 end) as PENDING
from Emails E
where DATENAME(MONTH,E.CreatedDate)='your month name'
group by CAST(E.CreatedDate as DATE)

SQL小提琴

于 2013-04-15T10:52:29.860 回答
1

您可以像这样每个月有所不同:

SELECT year(datestart), month(datestart)
finalStatus,
COUNT(finalStatus)
FROM [Emails]
GROUP BY year(datestart), month(datestart),finalStatus 

像这样的分组是一种比子查询更快的方法。

于 2013-04-15T10:57:42.747 回答
0

尝试这个

 SELECT DISTINCT  (CAST(CreatedDate as DATE)) as Date,

    (SELECT COUNT(finalStatus)
    FROM [Emails] E1
    WHERE finalStatus = 'DELIVERED'  AND 
    CAST(E1.CreatedDate as DATE) = CAST(E.CreatedDate as DATE)) AS Delivered,

    (SELECT COUNT(finalStatus)
    FROM [Emails] E2
    WHERE finalStatus = 'FAILED' AND 
     CAST(E2.CreatedDate as DATE) = CAST(E.CreatedDate as DATE)) AS Failed,

    (SELECT COUNT(finalStatus)
    FROM [Emails] E3
    WHERE finalStatus = 'PENDING' AND 
     CAST(E3.CreatedDate as DATE) = CAST(E.CreatedDate as DATE)) AS Pending

    FROM [Emails] E
    GROUP BY (CAST(CreatedDate as DATE))
于 2013-04-15T10:56:46.227 回答
0
with daily_figures as
(
select 
 cast(CreatedDate as date) as CreatedDate,
 finalStatus,
 Count(*) as DayCount
From 
  EMails
Group 
  by cast(CreatedDate as date), FinalStatus
)
select * from daily_figures 
Where 
  datepart(month, CreatedDate) = 4 and
  datepart(year, CreatedDate) = 2013

这取决于您的申请情况,您如何提供必要的年份。

Sql 小提琴

于 2013-04-15T11:11:10.500 回答
0

如果您只想获取有交易的日期,请忽略此答案,因为两者LUVSkinny Pipes 已经证明了

考虑到您想获取整个月份的所有日期APRIL,您需要首先生成一个整个月份的日历表,APRIL然后将它与您的表连接起来。此查询使用递归 CTE 创建日历。

WITH April_Calendar
AS
(
  SELECT CAST('20130401' as datetime) AS [date]
  UNION ALL
  SELECT DATEADD(dd, 1, [date])
  FROM   April_Calendar
  WHERE  DATEADD(dd, 1, [date]) <= '20130430'
)
SELECT  a.[Date] DateCreated,
        COUNT(case when E.finalStatus = 'DELIVERED' then 1 end) as DELIVERED,
        COUNT(case when E.finalStatus = 'FAILED' then 1 end) as FAILED,
        COUNT(case when E.finalStatus = 'PENDING' then 1 end) as PENDING
FROM    April_Calendar a
        LEFT JOIN Emails E
            ON a.[date] = CAST(E.CreatedDate AS DATE)
GROUP   BY a.[Date]
于 2013-04-15T11:17:59.513 回答