1

我有一个很棒的查询:

select *
from
(
  select MONTH(x.status_date) m
    , YEAR(x.status_date) y
    ,f.REQUISITION_TYPE
  from (select distinct accession_id,status_date from [XREF_WET_MASTER]) x

  join F_ACCESSION_DAILY f
    on x.accession_id=f.accession_id
) src
pivot 
(
  count(src.REQUISITION_TYPE) 
  for src.REQUISITION_TYPE in ([11/12 Panel]
,[6 Panel]
,[Non-POC]
,[Oral Fluids]
,[POC Other])
) as CountofThatTypeofReq

返回:

+----+------+-------------+---------+---------+-------------+-----------+
| m  |  y   | 11/12 Panel | 6 Panel | Non-POC | Oral Fluids | POC Other |
+----+------+-------------+---------+---------+-------------+-----------+
|  9 | 2012 |        6941 |    1170 |    2138 |          98 |      1446 |
|  7 | 2012 |        7906 |    1384 |    1984 |          98 |      1576 |
| 11 | 2012 |        4442 |     715 |    1436 |          59 |       833 |
| 10 | 2012 |        8027 |    1036 |    2645 |          86 |      1374 |
|  4 | 2012 |        5237 |     647 |    1502 |          27 |      1072 |
|  5 | 2012 |        5968 |     807 |    1884 |          40 |      1223 |
|  6 | 2012 |        6170 |    1130 |    1947 |          55 |      1268 |
|  8 | 2012 |        8846 |    1335 |    2877 |         121 |      1598 |
+----+------+-------------+---------+---------+-------------+-----------+

对于每个面板中的每一行,我怎样才能像这样返回整体的百分比:

+----+------+-------------+-----+---------+-----+---------+-----+-------------+----+-----------+
| m  |  y   | 11/12 Panel |     | 6 Panel |     | Non-POC |     | Oral Fluids |    | POC Other |
+----+------+-------------+-----+---------+-----+---------+-----+-------------+----+-----------+
|  9 | 2012 |        6941 | 59% |    1170 | 24% |    2138 | 58% |          98 | 6% |      1446 |
|  7 | 2012 |        7906 | 61% |    1384 | 27% |    1984 | 54% |          98 | 6% |      1576 |
| 11 | 2012 |        4442 | 59% |     715 | 23% |    1436 | 62% |          59 | 7% |       833 |
| 10 | 2012 |        8027 | 61% |    1036 | 20% |    2645 | 64% |          86 | 6% |      1374 |
|  4 | 2012 |        5237 | 62% |     647 | 20% |    1502 | 58% |          27 | 2% |      1072 |
|  5 | 2012 |        5968 | 60% |     807 | 20% |    1884 | 60% |          40 | 3% |      1223 |
|  6 | 2012 |        6170 | 58% |    1130 | 26% |    1947 | 60% |          55 | 4% |      1268 |
|  8 | 2012 |        8846 | 60% |    1335 | 23% |    2877 | 63% |         121 | 7% |      1598 |
+----+------+-------------+-----+---------+-----+---------+-----+-------------+----+-----------+

例如,第一个百分比值59% = 100*(6941 / (6941+1170+2138+98+1446))

这是我尝试过的:

select m,y,[11/12 Panel],[11/12 Panel]/([11/12 Panel]
                                        +[6 Panel]
                                        +[Non-POC]
                                        +[Oral Fluids]
                                        +[POC Other]) percentage
                        ,[6 Panel], [6 Panel]/([11/12 Panel]
                                        +[6 Panel]
                                        +[Non-POC]
                                        +[Oral Fluids]
                                        +[POC Other]) percentage2
                        etc.......                              
from
(
select *
from
(
  select MONTH(x.status_date) m
    , YEAR(x.status_date) y
    ,f.REQUISITION_TYPE
  from (select distinct accession_id,status_date from [XREF_WET_MASTER]) x

  join F_ACCESSION_DAILY f
    on x.accession_id=f.accession_id
) src
pivot 
(
  count(src.REQUISITION_TYPE) 
  for src.REQUISITION_TYPE in ([11/12 Panel]
,[6 Panel]
,[Non-POC]
,[Oral Fluids]
,[POC Other])
) as CountofThatTypeofReq
) mypivot

它有效,但我想知道是否

有更简洁/更智能的方法吗?

4

1 回答 1

1
select m, y, MAX([11/12 Panel]) AS [11/12 Panel],
             MAX(CASE WHEN [11/12 Panel] IS NOT NULL THEN [Percent] END) AS [Perc_11/12 Panel],
             MAX([6 Panel]) AS [6 Panel],
             MAX(CASE WHEN [6 Panel] IS NOT NULL THEN [Percent] END) AS [Perc_6 Panel],
             MAX([Non-POC]) AS [Non-POC],
             MAX(CASE WHEN [Non-POC] IS NOT NULL THEN [Percent] END) AS [Perc_Non-POC],
             MAX([Oral Fluids]) AS [Oral Fluids],
             MAX(CASE WHEN [Oral Fluids] IS NOT NULL THEN [Percent] END) AS [Perc_Oral Fluids],
             MAX([POC Other]) AS [POC Other],
             MAX(CASE WHEN [POC Other] IS NOT NULL THEN [Percent] END) AS [Perc_POC Other]
from
(
  select MONTH(x.status_date) m
         , YEAR(x.status_date) y
         , f.REQUISITION_TYPE
         COUNT(*) OVER(PARTITION BY f.REQUISITION_TYPE, x.status_date) AS [Count],
         100 * (COUNT(*) OVER(PARTITION BY f.REQUISITION_TYPE, x.status_date) * 1.00
         / COUNT(*) OVER(PARTITION BY x.status_date)) AS [Percent]
  from (select distinct accession_id,status_date from [XREF_WET_MASTER]) x
  join F_ACCESSION_DAILY f
    on x.accession_id=f.accession_id
) src
pivot 
(
 MAX(src.[Count])
   for src.REQUISITION_TYPE in ([11/12 Panel]
                                ,[6 Panel]
                                ,[Non-POC]
                                ,[Oral Fluids]
                                ,[POC Other])
) as CountofThatTypeofReq
GROUP BY m, y

我在SQLFiddle上的简单示例

如果可以包含在 PIVOT [Percent] 列语句中会更聪明;)

于 2012-11-27T00:03:52.553 回答