如何格式化 table 内容

how to format a table content

我是 database.I 的新手,执行过类似

的查询
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

谢谢 莫妮卡

我认为问题出在您的 PIVOT 语句的以下部分:

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

应该是

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

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