有没有一种方法可以仅显示第一个非零值,但是如果所有值为零,则在单独的表中显示一个列的第一个实例
这两个表非常大,我的选择语句具有更多的值,但是我认为我可以简化这些数据和查询,因此可以回答我的问题。
这是我的选择语句:
SELECT invoice.InvoiceNum, Layer, InvoiceItemNum
FROM (INVOICE
left outer join InvoiceItem item
ON item.InvoiceNum = Invoice.InvoiceNum
)
ORDER BY invoice.InvoiceNum
所以我有两个表。发票表和一张开票表。每个表中的invoiceNum列连接它们,并且
在此查询中显示invoiceNum和layer列的结果:
InvoiceNum | Layer | InvoiceItemNum
1 | 10 | 1
1 | 0 | 2
1 | 7 | 3
1 | 0 | 4
2 | 0 | 1
2 | 3 | 2
3 | 0 | 1
3 | 0 | 2
3 | 0 | 3
4 | 0 | 1
4 | 0 | 2
4 | 5 | 3
由于我的invoiceItem表有多个行,可以分配给1个invoiceNum,这使我在我的我中具有重复的invoiceNums。结果,我不想要。
这是我试图获得的结果,仅在发票表中列出1个发票,其中第一种从InvoIceItem Taber的层列中的非零值的情况,以及如果没有非零的零,则列出了第一个零。
尝试这样的事情:
InvoiceNum | Layer | InvoiceItemNum
1 | 10 | 1
2 | 3 | 2
3 | 0 | 1
4 | 5 | 3
我只是不确定该如何做到这一点,或者如果可以的话,鉴于它们在两个不同的表上。
These two tables are quite large, and my select statement has more values I am obtaining, but I think I can simplify this data and query so my question can be answered.
Here is my select statement:
SELECT invoice.InvoiceNum, Layer, InvoiceItemNum
FROM (INVOICE
left outer join InvoiceItem item
ON item.InvoiceNum = Invoice.InvoiceNum
)
ORDER BY invoice.InvoiceNum
So I have two tables. An Invoice table and an InvoiceItem table. They are joined by the InvoiceNum column in each table, and displaying the InvoiceNum and Layer Column
Here is a result to this query:
InvoiceNum | Layer | InvoiceItemNum
1 | 10 | 1
1 | 0 | 2
1 | 7 | 3
1 | 0 | 4
2 | 0 | 1
2 | 3 | 2
3 | 0 | 1
3 | 0 | 2
3 | 0 | 3
4 | 0 | 1
4 | 0 | 2
4 | 5 | 3
Since my InvoiceItem table has multiple rows that can be assigned to 1 InvoiceNum this is causing me to have duplicate invoiceNums in my result, which I do not want.
Here is the result I am trying to get, only listing 1 invoiceNum from the Invoice table, with the first case of a non-zero value from the InvoiceItem table's layer column, and If there is no non-zero than list the first zero.
Trying for something like this:
InvoiceNum | Layer | InvoiceItemNum
1 | 10 | 1
2 | 3 | 2
3 | 0 | 1
4 | 5 | 3
I am just not sure how to do this or if this is possible given that these are on two different tables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题有点棘手:
在postgres中尝试一下:
https://dbfiddle.uk/?rdbms = postgres_11& amp; fiddle = e16faa9ed8f9b9c8e08888888bc3c468229
尝试以下操作:
this question is a bit tricky:
Try this in Postgres:
DEMO
In MySQL 8:
Try This:
假设/理解:
Sybase
标记该问题并未区分4x不同的Sybase RDBMS产品(ase> ase
sqlanywhere iq iq ,优势
),因此我要坚持应该是常规SQL语法(即4X产品具有不同的SQL方言;此外,ASE也不支持CTE),左(outer)Join
由于提供的输出似乎没有表示InvoIteEtem
的“缺失”行,layer
和invoiceItemnum
列属于,因此我将假设它们属于InvoiceItem
在一组最小表定义和关联的
insert
猜测 /code>语句:生成OP的当前输出的查询:
生成OP的所需输出的几个不同(令人费解的,混乱,混乱)的想法:
这两个生成:
注释:
左(外)JOIN
(ASE 16.0
在(Sybase)SAP 中测试的所有查询Assumptions/Understandings:
sybase
the question does not distinguish between the 4x different Sybase RDBMS products (ASE
,SQLAnywhere
,IQ
,Advantage
) so I'm going to stick with what should be general SQL syntax (ie, the 4x products have different SQL dialects; also, ASE does not support CTEs)left (outer) join
since the provided output doesn't appear to indicate any 'missing' rows fromInvoiceItem
Layer
andInvoiceItemNum
columns belong to so I'm going to assume they belong toInvoiceItem
Guessing at a set of minimum table definitions and associated
insert
statements:Query that generates OP's current output:
A couple different (convolued, messy) ideas for generating OP's desired output:
Both of these generate:
NOTES:
left (outer) join
ASE 16.0
您是否尝试过ASC和DESC?
have you tried ASC and DESC?