2013-04-12 75 views
7

我有這個查詢,我發現我想修改,以獲得額外的列,也總結了發現量的最後3個月。我想爲此製作一份Crystal Report。 查詢如下。修改交叉連接查詢

SELECT 
    dbo.[@EIM_PROCESS_DATA].U_Tax_year, 
    dbo.[@EIM_PROCESS_DATA].U_Employee_ID, 
    SUM(dbo.[@EIM_PROCESS_DATA].U_Amount) AS PAYE, 
    dbo.OADM.CompnyName, 
    dbo.OADM.CompnyAddr, 
    dbo.OADM.TaxIdNum, 
    dbo.OHEM.lastName + ', ' + ISNULL(dbo.OHEM.middleName, '') + ' ' + 
     ISNULL(dbo.OHEM.firstName, '') AS EmployeeName, dbo.OHEM.govID 

FROM dbo.[@EIM_PROCESS_DATA] 
     INNER JOIN dbo.OHEM ON dbo.[@EIM_PROCESS_DATA].U_Employee_ID 
              = dbo.OHEM.empID CROSS JOIN dbo.OADM 
WHERE (dbo.[@EIM_PROCESS_DATA].U_PD_code = 'SYS033') 

GROUP BY 
    dbo.[@EIM_PROCESS_DATA].U_Tax_year, 
    dbo.[@EIM_PROCESS_DATA].U_Employee_ID, 
    dbo.OADM.CompnyName, 
    dbo.OADM.CompnyAddr, 
    dbo.OADM.TaxIdNum, 
    dbo.OHEM.lastName, 
    dbo.OHEM.firstName, 
    dbo.OHEM.middleName, 
    dbo.OHEM.govID 

OHEM包含一個名爲U_Process_month的字母數字字段有漢字是從一月到十二月。由於查詢如上,SUM(dbo.[@EIM_PROCESS_DATA].U_Amount)給出了所有PAYE金額的合計,ie. U_PD_code = 'SYS033'

我想有一個查詢,基於所選年份和月份,累計最近3個月(PAYE)。

我也想檢索和額外的列,SUM(dbo.[@EIM_PROCESS_DATA].U_Amount) as TAXABLEPAY where (dbo.[@EIM_PROCESS_DATA].U_PD_code = 'SYS034')

我該如何做到這一點?任何幫助讚賞。

+2

你能提供一些樣本數據和預期的結果嗎? –

回答

8

我不確定U_Tax_year是什麼數據類型,所以我把它作爲INT。但是,此查詢應返回您設置的月份之前的3個月。

DECLARE @start_month DATETIME; 
DECLARE @start_year INT; 

SET @start_month = '2013-04-01'; 
SET @start_year = 2013; 

SELECT dbo.[@EIM_PROCESS_DATA].U_Tax_year 
    , dbo.[@EIM_PROCESS_DATA].U_Employee_ID 
    , SUM(CASE WHEN dbo.[@EIM_PROCESS_DATA].U_PD_code = 'SYS033' THEN dbo.[@EIM_PROCESS_DATA].U_Amount ELSE 0 END) AS PAYE 
    , SUM(CASE WHEN dbo.[@EIM_PROCESS_DATA].U_PD_code = 'SYS034' THEN dbo.[@EIM_PROCESS_DATA].U_Amount ELSE 0 END) AS TAXABLEPAY 
    , dbo.OADM.CompnyName 
    , dbo.OADM.CompnyAddr 
    , dbo.OADM.TaxIdNum 
    , dbo.OHEM.lastName + ', ' + ISNULL(dbo.OHEM.middleName, '') + ' ' + ISNULL(dbo.OHEM.firstName, '') AS EmployeeName 
    , dbo.OHEM.govID 
    FROM dbo.[@EIM_PROCESS_DATA]INNER JOIN dbo.OHEM ON dbo.[@EIM_PROCESS_DATA].U_Employee_ID = dbo.OHEM.empID CROSS JOIN dbo.OADM 
WHERE dbo.[@EIM_PROCESS_DATA].U_PD_code IN ('SYS033', 'SYS034') 
    AND dbo.OHEM.U_Process_month IN (DATENAME(MONTH, DATEADD(MONTH,-3, @start_month)), DATENAME(MONTH, DATEADD(MONTH,-2, @start_month)), DATENAME(MONTH, DATEADD(MONTH,-1, @start_month))) 
    AND dbo.[@EIM_PROCESS_DATA].U_Tax_year = @start_year 
GROUP BY dbo.[@EIM_PROCESS_DATA].U_Tax_year 
    , dbo.[@EIM_PROCESS_DATA].U_Employee_ID 
    , dbo.OADM.CompnyName 
    , dbo.OADM.CompnyAddr 
    , dbo.OADM.TaxIdNum 
    , dbo.OHEM.lastName 
    , dbo.OHEM.firstName 
    , dbo.OHEM.middleName 
    , dbo.OHEM.govID; 
2
 
-- You can sum up columns based on filters by using a case 
SUM(CASE WHEN U_PD_code = 'SYS033' THEN dbo.[@EIM_PROCESS_DATA].U_Amount 
      ELSE 0) AS PAYE, 
SUM(CASE WHEN U_PD_code = 'SYS034' THEN dbo.[@EIM_PROCESS_DATA].U_Amount 
      ELSE 0) AS PAYE2, 

-- Obviously your WHERE becomes 
WHERE (dbo.[@EIM_PROCESS_DATA].U_PD_code IN ('SYS033','SYS034')) 

-- Your month year fitler can be added to your where like this.. 
-- You can convert month to a date and then use BETWEEN a month parameeter, and 2 months back (3 months total) 
-- Note I assumed month name for your param, change to number if needed. You gave no info on year so I assumed it was as shown. 
AND MONTH(U_Process_month + ' 1 2010') BETWEEN MONTH(@YourMonth + ' 1 2010') AND MONTH(@YourMonth + ' 1 2010') - 2 
AND dbo.[@EIM_PROCESS_DATA].U_Tax_year = @YourYear