我有一个正在运行的查询。但是,它仅按公司名称排序,而不是按这些公司的内部发票日期排序:
SELECT C.FULLNAME,C.COMPANY_ID,I.INVOICE_DATE FROM COMPANY C
JOIN #dsn2_alias#.INVOICE I ON I.COMPANY_ID = C.COMPANY_ID
WHERE I.INVOICE_ID IS NOT NULL AND I.INVOICE_DATE <= #attributes.date#
AND C.COMPANY_ID NOT IN (
SELECT C.COMPANY_ID FROM COMPANY C JOIN #dsn2_alias#.INVOICE I ON I.COMPANY_ID = C.COMPANY_ID WHERE I.INVOICE_ID IS NOT NULL AND I.INVOICE_DATE >= #attributes.date#
)
GROUP BY C.COMPANY_ID,C.FULLNAME,I.INVOICE_DATE ORDER BY C.FULLNAME
并输出:
<cfoutput query="get_companies" group="company_id">
<tr height="20" onMouseOver="this.className='color-light';" onMouseOut="this.className='color-row';" class="color-row">
<td style="text-align:center;">#row#</td>
<td style="text-align:center;">#dateformat(INVOICE_DATE,'dd/mm/yyyy')#</td>
<td>#fullname#</td>
</tr>
<cfset row++/>
</cfoutput>
实际上group by
子句在查询中不起作用。它在cfoutput中分组。
无论如何,有一个公司名单。每家公司都有多个销售(发票)。我想列出在一段时间内没有销售的公司。我已经实现了它,但有一点问题。我不能按时间排序。我理解这里的错误,因为有多张发票,公司每次都重复,不能按时间排序。但是,如果订购它们,则按发票日期完成,从而重复公司。但我希望看到的是具有最后销售日期时间的公司列表。每次销售都不会重复。希望我很清楚:)
感谢您的帮助!
答案 0 :(得分:0)
好的,我自己解决了草莓关于GROUPWISE-MAXIMUM的提示:
SELECT C.FULLNAME,C.COMPANY_ID,I.INVOICE_DATE
FROM COMPANY C INNER JOIN
(
SELECT I.COMPANY_ID,MAX(I.INVOICE_DATE) AS INVOICE_DATE
FROM #dsn2_alias#.INVOICE I GROUP BY I.COMPANY_ID
)
I ON I.COMPANY_ID = C.COMPANY_ID
WHERE I.INVOICE_DATE <= #attributes.date#
AND C.COMPANY_ID NOT IN
(
SELECT C.COMPANY_ID
FROM COMPANY C JOIN #dsn2_alias#.INVOICE I ON I.COMPANY_ID = C.COMPANY_ID
WHERE I.INVOICE_ID IS NOT NULL
AND I.INVOICE_DATE >= #attributes.date#
)
GROUP BY C.COMPANY_ID,C.FULLNAME,I.INVOICE_DATE ORDER BY I.INVOICE_DATE
现在它按时间排序,不会重复公司:)