2016-04-06 3 views
0

Wie gruppiert man Datensätze und dreht sie? SQL-Abfrage die folgenden Datensätze abzurufen ist wie folgt:Wie werden die Tabellensätze geschwenkt?

select d.Name as DrawerBox 
    , dt.Name as DrawerBoxType 
    , di.Inches as Inches 
    , isnull(dbc.Value, dc.Value) Value  
from DrawerBox_C dc 
join DrawerBox d 
    on d.DrawerBoxId = dc.DrawerBoxId 
join DrawerBoxInches di 
    on di.DrawerBoxInchesId = dc.DrawerBoxInchesId 
join DrawerBoxType dt 
    on dt.DrawerBoxTypeId = dc.DrawerBoxTypeId 
left join dbo.DrawerBox_Bid_C dbc 
    on dbc.DrawerBox_C_Id = dc.DrawerBox_C_Id 
    and dbc.BidId = 1 

enter image description here

wollen Ausgabe als dies unter Format

enter image description here

Vielen Dank im Voraus

+0

Wie viele Zoll Optionen haben Sie? – sagi

+0

Es gibt 5 Zoll.12 ", 18", 24 ", 30", 36 " –

+1

Machen Sie GROUP BY, verwenden Sie die Ausdrücke für jede Zolloption. – jarlh

Antwort

1

give it a shot so ... die Abfrage wird nicht getestet, wenn sie fehlschlägt, lass es mich wissen, damit ich es beheben kann ... aber das ' s was Sie wollen, brauchen Sie nur Pivot

select * from (
select d.Name as DrawerBox 
, dt.Name as DrawerBoxType 
, di.Inches as Inches 
, isnull(dbc.Value, dc.Value) Value  
from DrawerBox_C dc 
    join DrawerBox d 
     on d.DrawerBoxId = dc.DrawerBoxId 
    join DrawerBoxInches di 
     on di.DrawerBoxInchesId = dc.DrawerBoxInchesId 
    join DrawerBoxType dt 
     on dt.DrawerBoxTypeId = dc.DrawerBoxTypeId 
    left join dbo.DrawerBox_Bid_C dbc 
     on dbc.DrawerBox_C_Id = dc.DrawerBox_C_Id 
     and dbc.BidId = 1 
) src 
    PIVOT 
    (
    Max(value) 
    for Inches in ([12], [18], [24], [30], [36]) 
) piv 
order by DrawerBox, DrawerBoxType 
-1

Sie können Folgendes verwenden, um Ihre Abfrage dynamisch auszuführen. Dies bedeutet, dass selbst wenn Sie neue "Inches" -Werte hinzufügen, diese automatisch für Sie erstellt werden. Im Folgenden finden Sie einen manuellen Einsatz, damit Sie testen können. Beachten Sie auch, dass dies den maximalen "Wert" für jede DrawerBox/DrawerBoxType darstellt. Sie können auch "Avg", "Min", "Sum" verwenden, wenn Sie möchten.

Drop Table #PivotRecs 
Create Table #PivotRecs (DrawerBox nvarchar(200), DrawerBoxType nvarchar(200), Inches int, Value int) 
Insert Into #PivotRecs 
select d.Name as DrawerBox 
    , dt.Name as DrawerBoxType 
    , di.Inches as Inches 
    , isnull(dbc.Value, dc.Value) Value  
from DrawerBox_C dc 
join DrawerBox d 
    on d.DrawerBoxId = dc.DrawerBoxId 
join DrawerBoxInches di 
    on di.DrawerBoxInchesId = dc.DrawerBoxInchesId 
join DrawerBoxType dt 
    on dt.DrawerBoxTypeId = dc.DrawerBoxTypeId 
left join dbo.DrawerBox_Bid_C dbc 
    on dbc.DrawerBox_C_Id = dc.DrawerBox_C_Id 
    and dbc.BidId = 1 


    --Insert Into #PivotRecs 
    --Select 'Melamine', 'Standard', 12, 3 Union 
    --Select 'Melamine', 'Standard', 18, 6 Union 
    --Select 'Melamine', 'Standard', 24, 9 Union 
    --Select 'Melamine', 'Standard', 30, 12 Union 
    --Select 'Melamine', 'Standard', 36, 15 Union 
    --Select 'Melamine', 'File', 12, 5 Union 
    --Select 'Melamine', 'File', 18, 8 Union 
    --Select 'Melamine', 'File', 24, 11 Union 
    --Select 'Melamine', 'File', 30, 14 Union 
    --Select 'Melamine', 'File', 36, 17 


Declare @Inch int, @SQL nvarchar(max),@Inchs nvarchar(max),@SQL2 nvarchar(max), @i int 
Set @i = 0 
Set @SQL = '' 
Set @SQL2 = '' 
Set @Inchs = '' 
DECLARE iCursor CURSOR 
LOCAL 
FAST_FORWARD 
FOR 
Select Distinct Inches From #PivotRecs Order By Inches 
    OPEN iCursor 
FETCH NEXT FROM iCursor INTO @Inch 
WHILE (@@fetch_status <> -1) 
BEGIN 
If(@i=1) 
Begin 
Set @SQL = @SQL + ',['+Cast(@Inch as nvarchar(4))+']' 
Set @Inchs= @Inchs + ','+Cast(@Inch as nvarchar(4))+'' 
End 
If(@i=0) 
Begin 
Set @SQL = @SQL + '['+Cast(@Inch as nvarchar(4))+']' 
Set @Inchs = @Inchs + ''+Cast(@Inch as nvarchar(4))+'' 
Set @i = 1 
End 

FETCH NEXT FROM iCursor INTO @Inch 
Continue 
End 
Set @SQL2 = 
'Select * 
From 
(Select Distinct DrawerBox, DrawerBoxType, Inches, Value From #PivotRecs) a 
pivot (
    max (a.Value) 
    for Inches in ('[email protected]+')) 
    as Inches 
    Order By 1' 

Exec SP_ExecuteSQL @SQL2 
Verwandte Themen