加入两张桌子

时间:2009-12-23 07:47:50

标签: sql-server

我如何加入这两个查询

SELECT
GLMA_COMP_CODE COMP,
GLMA_ACCT_CODE ACCOUNT_NO,
GLMA_DEPT_NO DEPT,
GLMA_ACCT_NAME ACCOUNT_NAME
FROM GLAS_GL_MASTERS 
WHERE GLMA_COMP_CODE = @COMP_CODE
AND GLMA_YEAR = CONVERT(NUMERIC(8, 2), DATEPART(YYYY, @DATE_FROM))
AND GLMA_ACCT_CODE BETWEEN ISNULL(@ACCT_CODE_FROM, GLMA_ACCT_CODE) AND 
ISNULL(@ACCT_CODE_TO, GLMA_ACCT_CODE)
ORDER BY GLMA_ACCT_CODE 

SELECT
GLTR_COMP_CODE,
GLTR_DEPT_NO,
GLTR_ACCT_CODE,
CAST(GLTR_PSTNG_TYPE AS VARCHAR) + CAST(GLTR_PSTNG_NO AS VARCHAR) REF_NO,
CAST(GLTR_DOC_CODE AS VARCHAR) + CAST(GLTR_OUR_DOC_NO AS VARCHAR) DOC_NO,
GLTR_DOC_DATE DOC_DATE,
GLTR_DOC_NARR NARRATIVE,
GLTR_PSTNG_DATE,

CASE SIGN(GLTR_TRAN_AMT) 
WHEN + 1 THEN GLTR_TRAN_AMT 
ELSE 0 
END DEBIT,

CASE SIGN(GLTR_TRAN_AMT) 
WHEN - 1 THEN GLTR_TRAN_AMT 
ELSE 0 
END CREDIT,
GLTR_TRAN_AMT AMOUNT,
GLTR_FC_CODE FC_CODE,
GLTR_FC_AMT FC_AMOUNT
FROM GLAS_GL_TRANSACTIONS 
WHERE GLTR_PSTNG_DATE BETWEEN ISNULL(@DATE_FROM, GLTR_PSTNG_DATE) AND ISNULL(@DATE_TO, GLTR_PSTNG_DATE)
ORDER BY GLTR_ACCT_CODE,
GLTR_PSTNG_DATE,
GLTR_DOC_CODE,
GLTR_OUR_DOC_NO

1 个答案:

答案 0 :(得分:0)

从提供的内容来看,这里是:

  SELECT t.gltr_comp_code,
         t.gltr_acct_code,
         t.gltr_dept_no,
         m.glma_acct_name
         CAST(t.gltr_pstng_type AS VARCHAR) + CAST(t.gltr_pstng_no AS VARCHAR) REF_NO,
         CAST(t.gltr_doc_code AS VARCHAR) + CAST(t.gltr_our_doc_no AS VARCHAR) DOC_NO,
         t.gltr_doc_date DOC_DATE,
         t.gltr_doc_narr NARRATIVE,
         t.gltr_pstng_date,
         CASE SIGN(t.gltr_tran_amt) WHEN + 1 THEN t.gltr_tran_amt ELSE 0 END DEBIT,
         CASE SIGN(t.gltr_tran_amt) WHEN - 1 THEN t.gltr_tran_amt ELSE 0 END CREDIT,
         t.gltr_tran_amt AMOUNT,
         t.gltr_fc_code FC_CODE,
         t.gltr_fc_amt FC_AMOUNT
    FROM GLAS_GL_TRANSACTIONS t
    JOIN GLAS_GL_MASTERS m ON m.glma_comp_code = t.gltr_comp_code
                          AND m.glma_acct_code = t.gltr_acct_code
                          AND m.glma_dept_no = t.glma_dept_no
   WHERE t.gltr_pstng_date BETWEEN COALESCE(@DATE_FROM, t.gltr_pstng_date) AND COALESCE(@DATE_TO, t.gltr_pstng_date)
     AND m.glma_comp_code = @COMP_CODE
     AND m.glma_year = CONVERT(NUMERIC(8, 2), DATEPART(YYYY, @DATE_FROM))
     AND m.glma_acct_code BETWEEN COALESCE(@ACCT_CODE_FROM, t.glma_acct_code) AND COALESCE(@ACCT_CODE_TO, t.glma_acct_code)
ORDER BY t.gltr_acct_code, t.gltr_pstng_date, t.gltr_doc_code, t.gltr_our_doc_no