我如何在SQL语句中订购?

时间:2010-01-01 07:25:05

标签: sql-server select

我有这些表:股票,单位,位置,类别,股票平衡

在StockBalance:StockIn来自Stock,UnitId来自Unit,LocationID来自Location

我保存在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

我想在Classic ASP上显示为:

分组依据:CategoryName

  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中选择如上所示?我的编辑怎么样?

请帮帮我!

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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