Domanda

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. 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.

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?

È stato utile?

Soluzione

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.

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

Altri suggerimenti

@t-clausen.dk thanks for your help with this. I had not previously used coalesce for anything so I learned something useful today. 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.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top