今年夏天,我一直在为一家公司做一个项目,即使是即将毕业的学生,我仍然非常努力,我一直遇到处理这个小组的问题,并且通过条款订购。
我尝试的任何东西似乎都没有用,到目前为止我所拥有的(语法是suuuuper搞砸了分组/顺序)
SELECT DISTINCT ACT.ROW_ID , ACT.CREATED , MEM.FIRST_NAME , MEM.LAST_NAME , LOC.NAME , COUNT(*) as "number" , CAT.NAME , SER.NAME , EMP.FIRST_NAME , EMP.LAST_NAME , SER.DURATION , ACT.CASH , COS.NAME , ACT.COMMENTS , ACT.TIP FROM
S_ACTIVITY ACT, S_LOCATION LOC, S_CATEGORY CAT, S_EMPLOYEE EMP, S_SERVICE SER, S_COST_CODE COS, S_MEMBER MEM
WHERE ACT.EMPLOYEE_ID = EMP.ROW_ID AND ACT.SERVICE_ID = SER.ROW_ID AND ACT.CATEGORY_ID = CAT.ROW_ID AND ACT.COST_CODE_ID = COS.ROW_ID AND
ACT.LOCATION_ID = LOC.ROW_ID AND ACT.MEMBER_ID = MEM.ROW_ID
GROUP BY LOC.NAME
ORDER BY number
这就像在线的其他指南一样,我说我需要某种排序或数量来订购......继承人原来...
SELECT DISTINCT ACT.ROW_ID , ACT.CREATED , MEM.FIRST_NAME , MEM.LAST_NAME , LOC.NAME , CAT.NAME , SER.NAME , EMP.FIRST_NAME , EMP.LAST_NAME , SER.DURATION , ACT.CASH , COS.NAME , ACT.COMMENTS , ACT.TIP FROM
S_ACTIVITY ACT, S_LOCATION LOC, S_CATEGORY CAT, S_EMPLOYEE EMP, S_SERVICE SER, S_COST_CODE COS, S_MEMBER MEM
WHERE ACT.EMPLOYEE_ID = EMP.ROW_ID AND ACT.SERVICE_ID = SER.ROW_ID AND ACT.CATEGORY_ID = CAT.ROW_ID AND ACT.COST_CODE_ID = COS.ROW_ID AND
ACT.LOCATION_ID = LOC.ROW_ID AND ACT.MEMBER_ID = MEM.ROW_ID
GROUP BY LOC.NAME
ORDER BY LOC.NAME
感谢您的帮助,非常感谢! :)
答案 0 :(得分:0)
这是让你开始的事情,因为你是SO的新手。欢迎,BTW。
我真的会读到Gordon和Maciej的评论,以及关于JOINS的文章。祝你学习顺利。
这只是在语法上对MSSQL是正确的,对你的JOIN有一些假设。它不一定是您正在寻找的结果。
SELECT DISTINCT
ACT.ROW_ID,
ACT.CREATED,
MEM.FIRST_NAME,
MEM.LAST_NAME,
LOC.NAME,
COUNT(*) as "number",
CAT.NAME,
SER.NAME,
EMP.FIRST_NAME,
EMP.LAST_NAME,
SER.DURATION,
ACT.CASH,
COS.NAME,
ACT.COMMENTS,
ACT.TIP
FROM
S_ACTIVITY ACT
INNER JOIN S_LOCATION LOC on ACT.LOCATION_ID = LOC.ROW_ID
INNER JOIN LOC. S_CATEGORY CAT ON ACT.CATEGORY_ID = CAT.ROW_ID
INNER JOIN S_EMPLOYEE EMP ON ACT.EMPLOYEE_ID = EMP.ROW_ID
INNER JOIN S_SERVICE SER ON ACT.SERVICE_ID = SER.ROW_ID
INNER JOIN S_COST_CODE COS ON ACT.COST_CODE_ID = COS.ROW_ID
INNER JOIN S_MEMBER MEM ON ACT.MEMBER_ID = MEM.ROW_ID
GROUP BY
--every column that isn't an aggreggate... i.e. everything except COUNT(*).
ACT.ROW_ID,
ACT.CREATED,
MEM.FIRST_NAME,
MEM.LAST_NAME,
LOC.NAME,
CAT.NAME,
SER.NAME,
EMP.FIRST_NAME,
EMP.LAST_NAME,
SER.DURATION,
ACT.CASH,
COS.NAME,
ACT.COMMENTS,
ACT.TIP
ORDER BY
--You can't use alias names in GROUP BY or ORDER BY clauses.
COUNT(*)
答案 1 :(得分:0)
明确联接 - 使用它们,它们很棒。这是一个例子:
SELECT DISTINCT ACT.ROW_ID , ACT.CREATED , MEM.FIRST_NAME , MEM.LAST_NAME , LOC.NAME , CAT.NAME , SER.NAME , EMP.FIRST_NAME , EMP.LAST_NAME , SER.DURATION , ACT.CASH , COS.NAME , ACT.COMMENTS , ACT.TIP FROM
S_ACTIVITY ACT, S_LOCATION LOC, S_CATEGORY CAT, S_EMPLOYEE EMP, S_SERVICE SER, S_COST_CODE COS, S_MEMBER MEM
WHERE ACT.EMPLOYEE_ID = EMP.ROW_ID AND ACT.SERVICE_ID = SER.ROW_ID AND ACT.CATEGORY_ID = CAT.ROW_ID AND ACT.COST_CODE_ID = COS.ROW_ID AND
ACT.LOCATION_ID = LOC.ROW_ID AND ACT.MEMBER_ID = MEM.ROW_ID
变为
SELECT DISTINCT ACT.ROW_ID, ACT.CREATED, MEM.FIRST_NAME, MEM.LAST_NAME, LOC.NAME,
CAT.NAME, SER.NAME, EMP.FIRST_NAME, EMP.LAST_NAME, SER.DURATION,
ACT.CASH, COS.NAME, ACT.COMMENTS, ACT.TIP
FROM S_ACTIVITY ACT
INNER JOIN S_LOCATION LOC ON ACT.LOCATION_ID = LOC.ROW_ID
INNER JOIN S_CATEGORY CAT ON ACT.CATEGORY_ID = CAT.ROW_ID
INNER JOIN S_EMPLOYEE EMP ON ACT.EMPLOYEE_ID = EMP.ROW_ID
INNER JOIN S_SERVICE SER ON ACT.SERVICE_ID = SER.ROW_ID
INNER JOIN S_COST_CODE COS ON ACT.COST_CODE_ID = COS.ROW_ID
INNER JOIN S_MEMBER MEM ON ACT.MEMBER_ID = MEM.ROW_ID
区别 - 区别是SELECT
修饰符。它将结果限制为不同的行,与使用GROUP BY all_columns_here
相同。如果这是您的目标,那么请完全删除您的GROUP BY
,然后您就可以了(请参阅上面的查询)。这导致了最后一点......
分组依据/排序 - 您可以使用GROUP BY
或ORDER BY
,或两者兼而有之。这些并不需要彼此。此外,如果您有聚合,则只有SQL需要GROUP BY
,并且只有特定功能才需要ORDER BY
。否则,您可以随意使用它们来满足您的项目要求。