sql – 如何从列中清除冗余值?

这是示例输出

让我解释一下发生了什么:

查询返回每年的所有发票#以及产品
发票中涉及的内容.

如您所见,我们在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.

点赞