2010-01-01 71 views
0

我有這些表:股票,單位,地點,類別,StockBalance如何在SQL語句中訂購?

在StockBalance:有StockID從股票,單元ID從單位,LocationID從位置

我在StockBalance表保存像下面

StockBalanceID | StockID | UnitID | LocationID | BalanceQuantity 
1    | 1  | 1  | 1   | 20  
2    | 1  | 2  | 1   | 30  
3    | 1  | 3  | 1   | 40  
4    | 2  | 1  | 2   | 20  
5    | 2  | 2  | 2   | 30  
6    | 2  | 3  | 2   | 40 

我想顯示在傳統的ASP爲:

組方:類別名稱

Stock Name     Quantity        Location Name  
         Qty | Unit | Qty | Unit | Qty | Unit   
    Stock One |   20 | One | 30 | Two | 40 | Three  | Location One 
    Stock Two |   20 | One | 30 | Two | 40 | Three  | Location Two 

我該如何從StockBalance中選擇以獲得上述結果?我的編輯怎麼樣?

請幫幫我!

+0

感謝羅布·法利! – RedsDevils 2010-01-01 07:34:36

+0

是的,我離開我的辦公室時光,這就是爲什麼我無法測試答案,這就是爲什麼我沒有選擇。謝謝你的提醒。我改變了我的顯示格式,但我的新格式沒有答案。 :) – RedsDevils 2010-01-06 03:29:50

回答

1

PIVOT是你想要的。

首先是一些樣本數據設置:

create table yourTable (
StockBalanceID int, StockID int, UnitID int, LocationID int, BalanceQuantity int); 

insert yourTable 
select 1    , 1  , 1  , 1   , 20  
union all select 2    , 1  , 2  , 1   , 30  
union all select 3    , 1  , 3  , 1   , 40  
union all select 4    , 2  , 1  , 2   , 20  
union all select 5    , 2  , 2  , 2   , 30  
union all select 6    , 2  , 3  , 2   , 40 
; 

現在做的工作......

select StockID, LocationID, 
    sum(case UnitID when 1 then BalanceQuantity end) as [Unit One], 
    sum(case UnitID when 2 then BalanceQuantity end) as [Unit Two], 
    sum(case UnitID when 3 then BalanceQuantity end) as [Unit Three] 
from yourTable 
group by StockID, LocationID; 
+0

非常感謝它適用於我的問題! – RedsDevils 2010-01-01 08:28:48

1

請嘗試以下

DECLARE @StockBalances TABLE(
     StockBalanceID INT, 
     StockID INT, 
     UnitID INT, 
     LocationID INT, 
     BalanceQuantity FLOAT 
) 

DECLARE @Stock TABLE(
     StockID INT, 
     StockName VARCHAR(10) 
) 

DECLARE @Unit TABLE(
     UnitID INT, 
     UnitName VARCHAR(10) 
) 

DECLARE @Location TABLE(
     LocationID INT, 
     LocationName VARCHAR(10) 
) 

INSERT INTO @StockBalances SELECT 1,1,1,1,20  
INSERT INTO @StockBalances SELECT 2,1,2,1,30  
INSERT INTO @StockBalances SELECT 3,1,3,1,40  
INSERT INTO @StockBalances SELECT 4,2,1,2,20  
INSERT INTO @StockBalances SELECT 5,2,2,2 ,30  
INSERT INTO @StockBalances SELECT 6,2,3,2,40 

INSERT INTO @Stock SELECT 1, 'Stock 1' 
INSERT INTO @Stock SELECT 2, 'Stock 2' 

INSERT INTO @Unit SELECT 1, 'Unit 1' 
INSERT INTO @Unit SELECT 2, 'Unit 2' 
INSERT INTO @Unit SELECT 3, 'Unit 3' 

INSERT INTO @Location SELECT 1, 'Location 1' 
INSERT INTO @Location SELECT 2, 'Location 2' 


SELECT * 
FROM (
      SELECT s.StockName, 
        sb.BalanceQuantity, 
        u.UnitName, 
        l.LocationName 
      FROM @StockBalances sb INNER JOIN 
        @Stock s ON sb.StockID = s.StockID INNER JOIN 
        @Unit u ON sb.UnitID = u.UnitID INNER JOIN 
        @Location l ON sb.LocationID = l.LocationID 
     ) t 
PIVOT (SUM(BalanceQuantity) FOR UnitName IN ([Unit 1], [Unit 2], [Unit 3])) p 
+0

謝謝你的答案! – RedsDevils 2010-01-01 08:29:23