用于显示产品销售的SQL查询

时间:2010-06-27 14:46:46

标签: sql sql-server sql-server-2005

我有销售表,包括ItemSize,GroupName,Quantity,ProductID等......

现在我想按照以下格式显示销售额

GroupName   ItemSize  Quantity   ItemSize  Quantity

装置

BEER    350ml   500    650ml   1000 

我如何在SQL SERVER 2005 EXPRESS(T-SQL)中实现这一目标?感谢

更新:

我的销售表结构

CREATE TABLE [dbo].[SalesLog](
[SalesID] [int] IDENTITY(1,1) NOT NULL,
[MemoNo] [int] NULL,
[ProductCode] [int] NULL,
[Quantity] [int] NULL,
[Price] [int] NULL,
[ProductGroup] [int] NULL,
    CONSTRAINT [PK_SalesLog] PRIMARY KEY CLUSTERED 
    (
        [SalesID] ASC
    ) WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

我的产品表结构

CREATE TABLE [dbo].[Products](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[pName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[pSize] [int] NULL,
[pPrice] [int] NULL,
[pPackQty] [int] NULL,
[pGroup] [int] NULL,
[pCode] [int] NULL,
[pStock] [int] NULL,
[pYrStock] [int] NULL,
[pClearStock] [int] NULL,
    CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
   (
   [ProductId] ASC
   ) WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

2 个答案:

答案 0 :(得分:3)

您可以使用SUM和CASE语句聚合数据。

使用您的表定义(以及一些非常简单的组成数据),以下是您可以执行此操作的示例:

--** Create test tables
DECLARE @SalesLog TABLE ( 
SalesID int IDENTITY(1,1) NOT NULL, 
MemoNo int NULL, 
ProductCode int NULL, 
Quantity int NULL, 
Price int NULL, 
ProductGroup int NULL)

DECLARE @Products TABLE( 
ProductId int IDENTITY(1,1) NOT NULL, 
pName nvarchar(50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, 
pSize int NULL, 
pPrice int NULL, 
pPackQty int NULL, 
pGroup int NULL, 
pCode int NULL, 
pStock int NULL, 
pYrStock int NULL, 
pClearStock int NULL)

--** Setup test data
INSERT INTO @SalesLog ( MemoNo, ProductCode, Quantity, Price, ProductGroup)
SELECT   0, 1, 500, 0, 1 UNION
SELECT   0, 2, 700, 0, 1 UNION   
SELECT   0, 2, 333, 0, 1 UNION   
SELECT   0, 3, 200, 0, 2 UNION
SELECT   0, 4, 125, 0, 2 ;

INSERT INTO @Products (pName, pSize, pPrice, pPackQty, pGroup, pCode, pStock, pYrStock, pClearStock)
SELECT 'Beer', 350, 1 , 1, 1, 1, 0, 0, 0 UNION
SELECT 'Beer', 650, 1 , 1, 1, 2, 0, 0, 0 UNION
SELECT 'Beer', 1000, 1 , 1, 1, 3, 0, 0, 0 UNION
SELECT 'Wine', 750, 1 , 1, 2, 4, 0, 0, 0 UNION 
SELECT 'Wine', 1000, 1 , 1, 2, 5, 0, 0, 0 ;

--** Example query
SELECT t.pName AS 'Product'
     , MAX(CASE WHEN t.Col = 1 THEN t.pSize END) AS 'Item Size'
     , ISNULL(SUM(CASE WHEN t.Col = 1 THEN t.Quantity END),0) AS 'Quantity'
     , MAX(CASE WHEN t.Col = 2 THEN t.pSize END) AS 'Item Size'
     , ISNULL(SUM(CASE WHEN t.Col = 2 THEN t.Quantity END),0) AS 'Quantity'
     , MAX(CASE WHEN t.Col = 3 THEN t.pSize END) AS 'Item Size'
     , ISNULL(SUM(CASE WHEN t.Col = 3 THEN t.Quantity END),0) AS 'Quantity'
FROM (
SELECT pName
     , pCode
     , pGroup
     , pSize
     , sl.Quantity
     , DENSE_RANK() OVER(PARTITION BY p.pGroup ORDER BY p.pSize) AS Col
  FROM @Products AS p
  LEFT JOIN @SalesLog AS sl
    ON p.pGroup = sl.ProductGroup
   AND p.pCode = sl.ProductCode
     ) AS t
 GROUP BY t.pGroup
     , t.pName
;   

该查询使用DENSE_RANK函数将一个大小的项目组合在一起,并按照大小的顺序对它们进行排序,这用于计算应该将数据写入哪一列。

虽然SQL Server 2005及更高版本中有一个PIVOT运算符,但如果您有不同的列标题类型(在这种情况下为项目大小和数量),则它不是很有用。

您必须决定要报告的最大产品尺寸数,因为它已硬编码到查询中。因此,如果产品大小的最大数量为3,那么您可以编码查询,如上所示。但是,如果您的某个产品有4种不同的尺寸,那么您将为t.Col = 4添加额外的项目大小和数量对列,依此类推。

我希望这会有所帮助。

答案 1 :(得分:1)

看起来您正在尝试执行PIVOT表,其中不同的项目大小表示为不同的列而不是行。看看http://msdn.microsoft.com/en-us/library/ms177410.aspx