我有一个计算总价的查询,然后确定是否应该给出礼品卡。该表显示为:
SELECT
Customer.First_Name,
Customer.Last_Name,
Order.Order_ID,
Sum([Order Items].Quantity) AS [Total Items],
Sum(([Quantity]*[Price])) AS [Total Price],
IIf([Quantity]*[Price]>150,"Yes","No") AS [Gift Card]
FROM (Customer INNER JOIN [Order]
ON Customer.Customer_ID = Order.Customer_ID)
INNER JOIN (Item INNER JOIN [Order Items]
ON Item.Item_ID = [Order Items].Item_ID)
ON Order.Order_ID = [Order Items].Order_ID
GROUP BY
Customer.First_Name,
Customer.Last_Name,
Order.Order_ID,
IIf([Quantity]*[Price]>150,"Yes","No");
它显示了一个重复的字段,如何摆脱重复字段?
答案 0 :(得分:1)
尝试从分组中删除是/否标签。这将保证给定的名/姓和命令始终在结果集中显示为单个记录。
SELECT
Customer.First_Name,
Customer.Last_Name,
Order.Order_ID,
SUM([Order Items].Quantity) AS [Total Items],
SUM([Quantity]*[Price]) AS [Total Price],
IIF(SUM([Quantity]*[Price]) > 150, "Yes", "No") AS [Gift Card]
FROM (Customer INNER JOIN [Order]
ON Customer.Customer_ID = Order.Customer_ID)
INNER JOIN (Item INNER JOIN [Order Items]
ON Item.Item_ID = [Order Items].Item_ID)
ON Order.Order_ID = [Order Items].Order_ID
GROUP BY
Customer.First_Name,
Customer.Last_Name,
Order.Order_ID;
答案 1 :(得分:0)
根据数据模型,group by
和customerid
可以orderid
select c.customer_id, oi.order_id, SUM(oi.Qty) as TotalQty, SUM(oi.Qty*i.Price) as TotalPrice,
IIF(SUM(oi.Qty*i.Price) > 150, 'Yes', 'No') as GiftApplicable
from customer c
inner join order o in o.customer_id = c.customer_id
inner join order_item oi on oi.order_id = o.order_id
inner join item i on i.item_id = oi.item_id
group by c.customer_id, oi.order_id