如何在SQL Server 2012中生成特定查询

时间:2013-09-02 18:40:43

标签: sql sql-server database sql-server-2012

我需要生成一份报告,其中包含特定产品的销售数量。 (服装)

我有4张桌子:

  • Orders包含ID和日期
  • Product_Model包含模型说明和性别
  • Product_Type包含产品型号ID,价格,尺寸,颜色
  • Product_Order包含“订单”的引用和“Product_Type”
  • 的引用

我需要的是生成一份报告,显示所有月份的男性蓝色T恤衫的数量。

按此顺序:产品型号/性别/颜色

例如:

enter image description here

Also check my simple representation in SQLFiddle

非常感谢这种支持! =)

2 个答案:

答案 0 :(得分:3)

尝试这样的事情:

SELECT MODEL, 
       GENDER, 
       COLOR, 
       SUM(CASE 
             WHEN MONTH(ORDER_DATE) = 1 THEN 1 
             ELSE 0 
           END) Jan, 
       SUM(CASE 
             WHEN MONTH(ORDER_DATE) = 2 THEN 1 
             ELSE 0 
           END) Feb, 
       SUM(CASE 
             WHEN MONTH(ORDER_DATE) = 3 THEN 1 
             ELSE 0 
           END) Mar, 
       SUM(CASE 
             WHEN MONTH(ORDER_DATE) = 4 THEN 1 
             ELSE 0 
           END) Apr 
FROM   PRODUCT_MODELS t1 
       INNER JOIN PRODUCT_TYPES t2 
               ON t1.PRODUCT_MODEL_ID = t2.PRODUCT_MODEL_ID 
       INNER JOIN PRODUCT_ORDERS T3 
               ON t2.PRODUCT_TYPE_ID = t3.PRODUCT_TYPE_ID 
       INNER JOIN ORDERS T4 
               ON T3.ORDER_ID = T4.ORDER_ID 
GROUP  BY MODEL, 
          GENDER, 
          COLOR 

我用你的SQL小提琴做饭my own example

答案 1 :(得分:0)

其他解决方案可以根据您的要求使用SQL Pivot Table query

以下是我在您的表格上构建的数据透视查询

with OrderReport as (
select
--  o.Order_ID,
--  date,
    DATENAME(Month,date) months,
    Order_Quantity,
--  Price,
--  Size,
--  Color,
--  model_description,
--  gender,
    product = model_description + ' / ' + gender + ' / ' + Color
from Orders o
inner join Product_Order po on po.Order_ID = o.Order_ID
inner join Product_Type t on t.Product_Type_ID = po.Product_Type_ID
inner join Product_Model m on m.Product_Model_ID = t.Product_Model_ID
) SELECT * FROM OrderReport
PIVOT (
    SUM(Order_Quantity)
    FOR months IN (
        [January],[February],[March],[April],
        [May],[June],[July],[August],
        [September],[October],[November],[December]
    )
) PivotTable

这是DDL和一些用于测试数据透视查询的示例数据

create table Product_Model (Product_Model_ID int, model_description nvarchar(50),     gender nvarchar(20))
create table Product_Type (Product_Type_ID int, Product_Model_ID int, price int, size nvarchar(20), color nvarchar(20))
create table Orders (Order_ID int, date datetime)
create table Product_Order (Order_ID int, Product_Type_ID int, Order_Quantity int)

insert into Product_Model select 1, 'Basic T-Shirt','Male'
insert into Product_Model select 2, 'Basic T-Shirt','Female'
insert into Product_Model select 3, 'Polo','Male'
insert into Product_Model select 4, 'Polo','Female'
insert into Product_Model select 5, 'Jacket','Male'
insert into Product_Model select 6, 'Jacket','Female'
insert into Product_Model select 7, 'Long T-Shirt','Female'

insert into Product_Type select 1,1,1,'1','Red'
insert into Product_Type select 2,1,1,'1','Blue'
insert into Product_Type select 3,2,1,'1','Cyano'
insert into Product_Type select 4,2,1,'1','Pink'
insert into Product_Type select 5,3,2,'1','Grey'
insert into Product_Type select 6,3,2,'1','Black'
insert into Product_Type select 7,4,2,'1','Blue'
insert into Product_Type select 8,4,2,'1','Red'

insert into Orders select 1,'20130131'
insert into Orders select 2,'20130110'
insert into Orders select 3,'20130216'
insert into Orders select 4,'20130220'
insert into Orders select 5,'20130311'
insert into Orders select 6,'20130311'

insert into Product_Order select 1,1,3
insert into Product_Order select 2,1,5
insert into Product_Order select 3,2,7
insert into Product_Order select 4,1,4
insert into Product_Order select 5,1,1
insert into Product_Order select 6,3,8

我希望这有帮助,