gpt4 book ai didi

sql - 如何去除列中的冗余值?

转载 作者:行者123 更新时间:2023-12-01 04:16:00 25 4
gpt4 key购买 nike

这是示例输出 alt text

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

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

如你所见,我们有两张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 将数据划分到分区上值(value)
  • 现在,在这个分区中,我们想要枚举每一行,但没有什么可以 anchor 定的,所以我使用了 newid()这基本上意味着随机枚举这些行。
  • 最后,还有 case ... = 1我希望第一行显示 shippingPrice和所有其他人 -- null .
  • 关于sql - 如何去除列中的冗余值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3788470/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com