2016-08-30 2 views
2

Ich habe diese Tabellen:SQL Query Dividing 1 Spalte 3 Spalten

tbl_Masterlist

|Itemcode|Description|Model| 
| I1 | Item1 | M1 | 
| I2 | Item2 | M2 | 
| I3 | Item3 | M3 | 

tbl_Conditions

|Itemcode| Condition| Year | 
| I1 | 1 | 2014 | 
| I2 | 2 | 2014 | 
| I3 | 2 | 2014 | 
| I1 | 3 | 2015 | 
| I2 | 2 | 2015 | 
| I3 | 2 | 2015 | 
| I1 | 3 | 2016 | 
| I2 | 1 | 2016 | 
| I3 | 3 | 2016 | 

dies ist die erwartete Ausgabe.

| Itemcode | Description | Model | 2014 | 2015 | 2016 | 
|  I1  |  Item1 | M1 | 1 |  3 | 3 | 
|  I2  |  Item2 | M2 | 2 |  2 | 1 | 
|  I3  |  Item3 | M3 | 2 |  2 | 3 | 

Ich habe Probleme auf Dividieren der Kolonne Jahr in 3 Spalten, mit dem Artikel Zustand bevölkert, Base auf das Jahr wählen (3 Jahre Bereich).

+1

Was ist das erwartete Verhalten, wenn es 2017 ist? Möchten Sie, dass eine andere Spalte automatisch im Ergebnis angezeigt wird? – jarlh

+0

Nein, Herr, nur die 3 Jahre Auswahl. zB: 2014-2016. – Redentoru

+1

Mögliche Duplikate von [Konvertieren von Zeilen in Spalten SQL Server] (http://stackoverflow.com/questions/33623321/convert-rows-into-columns-sql-server) – Moumit

Antwort

1
select * 
from 
(
select A.Itemcode, A.[Description], A.Mode, 
B.Condition, B.[Year] from tbl_Masterlist A join tbl_Conditions B ON A.Itemcode = B.Itemcode 
) src 
pivot 
(
    MAX(CONDITION) 
    for YEAR in ([2014], [2015], [2016]) 
) piv; 
+0

Vielen Dank Herr. – Redentoru

0
WITH empact AS 
    (SELECT Itemcode,a.Description,a.Model,b.Condition,b.Year 
FROM tbl_Masterlist a JOIN tbl_Conditions b ON a.Itemcode=b.Itemcode) 

SELECT Itemcode,Description,Model, [2005], [2006], [2007], [2008] 
FROM [dbo].[PivotExample] 
PIVOT 
(
     max(condtion) 
     FOR [Year] IN ([2005], [2006], [2007], [2008]) 
FROM empact) AS P  
2

Sie PIVOT für feste Spalten wie folgt verwendet werden:

SELECT * 
FROM 
(
    SELECT a.Itemcode, a.[Description], a.Model, b.[Year], b.Condition 
    FROM  tbl_Masterlist a 
    INNER JOIN tbl_Conditions b ON a.Itemcode = b.Itemcode 
) src 
PIVOT 
(
    MAX(Condition) FOR [[Year]] IN([2014], [2015], [2016]) 
) piv 
0
SELECT * 
FROM 
(
    SELECT a.Itemcode, a.[Description], a.Model, b.[Year], b.Condition 
    FROM  tbl_Masterlist a 
    INNER JOIN tbl_Conditions b ON a.Itemcode = b.Itemcode 
) src 
PIVOT 
(
    MAX(Condition) FOR [[Year]] IN([2014], [2015], [2016]) 
) piv 
0

Sie unter Standard-SQL-Perspektive verwenden können:

SELECT m.Itemcode,m.Description,m.Model 
     ,SUM(CASE WHEN c.Years='2014' THEN c.Conditions END) AS '2014' 
     ,SUM(CASE WHEN c.Years='2015' THEN c.Conditions END) AS '2015' 
     ,SUM(CASE WHEN c.Years='2016' THEN c.Conditions END) AS '2016' 
    FROM tbl_Masterlist AS m 
    LEFT JOIN tbl_Conditions AS c ON m.Itemcode = c.Itemcode 
    GROUP BY m.Itemcode,m.Description,m.Model 

Teile mein Testskript :

CREATE TABLE tbl_Masterlist(Itemcode varchar(50),Description varchar(50),Model varchar(50)); 

CREATE TABLE tbl_Conditions(Itemcode varchar(50),Conditions INT,Years varchar(20)); 

INSERT INTO tbl_Masterlist(Itemcode,Description,Model) 
VALUES('I1','Item1','M1') 
,('I2','Item2','M2') 
,('I3','Item3','M3') 

INSERT INTO tbl_Conditions(Itemcode,Conditions,Years) 
VALUES('I1',1,'2014') 
,('I2',2,'2014') 
,('I3',2,'2014') 
,('I1',3,'2015') 
,('I2',2,'2015') 
,('I3',2,'2015') 
,('I1',3,'2016') 
,('I2',1,'2016') 
,('I3',3,'2016') 

Ich hoffe, es könnte Ihnen helfen.