如何格式化表格内容

时间:2015-04-14 08:30:27

标签: sql sql-server-2008 pivot

我是数据库的新手。我已经执行了像

这样的查询
SELECT   name                                       as request_name, 
         count(name)                                as no_of_open_req, 
         DATENAME(MM, Convert(DATE, created_date))  as month 
FROM     usm_request 
WHERE    DATENAME(YEAR, Convert(DATE,created_date)) = DATENAME(YEAR,GETDATE()) 
GROUP BY name, 
         DATENAME(YEAR, Convert(DATE, created_date)),
         DATENAME(MM, Convert(DATE,created_date)) 
ORDER BY DATENAME(MM, Convert(DATE, created_date)) 

得到了结果

 request_name                no_of_open_req      month

Computer Request                   1             April
Desk Phone Request                 1             April
E-mail ID Creation Request         1             April
Computer Request                   19            February
Desk Phone Request                 12            February
Email ID Creation Request           8            February
Computer Request                    45           January
Desk Phone Request                  28           January
Email ID Creation Request           55           January
Computer Request                    18           March
Desk Phone Request                  24           March
E-mail ID Creation Request          35           March

但我们需要像

这样的结果
  request_name                  January   February    March    April

Computer Request                   45        19         18       1
Desk Phone Request                 28        12         24       1
E-mail ID Creation Request         55        8          35       1

请帮忙。

我们已经尝试过此查询..

     SELECT * from (select name as [request_name],
                    count(name) as [no_of_open_req],
                    DATENAME(MM, Convert(DATE, created_date)) as [month] FROM usm_request WHERE DATENAME(YEAR,Convert(DATE,created_date))=DATENAME(YEAR,GETDATE()) group by name, DATENAME(YEAR, Convert(DATE, created_date)),DATENAME(MM, Convert(DATE,created_date)))as t PIVOT(sum(no_of_open_req) FOR month IN (['January'],['February'],['March'],['April'])) AS PivotTable

我们得到了这个结果。所有空值

          request_name              'January'   'February'  'March' 'April'
     Cell Phone Allocation             NULL         NULL      NULL    NULL
     Computer Request                  NULL         NULL      NULL    NULL
     Desk Phone Request                NULL         NULL      NULL    NULL
     Desk Phone Request test           NULL         NULL      NULL    NULL
     Email ID Creation Request         NULL         NULL      NULL    NULL
     E-mail ID Creation Request        NULL         NULL      NULL    NULL
     International Dialing Request     NULL         NULL      NULL    NULL
     New Employee Request              NULL         NULL      NULL    NULL
     New Non-Employee Request          NULL         NULL      NULL    NULL
     Onboard a Non-Employee            NULL         NULL      NULL    NULL
     Onboard a Non-Employee – Step 1   NULL         NULL      NULL    NULL
     Onboard a Non-Employee - Step 2   NULL         NULL      NULL    NULL

由于 莫妮卡

1 个答案:

答案 0 :(得分:1)

我认为问题出在PIVOT声明的以下部分:

FOR month IN (['January'],['February'],['March'],['April'])

相反它应该是

FOR month IN ([January],[February],[March],[April])

所以,没有单引号。如果在矩形括号之间添加这些单引号,则该语句实际上将查找包含单引号的月份值。例如,如果您的PIVOT列表中有['January'],则该语句将查找Month值为'January'。当然,Month列中的值中没有单引号。