2010-01-25 102 views
1

我有兩個查詢:加盟查詢SQL Server 2005中

SELECT PC_COMP_CODE, 
    PC_SL_LDGR_CODE, 
    PC_SL_ACNO ACCOUNT, 
    COUNT(PC_CHEQUE_NO) CHQS, 
    SUM(CONVERT(FLOAT, PC_AMOUNT)) CHQ_AMT 
        FROM GLAS_PDC_CHEQUES 
        WHERE PC_COMP_CODE = '1' 
        AND PC_DISCD IS NULL 
        GROUP BY PC_SL_LDGR_CODE, 
      PC_SL_ACNO ,PC_COMP_CODE 
         ORDER BY PC_SL_ACNO 

-------------------------------------------------- 

    SELECT COAD_PTY_FULL_NAME,PC_COMP_CODE, PC_SL_LDGR_CODE, PC_SL_ACNO, PC_DEPT_NO, PC_DOC_TYPE, PC_CHEQUE_NO, PC_BANK_AC_NO FROM GLAS_PTY_ADDRESS,GLAS_SBLGR_MASTERS,GLAS_PDC_CHEQUES WHERE 
    COAD_COMP_CODE = '1' AND SLMA_COMP_CODE = COAD_COMP_CODE AND SLMA_ADDR_ID = COAD_ADDR_ID 
    AND SLMA_LDGRCTL_CODE = PC_SL_LDGR_CODE AND PC_COMP_CODE=SLMA_COMP_CODE 
    AND SLMA_ACNO = PC_SL_ACNO 
    AND SLMA_LDGRCTL_YEAR = DBO.GLAS_VALIDATIONS_GET_OPEN_YEAR(PC_COMP_CODE) 

如果我單獨執行的第一個查詢,我得到5個記錄......

如果我加入上述兩個查詢像:

SELECT 
PC_COMP_CODE, 
    PC_SL_LDGR_CODE, 
    PC_SL_ACNO ACCOUNT, 
    COUNT(PC_CHEQUE_NO) CHQS, 
    SUM(CONVERT(FLOAT, PC_AMOUNT)) CHQ_AMT, 
COAD_PTY_FULL_NAME 
FROM GLAS_PDC_CHEQUES 
LEFT OUTER JOIN GLAS_SBLGR_MASTERS 
ON(SLMA_COMP_CODE=PC_COMP_CODE AND 
SLMA_LDGRCTL_CODE = PC_SL_LDGR_CODE AND SLMA_ACNO = PC_SL_ACNO) 
LEFT OUTER JOIN GLAS_PTY_ADDRESS ON(SLMA_COMP_CODE = COAD_COMP_CODE AND SLMA_ADDR_ID = COAD_ADDR_ID) 

WHERE PC_COMP_CODE = '1' 
    AND PC_DISCD IS NULL AND SLMA_LDGRCTL_YEAR = DBO.GLAS_VALIDATIONS_GET_OPEN_YEAR(PC_COMP_CODE) 
GROUP BY PC_SL_LDGR_CODE, 
    PC_SL_ACNO ,PC_COMP_CODE,COAD_PTY_FULL_NAME 
ORDER BY PC_SL_ACNO 

然後我就得到2個記錄....

我需要5條記錄在加入後顯示..... 我該怎麼辦?

+4

我的眼睛! ......... – 2010-01-25 06:26:07

+1

米奇:不要在**查詢時直接查看* **! :-) – 2010-01-25 06:34:28

+0

我覺得這很少見,米奇,但LLOL。 _(字面上)_ – Jay 2010-01-25 06:55:14

回答

1

我對第一個查詢中的所有字段進行了grep,使其更具可讀性,並且更有可能使某人能夠理解事物。不幸的是,我放棄了這一點。

下面的結果,但爲了它的價值,我會在黑暗中拍攝一張照片,並說您想加入UNION

SELECT id, first_name, account, COUNT(pants) pants, SUM(CONVERT(FLOAT, cost)) amount 
FROM table1 
WHERE id = '1' AND discount IS NULL GROUP BY first_name, account ,id 
ORDER BY account 

SELECT full_name,id, first_name, account, department, coffee_pref, pants, acct_no 
FROM table2,table3,table1 
WHERE secret_code = '1' 
    AND public_code = secret_code 
    AND addr_id = phone_id 
    AND nickname = first_name 
    AND id=public_code 
    AND movie_rating = account 
    AND album_year = DBO.get_random_year(id) 
+0

這是我的查詢正確還是錯誤? – Domnic 2010-01-25 09:04:51

+0

勇敢的+1。 – edosoft 2010-01-25 09:05:44

+0

Domnic,問題是沒有人可以用所有大寫字母中的晦澀名字讀取您的查詢,更不用說理解它。 同樣,你是否試圖從第一個查詢中得到5個結果和從第二個查詢中得到2個結果(總共7個結果)? – Jay 2010-01-25 09:33:00