我正在学习如何在SQL Server中使用Group By
,我正在尝试编写一个查询,让我可以在Tickets
之后的数字表中从inner joining
获取所有信息它与其他表格BY SELECTED DATE
。
我的表格如下:
第一张表格让我更详细地了解每张门票中售出的物品:
DATE | Ticket_ID | Items_Sold | Item_Name | Client_ID
2018-04-17 00:00:00 1 3 Watch 1
2018-04-17 00:00:00 1 2 Snapnack Hat 1
2018-04-17 00:00:00 2 1 Arizona Tea 1
2018-04-17 00:00:00 2 2 Wristband 1
2018-04-18 00:00:00 3 2 Pants 1
2018-04-18 00:00:00 3 2 Wristband 1
2018-04-18 00:00:00 4 1 Pants 1
2018-04-18 00:00:00 4 1 Wristband 1
第二张表格给出了每张门票内容和付费总额的基本描述。
DATE_Sale | Ticket_ID | Employee_ID | Total | Client_ID
2018-04-17 00:00:00 1 1 150 1
2018-04-17 00:00:00 2 1 60 1
2018-04-18 00:00:00 3 2 200 1
2018-04-18 00:00:00 4 2 100 1
所以,如果我想知道2018-04-17 00:00:00
和2018-04-18 00:00:00
之间发生了什么,请按Client_ID和DATE分组
我想要的结果如下:
DATE_Sale | Tickets | Total_Items_Sold | Total_Paid | Client_ID
2018-04-17 00:00:00 2 8 210 1
2018-04-18 00:00:00 2 4 300 1
问题在于,当我使用'内部联接'来获取销售的总项目时,所有总和都给了我错误的结果,因为它似乎是从重复的行添加,因为表1包含相同Ticket_ID和相同日期
我目前的查询如下:
SELECT B.DATE_Sale,
B.Client_ID, COUNT(B.Ticket_ID) as Tickets,
SUM(A.Items_Sold) as Total_Items_Sold,
SUM(B.Total_Paid) as Total_Paid
FROM Table_A as A
INNER JOIN Table_B as B
ON A.Client_ID = B.Client_ID
AND B.DATE_sales = A.DATE_Sales
Group by B.DATE_sales, A.Client_ID
答案 0 :(得分:1)
好吧,您可以使用subquery
与相关代替
select a.DATE as DATE_Sale, COUNT(DISTINCT a.Ticket_ID) as Tickets,
sum(a.Items_Sold) as Total_Items_Sold,
(select sum(Total) from Table_B
where DATE_Sale = a.DATE and
Client_ID = a.Client_ID) as Total_Paid, a.Client_ID
from Table_A as a
group by a.DATE, a.Client_ID;
另外,您可以使用subquery
并执行join
select a.DATE as DATE_Sale, COUNT(DISTINCT a.Ticket_ID) as Tickets,
sum(a.Items_Sold) as Total_Items_Sold,
b.Total_Paid, a.Client_ID
from Table_A as a
inner join (select DATE_Sale, Client_ID,
sum(Total) as Total_Paid
from Table_B
group by DATE_Sale, Client_ID) as b
ON b.DATE_Sale = a.DATE and
b.Client_ID = a.Client_ID
group by a.DATE, a.Client_ID;
答案 1 :(得分:0)
;with cte as
(select DATE_Sale,client_id, count(distinct ticket_id ) as tickets, sum(items_sold) as total_items_sold
from table_a group by client_id, DATE_Sale),
cte2 as
(select client_id,DATE_Sale, count(distinct ticket_id) as tickets, sum(total) as total_paid
from table_b group by client_id, DATE_Sale)
select a.DATE_Sale_is,a.tickets, a.total_items_sold, b.total_paid,a.client_id from cte a join cte2 b
on a.client_id = b.client_id
and a.DATE_Sale_is = b.DATE_Sale
答案 2 :(得分:0)
我会用两级聚合来做这件事,因为它捕获了数据应该如何填充。第一级是票证级别,第二级是您想要的汇总级别:
select t1.date, count(*) as num_tickets, sum(num_items) as num_items,
sum(t2.total_paid) as total_paid
from (select date, ticket_id, client_id, count(*) as num_items,
from table1
group by date, ticket_id, client_id
) t1 join
table2 t2
on t1.ticket_id = t2.ticket_id and
t2.date_sale = t1.date and t2.client_id = t1.client_id
group by t1.date;
COUNT(DISTINCT)
通常是避免多级聚合的捷径。在这种情况下,您确实需要聚合并使用大量子查询/ CTE来解决问题只会使查询更加复杂。