简体   繁体   English

SQL查询以统计在日期范围内与某些条件匹配的响应数,并按每天分组显示

[英]SQL Query to Count Number of Responses Matching Certain Criteria over a Date Range and Display as Grouped per Day

I have the following set of survey responses in a table. 我在表中有以下调查问卷集。

It's not very clear but the numbers represent the 'satisfaction' level where: 尚不清楚,但数字代表“满意”级别,其中:

0 = happy
1 = neutral
2 = sad

+----------+--------+-------+------+-----------+-------------------------+
| friendly | polite | clean | rate | recommend |      booking_date       |
+----------+--------+-------+------+-----------+-------------------------+
|        2 |      2 |     2 |    0 |         0 | 2014-02-03 00:00:00.000 |
|        1 |      2 |     0 |    0 |         2 | 2014-02-04 00:00:00.000 |
|        0 |      0 |     0 |    1 |         0 | 2014-02-04 00:00:00.000 |
|        1 |      1 |     2 |    0 |         2 | 2014-02-04 00:00:00.000 |
|        0 |      0 |     1 |    2 |         1 | 2014-02-04 00:00:00.000 |
|        2 |      2 |     0 |    2 |         0 | 2014-02-05 00:00:00.000 |
|        2 |      1 |     1 |    0 |         2 | 2014-02-05 00:00:00.000 |
|        1 |      0 |     1 |    2 |         0 | 2014-02-05 00:00:00.000 |
|        0 |      1 |     1 |    1 |         1 | 2014-02-05 00:00:00.000 |
|        1 |      0 |     2 |    2 |         0 | 2014-02-05 00:00:00.000 |
+----------+--------+-------+------+-----------+-------------------------+

For each day I need the totals of each of the columns matching each response option. 对于每一天,我需要与每个响应选项匹配的每一列的总计。 This will answer the question: "How may people answered happy, neutral or sad for each of the available question options". 这将回答以下问题:“人们如何对每个可用的问题选项回答快乐,中立或悲伤”。

I would then require a recordset returned such as: 然后,我将要求返回一个记录集,例如:

+------------+----------+------------+--------+----------+------------+--------+
|    Date    | FriHappy | FriNeutral | FriSad | PolHappy | PolNeutral | PolSad |
+------------+----------+------------+--------+----------+------------+--------+
| 2014-02-03 |        0 |          0 |      1 |        0 |          0 |      1 |
| 2014-02-04 |        2 |          2 |      0 |        2 |          1 |      1 |
| 2014-02-05 |        1 |          2 |      2 |        2 |          2 |      1 |
+------------+----------+------------+--------+----------+------------+--------+

This shows that on the 4th two responders answered "happy" for the "Polite?" 这表明两个响应者4日对“礼貌?”的回答是“高兴”。 question, one answered "Neutral" and one answered "sad". 问题,一个回答“中立”,另一个回答“悲伤”。

On the 5th, one responder answered "happy" for the Friendly option, two choose "neutral" and two chose "sad". 5日,一名响应者对“友好”选项回答“高兴”,两名选择了“中立”,两名选择了“悲伤”。

I really wish to avoid doing this in code but my SQL isn't great. 我真的很想避免在代码中这样做,但是我的SQL并不是很好。 I did have a look around but couldn't find anything matching this specific requirement. 我确实环顾四周,但找不到符合此特定要求的任何东西。

Obviously this is never going to work (nice if it did) but this may help explain: 显然,这永远都行不通(如果可行的话),但这可能有助于解释:

SELECT cast(booking_date as date) [booking_date], 
COUNT(friendly=0) [FriHappy],
COUNT(friendly=1) [FriNeutral],
COUNT(friendly=2) [FriSad]
FROM [u-rate-gatwick-qsm].[dbo].[Questions]
WHERE booking_date >= '2014-02-01'
AND booking_date <= '2014-03-01'
GROUP BY cast(booking_date as date) 

Any pointers would be much appreciated. 任何指针将不胜感激。

Many thanks. 非常感谢。

Here is a working version of your sample query: 这是示例查询的工作版本:

SELECT cast(booking_date as date) as [booking_date], 
       sum(case when friendly = 0 then 1 else 0 end) as [FriHappy],
       sum(case when friendly = 1 then 1 else 0 end) as [FriNeutral],
       sum(case when friendly = 2 then 1 else 0 end) as [FriSad]
FROM [u-rate-gatwick-qsm].[dbo].[Questions]
WHERE booking_date >= '2014-02-01' AND booking_date <= '2014-03-01'
GROUP BY cast(booking_date as date) 
ORDER BY min(booking_date);

Your expression count(friendly = 0) doesn't work in SQL Server. 您的表达式count(friendly = 0)在SQL Server中不起作用。 Even if it did, it would be the same as count(friendly) -- that is, the number of non- NULL values in the column. 即使这样做,也将与count(friendly)相同-也就是说,列中非NULL值的数量。 Remember what count() does. 记住count()作用。 It counts the number of non-NULL values. 它计算non-NULL值的数量。

The above logic says: add 1 when there is a match to the appropriate friendly value. 上面的逻辑说:当匹配到适当的friendly值时加1。

By the way, SQL Server doesn't guarantee the ordering of results from an aggregation, so I also added an order by clause. 顺便说一句,SQL Server不保证聚合结果的顺序,因此我还添加了order by子句。 The min(booking_date) is just an easy way of ordering by the date. min(booking_date)只是按日期排序的一种简便方法。

And, I didn't make the change, but I think the second condition in the where should be < rather than <= so you don't include bookings on March 1st (even one at exactly midnight). 而且,我没有进行更改,但是我认为where的第二个条件应该是<而不是<=因此您不包括3月1日的预订(即使是恰好在午夜)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM