给定日期,该表格显示该日期售出的物品。
该表将项目的类别分组,并显示每个类别的总销售额。最后,报告显示当天的总销售额。像这样:
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
DBMS:SQL Server 2012
Bolded:主键
项目( upc ,标题*,类型,类别,公司,年份,价格,库存)
PurchaseItem( receiptId , upc ,数量)
订单( receiptId ,日期,cid,卡#,expiryDate,expectedDate,deliverDate)
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,Beatles,rock,Music Inc,1998,50.50,5000)
PurchaseItem(5300,2568,2)
订单(5300,10/09/2014,...不重要..)cid是customerId,card#是信用卡号。
答案 0 :(得分:0)
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;
答案 1 :(得分:0)
看看这是否有帮助:
创建示例数据
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 SAMPLE DATA
drop table item
drop table PurchaseItem
drop table [Order]