SQL 组不同计数
我有下表,
User ID Start Date End Date
-------------------------------------
John Doe Mar 11 2011 May 28 2011
Robret S Mar 21 2011 Jun 29 2011
Tina T Feb 01 2011 August 20 2011
我想显示过去 6 个月有多少人可以使用,即使该月没有人。这怎么可能。我知道我必须进行分组并使用不同的计数。
预期输出:
February = 1 Resource
March = 3 Resources
April = 3 Resources
May = 3 Resources
June = 2 Resources
July = 1 Resource
August = 1 Resource
I have the following table
User ID Start Date End Date
-------------------------------------
John Doe Mar 11 2011 May 28 2011
Robret S Mar 21 2011 Jun 29 2011
Tina T Feb 01 2011 August 20 2011
I want to show how many people I have available for the past 6 months, even if the month has no people. How can this be possible. I know I have to do grouping and use distinct count.
Expected Output:
February = 1 Resource
March = 3 Resources
April = 3 Resources
May = 3 Resources
June = 2 Resources
July = 1 Resource
August = 1 Resource
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
结果:
Results:
您将需要过去 6 个月的现有数据记录,以便可以合并两组数据。您可以在 CTE 中生成过去 6 个月的数据,并对数据进行左连接。即使您没有数据,这也可以让您显示过去 6 个月的情况。
You will need the existing data records for the last 6 months, so that you can merge the two sets of data. You can generate the last 6 months in a CTE and do a left join with your data. That will allow you to show the last 6 months even if you have no data.
我认为你不能使用“简单”的 select 语句(甚至使用 GROUPing 等)来做你想做的事情。以下是我的想法,所以你必须稍微尝试一下,最好阅读Joe Celko 的优秀SQL for Smarties 书籍。
您需要创建第二个表,其中包含所有月份(开始/结束日期)。对于所有类型的类似查询,您只需要一张表,并且它必须包含您感兴趣查询的日期范围内的所有月份:
然后,您可以从用户表到本月表进行 LEFT OUTER JOIN。这将为您提供每个用户每个可用月份的一行,您可以根据需要对其进行分组:
希望有帮助。
I don't think you can do what you want using a "simple" select statement (even using GROUPing etc.) The following is off the top of my head, so you'll have to experiment with it a little, and preferably read Joe Celko's excellent SQL for Smarties book.
You need to create a second table that contains all of your months (start/end dates). You only need one table for all types of similar queries, and it must contain all the months in the date ranges your interested in querying:
You then LEFT OUTER JOIN from your user table to this month table. That will give you a row for each user for each month they were available, which you can GROUP as required:
Hope that helps.