这是示例输出
让我解释一下发生了什么:
该查询返回每年的所有发票编号以及产品
涉及发票。
如您所见,2010年有两张发票...发票分别为30463和30516。
发票30463有4个产品,其运输价格为105.88。正如你看到的
每种产品的运费都重复,这会在
我在报告级别计算总和。发票#30463的4产品有
整体运费为105.00。我想要每张发票的每张运输价格
无论发票中有多少种产品,都只能显示一次。我该如何实现?
查询如下:
SELECT
DATEPART(year, CustomerInvDetail.sentDate) AS "Year",
CustomerInvoice.cuInvoiceID,
Product.productName,
CustomerQuoteProducts.unitPrice,
CustomerQuoteProducts.qty,
CustomerQuoteProducts.qty * CustomerQuoteProducts.unitPrice AS "Price",
CustomerShipping.shippingPrice
FROM CustomerInvoice INNER JOIN CustomerInvDetail
ON CustomerInvoice.cuInvoiceID = CustomerInvDetail.cuInvoiceID
INNER JOIN CustomerQuote
ON CustomerQuote.customerQuoteID = CustomerInvoice.customerQuoteID
INNER JOIN CustomerQuoteProducts
ON CustomerQuoteProducts.customerQuoteID = CustomerQuote.customerQuoteID
INNER JOIN CustomerShipping
ON CustomerShipping.customerQuoteID = CustomerInvoice.customerQuoteID
INNER JOIN Customer
ON Customer.customerID = CustomerQuote.customerID
INNER JOIN Product
ON CustomerQuoteProducts.productID = Product.productID
WHERE (DATEPART(year, CustomerInvDetail.sentDate) BETWEEN 2001 AND 2022) AND (Customer.customerID = 500)
最佳答案
遵循这些思路吧?
case when row_number() over(partition by cuInvoiceId order by newid()) = 1 then shippingPrice end
更新资料
它是做什么的:
它将根据
cuInvoiceId
值将数据划分为多个分区现在,在该分区内,我们想枚举每一行,但是没有任何锚定,因此我使用了
newid()
,这基本上意味着随机枚举那些行。最后,对于
case ... = 1
,我希望第一行是显示shippingPrice
以及所有其他行的null
。