这是示例输出
让我解释一下发生了什么:
查询会返回每年的所有发票#以及产品 发票中涉及的内容。
如您所见,我们在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)
答案 0 :(得分:2)
沿着这些方向可能有什么?
case when row_number() over(partition by cuInvoiceId order by newid()) = 1 then shippingPrice end
<强>更新强>
它的作用是:
cuInvoiceId
值newid()
,这基本上意味着随机枚举这些行。 case ... = 1
,我希望第一行显示shippingPrice
和其他所有 - null
。 答案 1 :(得分:0)
第一项时的运费价格的个案陈述怎么样?我假设您有一个lineitem或某种方式确定发票上的第一项 - 然后
当lineno = 1时的情况,然后是CustomerShipping.shippingPrice else 0结束