2016-07-13 91 views
0

我今年夏天一直在爲一家公司開發一個項目,即使在即將畢業的學校,我仍然非常喜歡noob,但我一直遇到與分組相關的問題和按子句排序的問題。按問題分組排序MSSQL

沒有我嘗試似乎工作,繼承人是我迄今爲止

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 

那與統計網上其他導遊在說我(的語法suuuuper由/訂單一塌糊塗組)需要一些排序或計數的訂單...繼承人原來...

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 

感謝您的幫助,非常感謝! :)

+2

(1)我刪除了無關的數據庫標記。隨意爲您真正使用的數據庫添加標籤。 (2)學習使用明確的,適當的'JOIN'語法。 FROM子句中的逗號很難閱讀。 –

+0

我強烈建議閱讀以下內容:[SQL加入的可視化表示](http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins) –

+0

沒有什麼明顯的錯誤這些查詢的「GROUP BY」和「ORDER BY」子句。 – Uueerdo

回答

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 BYORDER BY,或兩者 - 這導致了最後一點...

集團通過/順序按。這些不需要對方。另外,如果您有一個聚合,則只有SQL需要GROUP BY,並且只有特定功能需要ORDER BY。否則,您可以隨意使用它們來滿足您的項目需求。

+0

感謝您的幫助,仍然對訂購內容做了一些調整,但是我想我已經瞭解了一些邏輯,感謝所有的評論! – Rolthar

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(*)