GROUP BY
If you want to group by 5th column then the order by should be in the same line.
SELECT wo.WORKORDERID AS "Request ID",
wo.TITLE AS "Subject",
cd.CATEGORYNAME AS "Category",
std.STATUSNAME AS "Request Status",
ti.FIRST_NAME AS "Technician" FROM WorkOrder wo
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN CategoryDefinition cd ON wos.CATEGORYID=cd.CATEGORYID
LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID
LEFT JOIN SDUser td ON wos.OWNERID=td.USERID
LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID
WHERE (wo.ISPARENT='1') ORDER BY 5
If the ORDER BY is in the next line then it will sort by the 5th column.
SELECT wo.WORKORDERID AS "Request ID",
wo.TITLE AS "Subject",
cd.CATEGORYNAME AS "Category",
std.STATUSNAME AS "Request Status",
ti.FIRST_NAME AS "Technician"FROM WorkOrder wo
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN CategoryDefinition cd ON wos.CATEGORYID=cd.CATEGORYID
LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID
LEFT JOIN SDUser td ON wos.OWNERID=td.USERID
LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID
WHERE (wo.ISPARENT='1')
ORDER BY 5
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
SELECT wo.WORKORDERID "Request ID",
std.STATUSNAME "Request Status" FROM WorkOrder wo
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID
WHERE std.STATUSNAME = 'open'
ORDER BY 1 desc
UNION
The UNION operator is used to combine the result-set of two or more SELECT statements.
Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
Example : The below query will combine active and archived request.
SELECT wo.WORKORDERID "Request ID",
std.STATUSNAME "Request Status" FROM WorkOrder wo
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN StatusDefinition std ON wos.STATUSID=std.STATUSID
WHERE std.STATUSNAME = 'open'
UNION
SELECT arcwo.WORKORDERID AS "Request ID",
arcwo.STATUSNAME AS "Request Status" FROM Arc_WorkOrder arcwo
COUNT
To get the count of Request we use group by option
SELECT ti.FIRST_NAME AS "Technician",count(wo.workorderid) "Count" FROM WorkOrder wo
LEFT JOIN WorkOrderStates wos ON wo.WORKORDERID=wos.WORKORDERID
LEFT JOIN SDUser td ON wos.OWNERID=td.USERID
LEFT JOIN AaaUser ti ON td.USERID=ti.USER_ID
WHERE (wo.ISPARENT='1') Group BY ti.FIRST_NAME