简体   繁体   English

SQL过滤表中的数据

[英]SQL filtering data in table

I have table with sales documents and invoices eg 我的桌子上有销售文件和发票,例如

SalesDoc | Invoice | InvoiceType
------------------------------------
Doc1     | Inv1    | A
Doc1     | Inv2    | B
Doc1     | Inv3    | C
Doc2     | Inv1    | A
Doc2     | Inv2    | C
Doc3     | Inv1    | A
Doc3     | Inv2    | B

Each Sales document can have more than one invoice eg standard invoice or proforma.. 每个销售单据可以具有多个发票,例如标准发票或形式。

I need to filter that table to receive below table 我需要过滤该表才能接收下面的表

So If for Sales doc exist incoice C then show only this row for that sales doc, if not then check for type B and then for type A. 因此,如果存在销售凭证,则发票C会仅显示该销售凭证的这一行,如果不存在,则检查类型B,然后检查类型A。

SalesDoc | Invoice | InvoiceType
-----------------------------------
Doc1     | Inv3    | C
Doc2     | Inv2    | C
Doc3     | Inv2    | B

I know that I can store sales docs in temp table and then loop (using cursor) and filter source table to delete unnecessary records But I want to be sure if is any other solution than loop to achieve it 我知道我可以将销售文档存储在临时表中,然后循环(使用游标)和过滤器源表以删除不必要的记录,但是我想确定是否有其他解决方案而不是循环来实现它

Thanks Tomek 谢谢托梅克

Depending on the greater logic, you could do this with aggregates: 根据更大的逻辑,可以使用聚合来执行此操作:

Select  SalesDoc, Max(Invoice) as Invoice, Max(InvoiceType) as InvoiceType
FROM MYSALESTABLE
GROUP BY SalesDoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM