1

我不是 SQL 专家,我什至不确定这种查询是否可行。

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

任何帮助将不胜感激!

谢谢。

我的表看起来像:

媒体表

表数据如下所示:

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

该查询将返回一月至十二月的 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
4

3 回答 3

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

如果您有未知数量的值要转置为列,则可以使用动态 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

结果将如下所示:

|    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 |
于 2013-01-22T20:50:09.150 回答
0

我想这可能是你正在寻找的。以下将简单地返回基于媒体类型 id 的计数,然后按年分组,然后按月分组

Select MediaTypeID, datepart(year, MediaDate), datepart(month, MediaDate), count(*)
From Media
Group by MediaTypeID, datepart(year, MediaDate), datepart(month, MediaDate)
于 2013-01-22T20:35:16.903 回答
-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的Options中总是将tab size设置为8个字符,所以我建议你这样做看看正确的T-SQL编写方式)

希望它可以帮助你。

干杯

于 2013-01-22T20:25:47.900 回答