【问题标题】:Getting the daily sales report given the date获取给定日期的每日销售报告
【发布时间】:2014-11-25 10:44:54
【问题描述】:

给定一个日期,表格会显示在该日期售出的商品。

该表对商品的类别进行了分组,并显示了每个类别的总销售额。最后,报告显示当天的总销售额。像这样的:

ID    Category  Price   Units   Total Value
----------------------------------------------------
2244    class   10.50    10       105.00   
2555    class   5.00      5       25.00   
3455    class   20.00     1       20.00   

  Total                  16       150.00    

1255    pop     20.00     5       100.00
5666    pop     10.00    10       100.00

  Total                  15       200,00

1244    rock    2.50     20       50.00   
8844    rock    5.00     50       250.00   

  Total                  70       300.00

----------------------------------------------

Total Daily     Sales   101       650.00

数据库管理系统:SQL Server 2012

粗体:主键

商品(upc、标题*、类型、类别、公司、年份、价格、库存)
PurchaseItem(receiptIdupc、数量)
订单(receiptId、日期、cid、card#、expiryDate、expectedDate、deliveredDate)

Rough work of what I have so far..

SELECT I.upc, I.category, I.price, P.quantity, P.quantity*I.price AS totalValue, SUM(totalValue), SUM(P.quantity) AS totalUnits, O.date
FROM Item I, Order O
JOIN (SELECT P.quantity
      FROM PurchaseItem P, Item I
      WHERE I.upc = P.upc)
ON I.upc = P.upc
WHERE O.date = ({$date}) AND O.receiptId = P.receiptId
GROUP BY I.upc, I.category, I.price, P.quantity, totalValue, O.date

好吧,这是不对的,我有点卡住了。需要帮助!
我想要它,所以它会产生一个类别的项目的总价值,然后最后,它将所有类别的项目的总价值相加。

示例表
项目(2568,披头士,摇滚,音乐公司,1998,50.50,5000)
购买项目 (5300, 2568, 2)
Order (5300, 10/09/2014, ...不重要..) cid 是 customerId,card# 是信用卡号。

【问题讨论】:

  • 能否提供每张表的样本数据?
  • Item(2568, Beatles, rock, Music Inc, 1998, 50.50, 5000) PurchaseItem (5300, 2568, 2) Order (5300, 10/09/2014, ...不重要.. ) cid 是 customerId,card# 是信用卡号。
  • 最好包含您在问题中使用的数据。此外,在包含示例数据时编辑问题。
  • 至少包含更多数据。如果我们只有一行,我们如何测试总和?
  • 我觉得你需要group by with rollup technet.microsoft.com/en-us/library/ms189305(v=sql.90).aspx

标签: sql sql-server tsql


【解决方案1】:

SSRS 或类似的报告包通常可以为您本地处理此问题。如果这必须在 SQl 脚本中完成,那么一个快速的解决方案将是光标/while 循环。伪代码看起来像这样;

create tempsales table;
get distinct list of categories;

for each category
Begin
  insert into tempsale (...)
  sales where category = @category 
  group by category, item

  insert into tempsales (...) -- use NULL value for item or perhaps a value of 'TOTAL'
  Sales where category = @category
  group by category

  when last category 
  insert into tempsales (...) -- use NULL value for item AND Category or perhaps a value of 'TOTAL'
  total with no group
end

 select from tempsales;

【讨论】:

    【解决方案2】:

    看看这是否有帮助:

    创建样本数据

    use tempdb;
    
    create table Item(
        upc         int,
        category    varchar(100),
        price       decimal(8,2)
    )
    create table PurchaseItem(
        receiptId   int,
        upc         int,
        quantity    int
    )
    create table [Order](
        receiptId   int,
        [date]      date
    )
    
    insert into Item values
    (2244, 'class', 10.50),
    (2555, 'class', 5.0),
    (3455, 'class', 20.0),
    (1255, 'pop', 20.0),
    (5666, 'pop', 10.0),
    (1244, 'rock', 2.50),
    (8844, 'rock', 5.0)
    
    insert into PurchaseItem values
    (5300, 2244, 10),
    (5300, 2555, 5),
    (5300, 3455, 1),
    (5300, 1255, 5),
    (5300, 5666, 10),
    (5300, 1244, 20),
    (5300, 8844, 50)
    
    insert into [Order] values(5300,'20140910')
    

    解决方案

    ;with cte as(
        select
            i.upc as Id,
            i.category as Category,
            i.price as Price,
            p.quantity as Units,
            price * quantity as TotalValue
        from [Order] o
        inner join PurchaseItem p
            on p.receiptId = o.receiptId
        inner join Item i
            on i.upc = p.upc
    )
    select
        Id,
        case
            when grouping(Id) = 1 then 'Total'
            else Category
        end as Category,
        Price,
        sum(Units) as Units,
        sum(TotalValue) as TotalValue
    from cte
    group by 
        grouping sets(Category, (Category, Id, Price, Units, TotalValue))
    union all
    select
        null,
        'Total Daily Sales',
        null,
        sum(Units),
        sum(Totalvalue)
    from cte
    

    删除样本数据

    drop table item
    drop table PurchaseItem
    drop table [Order]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多