简体   繁体   English

SQL超过分区计数然后按月分组

[英]SQL over partition count then group by month

I've been playing around with using COUNT(value) OVER (PARTITION BY anothervalue) and I love how fast it is pulling results from my db.我一直在COUNT(value) OVER (PARTITION BY anothervalue)使用COUNT(value) OVER (PARTITION BY anothervalue) ,我喜欢它从我的数据库中提取结果的速度。 I'm trying to step things up now so that I can create a table that will show me multiple counts - one for each month, side by side.我现在正在尝试加强工作,以便我可以创建一个表格来显示多个计数 - 每个月一个,并排。 I'm not sure if it is possible but wanted to check here.我不确定是否可能,但想在这里检查。 I had a look around online but couldn't find any examples of the question having been asked before, so no leads.我在网上环顾四周,但找不到任何之前被问过的问题的例子,所以没有线索。

Here is what I currently have:这是我目前拥有的:

SELECT DISTINCT
TBL.CATEGORY as 'Category Name',
COUNT(TBL.ROW_ID) OVER (PARTITION BY TBL.CATEGORY) as 'Rows in Category'
FROM
MYDB.TBL
WHERE
TBL.FIELD1 = 'something'
AND TBL.FIELD2 = 'somethingelse'
AND TBL.CREATED >= '2014-01-01'
ORDER BY [Rows in Category] desc

Gives me a lovely table like this:给了我一个像这样的可爱的桌子:

Category Name |Rows in Category
 ABC          | 166
 CBA          | 137
 CCC          | 112

Where I'm trying to get now is to subdivide by month so my output ends up looking something like this (does not have to be exact, the headers can be shuffled around):我现在想要的地方是按月细分,这样我的输出最终看起来像这样(不必精确,标题可以随意移动):

JANUARY                          |FEBRUARY
 Category Name |Rows in Category | Category Name |Rows in Category 
  ABC          | 162             |  CBA          | 51
  CBA          | 86              |  CCC          | 32               
  CCC          | 70              |  ABC          | 4

When I try adding GROUP BY it throws an error about something not being contained in an aggregate function.当我尝试添加GROUP BY它会引发关于聚合函数中未包含某些内容的错误。

If I have to I can just stack queries on top of each other and limit each one to only show one month, but that seems like a lot of repetitive code and I'd prefer a side by side view if it can be done.如果必须的话,我可以将查询堆叠在一起,并将每个查询限制为仅显示一个月,但这似乎是很多重复代码,如果可以完成,我更喜欢并排视图。

Any ideas?有任何想法吗?

The format will not be exactly as you described.格式将与您描述的不完全相同。 But this would be a normal way to show the result and all information has been included, make sure you do not display more than 1 year at a time.但这将是显示结果的正常方式,并且已包含所有信息,请确保一次显示的时间不超过 1 年。

Try this:尝试这个:

;WITH x AS
(
   SELECT TBL.CATEGORY, TBL.ROW_ID, datename(month, TBL.CREATED) month
   FROM
      MYDB.TBL
   WHERE
      TBL.FIELD1 = 'something'
      AND TBL.FIELD2 = 'somethingelse'
      AND TBL.CREATED >= '2014-01-01'
      AND TBL.CREATED < '2015-01-01'
)
SELECT CATEGORY as 'Category Name', [January],[February]
FROM x
   PIVOT(
      count([ROW_ID])  
      FOR month
      in([January],[February])  
   )AS p

@t-clausen.dk thanks for your help with this. @t-clausen.dk 感谢您对此的帮助。 I had not previously used coalesce for anything so I learned something useful today.我以前没有使用过coalesce,所以我今天学到了一些有用的东西。 In the end, since I it turns out I need a total column anyway, I added a YTD column and was able to order by that.最后,因为事实证明我无论如何都需要一个总列,所以我添加了一个 YTD 列并且能够通过它进行排序。

Wanted to post the final result in case it is of use to anyone else:想发布最终结果,以防对其他人有用:

WITH X AS

(
SELECT 
tbl.category,
tbl.row_id,
DATENAME(MONTH, tbl.created) MONTH,
COUNT(tbl.row_id) OVER (PARTITION BY tbl.category) AS 'YTD'
FROM
db.tbl
WHERE
tbl.randomcolumn = 'something specific'
AND tbl.anothercolumn = 'something else specific'
AND created >= '2014-01-01'
AND created < '2015-01-01'
)

SELECT
category AS 'Category Name',
[January], [February], [March], [April], [May], [June], [July], [August], [September], [October], [November], [December],
[YTD]
FROM X

   PIVOT
   (COUNT([row_id]) 
   FOR MONTH
   in([January], [February], [March], [April], [May], [June], [July], [August], [September], [October], [November], [December])
   )AS P

ORDER BY [YTD] DESC

Switch out the lower case parts for the equivalents in your own db/table if looking to repurpose this query.如果要重新调整此查询的用途,请为您自己的 db/table 中的等效项切换小写部分。

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

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