2010-09-24 78 views
4

这是样本输出alt text如何摆脱列中的冗余值?

让我解释一下这是怎么回事:

查询返回的所有发票,每年的#与参与发票产品 一起。

如您所见,我们在2010年有两张发票...发票是30463和30516. 发票30463有4件商品,运费为105.88美元。如您所见, 当我在报告级别计算总和时,会在导致问题的每件产品上重复发货价格。发票#30463的4款产品的总运费价格为 。无论发票内有多少种商品,我都希望每张发票 的每个运费只显示一次。我怎样才能实现它?

下面是该查询:

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

那么,您的目标是让发票的105.00只出现一次,而shippingPrice中剩余的值显示为零? – Andrew 2010-09-24 15:21:19

+0

你想显示所有产品,但价格只有一次? 或者您只需要每张发票的价格和编号? – 2010-09-24 15:21:44

+0

因此,对于任何给定的发票号码,您希望运费仅在其中一行显示?只要它只出现一次,哪一排不重要?发票的所有其他运输价值将为零? – LittleBobbyTables 2010-09-24 15:22:09

回答

2

沿着也许这些东西线?

case when row_number() over(partition by cuInvoiceId order by newid()) = 1 then shippingPrice end 

更新

它的作用是这样的:

  1. 这将数据划分到取决于cuInvoiceId值分区现在
  2. ,这个分区里面,我们要列举每行,但没有什么可以锚定,所以我使用newid()这基本上意味着随机枚举这些行。
  3. 最后,与case ... = 1我想要第一行将显示shippingPrice和所有其他 - null
+0

太棒了!工作...你能简单地解释一下这条线是什么...我看到一些新的关键字,如分区...... – user311509 2010-09-24 20:30:59

0

怎么样时,它的第一个项目航运价格case语句?我假设你有一个订单项或某种方式来确定发票上的第一个项目 - 然后

情况下,当LINENO = 1,那么CustomerShipping.shippingPrice否则为0结束