2017-08-13 68 views
0

考慮:SQL移動一排是列 - PIVOT查詢

+-----------+---------------+-------------+-------+ 
| Name | Location | Description | Value | 
+-----------+---------------+-------------+-------+ 
| Company A | Houston  | Sales  | 100 | 
| Company A | Houston  | Profit  | 50 | 
| Company B | San Francisco | Sales  | 500 | 
| Company B | San Francisco | Profit  | 200 | 
| Company C | New York  | Sales  | 200 | 
| Company C | San Francisco | Profit  | 150 | 
+-----------+---------------+-------------+-------+ 

如何使用SQL將它轉換爲:

+----------+---------------+--------+---------+ 
| Name | Location | Sales | Profit | 
+----------+---------------+--------+---------+ 
| CompanyA | Houston  | 100 |  50 | 
| CompanyB | San Francisco | 500 |  200 | 
| CompanyC | New York  | 200 |  150 | 
+----------+---------------+--------+---------+ 

回答

3

嘗試1或2個查詢:http://sqlfiddle.com/#!6/bb33b/7
第二個給你的問題出準確的結果,但我的猜測是,而第一個是你在找什麼。

SELECT Name, Location, 
     Sum(Case Description when 'Sales' Then Value else 0 end) Sales, 
     Sum(Case Description when 'Profit' Then Value else 0 end) Profit 
FROM table1 
GROUP BY Name, Location 
Order by 1,2 
; 

|  Name |  Location | Sales | Profit | 
|-----------|---------------|-------|--------| 
| Company A |  Houston | 100 |  50 | 
| Company B | San Francisco | 500 | 200 | 
| Company C |  New York | 200 |  0 | 
| Company C | San Francisco |  0 | 150 | 

SELECT Name, Min(Location) As Location, 
     Sum(Case Description when 'Sales' Then Value else 0 end) Sales, 
     Sum(Case Description when 'Profit' Then Value else 0 end) Profit 
FROM table1 
GROUP BY Name 
Order by 1,2 
; 
; 

|  Name |  Location | Sales | Profit | 
|-----------|---------------|-------|--------| 
| Company A |  Houston | 100 |  50 | 
| Company B | San Francisco | 500 | 200 | 
| Company C |  New York | 200 | 150 | 
0

假設你的描述值只有永遠SalesProfit ,這是一個CTE解決方案。我打電話給初始表Before

with cteSales as (select name, value from Before where description = 'Sales') 
, cteProfit as (select name, value from Before where description = 'Profit') 
select 
distinct 
    name 
    , location 
    , (select value from cteSales where name = B.name) as Sales 
    , (select value from cteProfit where name = B.name) as Profit 
from Before B 
0

要改變你的數據,你需要的形式,你可以這樣做:

SELECT NAME 
      ,LOCATION 
      ,SUM (CASE WHEN DESCRIPTION = 'Sales' THEN Values ELSE 0 END) AS SALES 
      ,SUM (CASE WHEN DESCRIPTION = 'Profit' THEN Values ELSE 0 END) AS PROFIT 
FROM yourtable 
GROUP BY NAME 
       ,LOCATION 
0

您可以使用PIVOT功能行值轉換成列但它將提供稍微不同但更合理的輸出 -

SELECT Name, Location, Sales, Profit 
FROM 
(
    SELECT * 
    FROM Table1 
) AS SourceTable 
PIVOT 
(
    MIN(value) 
    FOR Description IN (Sales, Profit) 
) AS PivotTable 
ORDER BY Name, Location; 

將產生以下輸出 -

+-----------+---------------+-------+--------+ 
| Name  | Location  | Sales | Profit | 
+-----------+---------------+-------+--------+ 
| Company A | Houston  | 100 | 50  | 
+-----------+---------------+-------+--------+ 
| Company B | San Francisco | 500 | 200 | 
+-----------+---------------+-------+--------+ 
| Company C | New York  | 200 | null | 
+-----------+---------------+-------+--------+ 
| Company C | San Francisco | null | 150 | 
+-----------+---------------+-------+--------+ 

可以使用此作爲一個替代的解決方案。