我需要帮助构建一个SQL查询,以根据我的日期列返回Jan-Dec列的计数

时间:2013-01-22 20:06:39

标签: c# sql sql-server sql-server-2008 pivot

我不是SQL的专家,我甚至不确定这种类型的查询是否可行。

我想根据“MediaDate”为每个月的每个“MediaTypeID”返回一个计数(*)。

非常感谢任何帮助!

感谢。

我的表格如下:

MediaTable

表格数据如下:

1 | 1 | Funny Cat Video     | 2006-01-25 00:00:00.000
2 | 1 | Funny Dog Video     | 2006-01-20 00:00:00.000
3 | 2 | Angry Birds Game    | 2006-03-13 00:00:00.000
4 | 4 | Blonde Joke         | 2006-03-16 00:00:00.000
5 | 3 | Goofy Clown Picture | 2006-02-27 00:00:00.000
6 | 2 | Racing Game         | 2006-02-10 00:00:00.000
7 | 1 | Star Wars Video     | 2006-07-15 00:00:00.000

查询将返回Jan-Dec的12行结果,如下所示:

Month | MediaTypeID1Count | MediaTypeID2Count | MediaTypeID3Count | MediaTypeID4Count
Jan   | 400               | 255               | 15                | 65
Feb   | 100               | 25                | 75                | 35
Mar   | 320               | 155               | 50                | 99
Apr   | 56                | 0                 | 98                | 313

3 个答案:

答案 0 :(得分:4)

此类数据转换称为PIVOT。 SQL Server 2005+具有可以实现的数据透视功能:

select month,
  [1] MediaType1_count,
  [2] MediaType2_count,
  [3] MediaType3_count,
  [4] MediaType4_count
from 
(
  select 
    mediatypeid,
    datename(m, mediadate) Month,
    datepart(m, mediadate) monnum
  from yourtable
) src
pivot
(
  count(mediatypeid)
  for mediatypeid in ([1], [2], [3], [4])
) piv
order by monnum

请参阅SQL Fiddle with Demo

如果要将未转换的值转换为列,则可以使用动态sql:

DECLARE @cols AS NVARCHAR(MAX),
    @colNames AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(mediatypeid) 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


select @colNames = STUFF((SELECT distinct ', ' + QUOTENAME(mediatypeid) +' as MediaType' + cast(mediatypeid as varchar(50))+'_Count' 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT month,' + @colNames + ' from 
             (
                select mediatypeid,
                  datename(m, mediadate) Month,
                  datepart(m, mediadate) monnum
                from yourtable
            ) x
            pivot 
            (
                count(mediatypeid)
                for mediatypeid in (' + @cols + ')
            ) p 
            order by monnum'

execute(@query)

请参阅SQL Fiddle with Demo

结果如下:

|    MONTH | MEDIATYPE1_COUNT | MEDIATYPE2_COUNT | MEDIATYPE3_COUNT | MEDIATYPE4_COUNT |
----------------------------------------------------------------------------------------
|  January |                2 |                0 |                0 |                0 |
| February |                0 |                1 |                1 |                0 |
|    March |                0 |                1 |                0 |                1 |
|     July |                1 |                0 |                0 |                0 |

答案 1 :(得分:0)

我认为这可能就是你要找的东西。以下将根据媒体类型ID返回计数,然后按年份分组,然后按月分组

Select MediaTypeID, datepart(year, MediaDate), datepart(month, MediaDate), count(*)
From Media
Group by MediaTypeID, datepart(year, MediaDate), datepart(month, MediaDate)

答案 2 :(得分:-1)

在我看来,对于您来说这可能是一个很好的挑战:

Declare @T  Table   (
    MediaID     BigInt  Primary Key Identity(1, 1)
,   MediaTypeID BigInt
,   MediaTitle  Nvarchar(50)
,   MediaDate   DateTime
);

Insert  @T  (
    MediaTypeID
,   MediaTitle
,   MediaDate
)   Select  1
    ,   'Funny Cat Video'
    ,   '2006-01-25 00:00:00.000'
Union
    Select  1
    ,   'Funny Dog Video'
    ,   '2006-01-20 00:00:00.000'
Union
    Select  2
    ,   'Angry Birds Game'
    ,   '2006-03-13 00:00:00.000'
Union
    Select  4
    ,   'Blonde Joke'
    ,   '2006-03-16 00:00:00.000'
Union
    Select  3
    ,   'Goofy Clown Picture'
    ,   '2006-02-27 00:00:00.000'
Union
    Select  2
    ,   'Racing Game'
    ,   '2006-02-10 00:00:00.000'
Union
    Select  1
    ,   'Star Wars Video'
    ,   '2006-07-15 00:00:00.000'
;

Select  Month.Title
,   Count(Type01_Result.MediaID)    As  MediaTypeID_1
,   Count(Type02_Result.MediaID)    As  MediaTypeID_2
,   Count(Type03_Result.MediaID)    As  MediaTypeID_3
,   Count(Type04_Result.MediaID)    As  MediaTypeID_4
    From    (
        Select  1       As  Id
        ,   'Jan'       As  Title
    Union
        Select  2
        ,   'Feb'
    Union
        Select  3
        ,   'March'
    Union
        Select  4
        ,   'April'
    Union
        Select  5
        ,   'May'
    Union
        Select  6
        ,   'June'
    Union
        Select  7
        ,   'July'
    Union
        Select  8
        ,   'Aug'
    Union
        Select  9
        ,   'Sept'
    Union
        Select  10
        ,   'Nov'
    Union
        Select  11
        ,   'Oct'
    Union
        Select  12
        ,   'Dec'
    )   As  Month
    Left    Outer   Join
        @T  As  Type01_Result
    On  Type01_Result.MediaTypeID           =   1
    And DatePart(Month, Type01_Result.MediaDate)    =   Month.Id
    Left    Outer   Join
        @T  As  Type02_Result
    On  Type02_Result.MediaTypeID           =   2
    And DatePart(Month, Type02_Result.MediaDate)    =   Month.Id
    Left    Outer   Join
        @T  As  Type03_Result
    On  Type03_Result.MediaTypeID           =   3
    And DatePart(Month, Type03_Result.MediaDate)    =   Month.Id
    Left    Outer   Join
        @T  As  Type04_Result
    On  Type04_Result.MediaTypeID           =   4
    And DatePart(Month, Type04_Result.MediaDate)    =   Month.Id
    Group   By  Month.Title
    ;

唯一的一点是你应该小心年份值以及你想在输出中包含多少年,因为这个结果将为你提供每个月所有记录的总和,无论哪一年记录的日期是。这可能会导致结果混乱。

(请记住,我总是在Management Studio的选项中将标签大小设置为8个字符,因此我建议您这样做以查看正确的T-SQL编写风格)

希望它对你有所帮助。

干杯