GROUP BY条款基于条件

时间:2018-02-27 05:55:01

标签: mysql group-by

我想根据查询生成一个Excel报告。在那我有工作号和状态。我只想按工作号码的状态进行分组,然后按工作号码排序。

以下是报告的查询

SELECT DISTINCT
JobNumber.JOB_NO AS JobNumber,
CONCAT(ADDRESS.LOT,' ', ADDRESS.UNIT_NUMBER,' ', ADDRESS.STREET_NUMBER,' ',ADDRESS.STREET_NAME,' ', ADDRESS.SUBURB,' ', ADDRESS.STATE,' ', ADDRESS.POST_CODE) As JobAddress,
Builder.TITLE As Builder,
GROUP_CONCAT(DISTINCT(Type.Title) SEPARATOR ', ') AS EstimationType,
GROUP_CONCAT(DISTINCT(Mode.Title) SEPARATOR ', ') AS RequestMode,
Status.Title AS Status,
EstimationRequest.Notes,
EstimationRequestOrder.Notes As OrderNotes,
EstimationRequestQuote.Notes AS QuoteNotes,
EstimationRequestSale.Notes AS SaleNotes,
EstimationRequestMasterBOQ.Notes AS MasterNotes,
EstimationRequest.EstimatedTime,
EstimationRequest.ActualTime,
EstimationRequest.BillableTime,
EstimationRequest.AssignedToID,
CONCAT(AssignedTo.FIRST_NAME,' ',AssignedTo.LAST_NAME) AS AssignedTo,
Operator.USER_NAME As Operator

FROM
EstimationRequest

INNER JOIN EstimationType
        ON EstimationType.EstimationRequestID = EstimationRequest.ID AND EstimationType.Inactive = '0'

 LEFT OUTER JOIN Lookup Type
        ON Type.ID = EstimationType.TypeID AND Type.EntityType = 'ESTIMATIONTYPE'

INNER  JOIN RequestMode
        ON RequestMode.EstimationRequestID = EstimationRequest.ID AND RequestMode.Inactive = '0'

LEFT OUTER  JOIN Lookup Mode
        ON Mode.ID = RequestMode.ModeID AND Mode.EntityType = 'REQUESTMODE'


LEFT OUTER JOIN JOB JobNumber
        ON JobNumber.JOB_ID = EstimationRequest.JobID 

LEFT OUTER JOIN ADDRESS
    ON ADDRESS.ENTITY_ID = EstimationRequest.JobID AND ADDRESS.ENTITY_TYPE = 'JOB'

LEFT OUTER JOIN Lookup Status
        ON Status.ID = EstimationRequest.StatusID AND Status.EntityType = 'Status'

LEFT OUTER JOIN JOB 
        ON JOB.JOB_ID = EstimationRequest.JobID

LEFT OUTER JOIN COMPANY AS Builder
        ON Builder.COMPANY_ID = JOB.COMPANY_ID

 LEFT OUTER JOIN USER AS Operator
        ON Operator.USER_ID = EstimationRequest.OperatorID

LEFT OUTER JOIN EMPLOYEE AS AssignedTo
        ON AssignedTo.EMPLOYEE_ID = EstimationRequest.AssignedToID

LEFT OUTER JOIN  EstimationRequestOrder
        ON EstimationRequestOrder.EstimationRequestID = EstimationRequest.ID AND EstimationRequestOrder.Inactive = '0'

LEFT OUTER JOIN  EstimationRequestQuote
        ON EstimationRequestQuote.EstimationRequestID = EstimationRequest.ID AND EstimationRequestQuote.Inactive = '0'

LEFT OUTER JOIN  EstimationRequestSale
        ON EstimationRequestSale.EstimationRequestID = EstimationRequest.ID AND EstimationRequestSale.Inactive = '0'

LEFT OUTER JOIN  EstimationRequestMasterBOQ
        ON EstimationRequestOrder.EstimationRequestID = EstimationRequest.ID AND EstimationRequestMasterBOQ.Inactive = '0'

GROUP BY EstimationRequest.JobID ORDER BY JobNumber.JOB_NO

我的状态为New,In Progress,On Hold和Completed。当我给出状态的Group by子句时,它只返回四行,但我有11行具有不同的状态。我只需要按职务顺序排序 - 职位编号,按工作编号排序。

任何帮助将不胜感激

先谢谢。

0 个答案:

没有答案