0

我有一个正在工作的查询。但是,它仅按公司名称排序,而不是按这些公司的内部发票日期排序:

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 中分组。

无论如何,有一份公司名单。每家公司都有多个销售(发票)。我想列出一段时间内没有销售的公司。我已经实现了,但有一个小问题。我不能按时间订购它们。我理解这里的错误,因为有多张发票,公司每次都重复,不能按时间订购。但是,如果订购了它们,则会在发票日期之前完成,因此会重复公司。但我只想看到最后销售日期时间的公司列表。每次有销售都不会重复。希望我很清楚:)

感谢您的帮助!

4

1 回答 1

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

现在它是按时间排序的,不会重复公司:)

于 2013-12-13T11:40:25.320 回答