需要有关SELECT DISTINCT查询的帮助

时间:2012-09-04 05:21:48

标签: sql firebird

我正在使用Firebird 2.1来执行此查询:

SELECT DISTINCT INVOICES.INVNO,INVOICES.DEPNO,INVOICES.INVN,INVOICES.CONTNO,SYSPROF.NAME
FROM INVOICES
LEFT JOIN INVSPEC ON (INVOICES.INVNO = INVSPEC.INVNO) AND (INVOICES.DEPNO = INVSPEC.DEPNO)
LEFT JOIN SYSPROF ON (INVSPEC.SYSNO = SYSPROF.SYSNO) WHERE
(INVOICES.INVNO = 17 AND INVOICES.DEPNO = 6555)
OR(INVOICES.INVNO = 16 AND INVOICES.DEPNO = 6555);

它会返回这些记录:

enter image description here

请帮我构建一个返回这些记录的查询:

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:0)

例如:

SELECT INVOICES.INVNO,min(INVOICES.DEPNO),min(INVOICES.INVN),min(INVOICES.CONTNO),min(SYSPROF.NAME)
FROM INVOICES
LEFT JOIN INVSPEC ON (INVOICES.INVNO = INVSPEC.INVNO) AND (INVOICES.DEPNO = INVSPEC.DEPNO)
LEFT JOIN SYSPROF ON (INVSPEC.SYSNO = SYSPROF.SYSNO) WHERE
(INVOICES.INVNO = 17 AND INVOICES.DEPNO = 6555)
OR(INVOICES.INVNO = 16 AND INVOICES.DEPNO = 6555);
GROUP BY INVOICES.INVNO

此查询每个发票返回一行,并从sysprof中获取任何名称。