I'm new to this so I'm sorry if I asked this question wrong. But I'm trying to get 4 different SUM's from my table that uses 4 different cases. But I want the id to be listed only once with the totals. I'll show you what I have, and what I'm trying to get. Please help if possible.
SELECT schools.name, articles.competition_place,
Case when articles.competition = 'yes' and competition_place = '1' then int '100'
when articles.competition = 'yes' and competition_place = '2' then int '60'
when articles.competition = 'yes' and competition_place = '3' then int '20'
ELSE 0 end AS "Competition_Score",
Case when articles.out_reach = 'yes' then int '30'
ELSE 0 end AS "out_reach_Score",
CASE when schools.school_id is not null then int '5'
ELSE 0 end as "article_score",
(Case when articles.competition = 'yes' and competition_place = '1' then int '100'
when articles.competition = 'yes' and competition_place = '2' then int '60'
when articles.competition = 'yes' and competition_place = '3' then int '20'
ELSE 0 end) +
(Case when articles.out_reach = 'yes' then int '30'
ELSE 0 end) +
(CASE when schools.school_id is not null then int '5'
ELSE 0 end) as "total_score"
from articles
join clubs on articles.club_id = clubs.club_id
join schools on clubs.school_id = schools.school_id
My table that I have
And this is what I'm trying to get.
This is the table I'm trying to get
Is this possible?
use aggregation and group by
SELECT schools.name, articles.competition_place,
sum(Case when articles.competition = 'yes' and competition_place = '1' then int '100'
when articles.competition = 'yes' and competition_place = '2' then int '60'
when articles.competition = 'yes' and competition_place = '3' then int '20'
ELSE 0 end) AS "Competition_Score",
sum(Case when articles.out_reach = 'yes' then int '30'
ELSE 0 end) AS "out_reach_Score",
sum(CASE when schools.school_id is not null then int '5'
ELSE 0 end) as "article_score",
sum((Case when articles.competition = 'yes' and competition_place = '1' then int '100'
when articles.competition = 'yes' and competition_place = '2' then int '60'
when articles.competition = 'yes' and competition_place = '3' then int '20'
ELSE 0 end)) +
sum((Case when articles.out_reach = 'yes' then int '30'
ELSE 0 end)) +
sum(CASE when schools.school_id is not null then int '5'
ELSE 0 end)) as "total_score"
from articles
join clubs on articles.club_id = clubs.club_id
join schools on clubs.school_id = schools.school_id
group by schools.name, articles.competition_place
Related
I am using a CASE WHEN statement and a SUM function. I want to replace all nulls with 0.
I tried adding a 0 at the end of my statement.
This is my current code
select ID, PT_Number,
ORGANIZATION,
[Approved] = sum(case when PT_TYPE = 'Lend' then Approved END),
[Disbursed] = sum(case when PT_TYPE = 'Give' then Disbursed end),
[Repaid] = sum(case when PT_TYPE = 'Pay' then Payment end)
from TABLE1
group by ID, PT_Number,
ORGANIZATION
This is what I am trying
[Approved] = sum(case when PT_TYPE = 'Lend' then Approved END, 0),
[Disbursed] = sum(case when PT_TYPE = 'Give' then Disbursed end, 0),
[Repaid] = sum(case when PT_TYPE = 'Pay' then Payment end, 0)
Just add an else clause:
select ID, PT_Number,
ORGANIZATION,
[Approved] = sum(case when PT_TYPE = 'Lend' then Approved else 0 END),
[Disbursed] = sum(case when PT_TYPE = 'Give' then Disbursed else 0 end),
[Repaid] = sum(case when PT_TYPE = 'Pay' then Payment else 0 end)
from TABLE1
group by ID, PT_Number, ORGANIZATION;
You could also use coalesce() but that seems like overkill.
I wrote following sql query for a report.
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30';
this is the output
but my desired output is here
So how I change my query for desired result(grouping to ACC and GL_ID)?
The quick and dirty approach is to wrap the entire query and group by, this would prevent duplicate values in your 3,4,5, 6th columns.
The cleaner approach would be to take those 4 columns and wrap each one in a Select SUM(...) as you did for the later columns.
In short, no GROUP BY will leave you with duplicates - I took the liberty of giving you the syntax for the quick and dirty approach, see below.
SELECT
ACC
,GL_ID
,GL_NAME
,SUM(CASHCR) AS CASHCR
,SUM(CASHDR) AS CASHDR
,SUM(OTHERCR) AS OTHERCR
,SUM(OTHERDR) AS OTHERDR
,BRNCODE
,DRBRFORE
,CRBRFORE
FROM (
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30'
)A
GROUP BY
ACC
,GL_ID
,GL_NAME
,CASHCR
,CASHDR
,OTHERCR
,OTHERDR
,BRNCODE
,DRBRFORE
,CRBRFORE
;
You could surround your query with a CTE then aggregate the fields you require.
WITH this as
(
Select AT.OTHER_GL_CODE As ACC,
GL.GL_ID,
GL.GL_NAME,
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHCR',
(Case When (
AT.OTHER_TRN_DEC = 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'CASHDR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'CR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERCR',
(Case When (
AT.OTHER_TRN_DEC <> 'Cash'
And AT.OTHER_CR_DR = 'DR'
) Then AT.OTHER_AMOUNT
Else '0'
End
) As 'OTHERDR',
AT.OTHER_BRN_CODE BRNCODE,
(
Select Sum( Case When TR.CR_DR = 'DR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As DRBRFORE,
(
Select Sum( Case When TR.CR_DR = 'CR'
And TR.BRANCH_CODE = 1000
And TR.TRAN_DATE < '2017-07-01' Then TR.GL_TRN_AMT
Else 0
End
)
From COREBANKER1.dbo.GL_DAILY_TRN As TR
Where TR.GL_CODE = AT.OTHER_GL_CODE
) As CRBRFORE
From COREBANKER1.dbo.MAIN_OTHER_TRN As AT,
COREBANKER1.dbo.GL_MAIN As GL
Where AT.OTHER_BRN_CODE = 1000
And GL.GL_ID = AT.OTHER_GL_CODE
And AT.OTHER_BANK_DATE
Between '2017-07-01' And '2017-07-30';
)
)
SELECT ACC ,GL_ID ,GL_NAME, SUM(CASHCR) AS CASHCR ,SUM(CASHDR) AS CASHDR ,SUM(OTHERCR) AS OTHERCR ,SUM(OTHERDR) AS OTHERDR, BRNCODE, DRBRFORE, CRBRFORE
FROM this
GROUP BY ACC ,GL_ID ,GL_NAME /*,CASHCR ,CASHDR ,OTHERCR ,OTHERDR */,BRNCODE
,DRBRFORE ,CRBRFORE
I'm new to SQL and I'm using SQL Server 2008 Management Studio. The below code isn't working due to a decimal being converted to an INT. From similar issues I can see that I need to use CONVERT, but unsure how to use this in my code?
DROP TABLE [DBO].[STEP9_OUTPUT]
GO
SELECT
S8.*,
CAST(CASE
WHEN S8.ERROR_TRANSACTION = 'N'
THEN NOERROR_VALUE
WHEN S8.SOURCE_SYSTEM_IND = 'P'
THEN P_VALUE
ELSE A_VALUE
END AS FLOAT) AS CORRECTED_PRICE
INTO STEP9_OUTPUT
FROM
(SELECT
S8.*,
CASE
WHEN S8.SOURCE_SYSTEM_IND = 'P' AND DIVRATE = 'Y'
THEN P_VALUE_DIV
WHEN S8.SOURCE_SYSTEM_IND = 'P' AND DIVRATE = 'N'
THEN P_VALUE_MULT
ELSE NULL
END AS P_VALUE
FROM
(SELECT
S8.*,
CASE
WHEN S8.SOURCE_SYSTEM_IND = 'P' AND
S8.ERROR_TRANSACTION = 'Y' AND
DIVRATE = 'N' AND S8.FN > 0
THEN S8.FN
WHEN S8.UNITS_INFUND_FOR_TRADE > 0
THEN S8.FTP
WHEN S8.FJFG = 'N' AND S8.RATE IS NULL
THEN S8.FB / 100 * 1
WHEN S8.FJFG = 'N' AND S8.RATE IS NOT NULL
THEN S8.FB / 100 * S8.RATE
WHEN S8.FJFG <> 'N' AND S8.RATE IS NULL
THEN 1 * 1
WHEN S8.FJFG <> 'N' AND S8.RATE IS NOT NULL
THEN 1 * S8.RATE
ELSE NULL
END AS P_VALUE_MULT,
CASE
WHEN S8.SOURCE_SYSTEM_IND = 'P' AND
S8.ERROR_TRANSACTION = 'Y' AND
[DIVRATE] = 'Y' AND S8.FN > 0
THEN S8.FN
WHEN S8.UNITS_INFUND_FOR_TRADE > 0
THEN S8.FTP
WHEN S8.FJFG = 'N' AND S8.RATE IS NULL
THEN S8.FB / 100
WHEN S8.FJFG = 'N' AND S8.RATE IS NOT NULL
THEN S8.FB / (100 / S8.RATE)
ELSE NULL
END AS P_VALUE_DIV
FROM
(SELECT
S8.*,
CASE
WHEN S8.ERROR_TRANSACTION = 'N'
THEN FUND_UNIT_PRICE_FOR_TRADE
ELSE NULL
END AS NOERROR_VALUE,
CASE
WHEN S8.SOURCE_SYSTEM_IND = 'A' AND
S8.ERROR_TRANSACTION = 'Y' AND
S8.ASSET_CODE = 'XX'
THEN S8.FUND_BID_VALUE
ELSE NULL
END AS A_VALUE,
CASE
WHEN S8.FACURR = 'SEK' OR S8.FACURR = 'JPY'
THEN 'Y'
ELSE 'N'
END AS DIVRATE
FROM
STEP8_OUTPUT S8) S8
) S8
) S8
SELECT CAST(CAST('2.126' as decimal) as int)
I am trying to store the following sql in a bean within an XML file. I am trying to escape the greater than and less than signs, however I seem to still be getting an error in Eclipse. I must not understand how to properly escape these signs. I count six less than and greater than signs, which I have attempted to escape.
Here's my attempt:
<property name="SQL" value="<><><><><><>"
"SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') SQL_RUN_DT
,'STG_DISB_HOLD_TH' SRC_TBL
,A1.DISB_DT
,A1.CPC
,A1.AWARD_YR
,COUNT(1) ROW_CNT
,SUM(A1.APPL_NBR) APPL_NBR_SUM
,SUM(A1.TSYS_SCHL_ID) TSYS_SCHL_ID_SUM
,SUM(TO_NUMBER(A1.DISB_NBR)) DISB_NBR_SUM
,SUM(A1.DISB_SEQ_NBR) DISB_SEQ_NBR_SUM
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 THEN 1
ELSE A1.INTNL_APPL_SEQ_NBR + 1
END) INTERNAL_DISB_SEQ_SUM
,MIN(A1.DISB_RCVD_DT) DISB_RCVD_DT_MIN
,MAX(A1.DISB_RCVD_DT) DISB_RCVD_DT_MAX
,SUM(CASE WHEN A1.DISB_STAT_CD = 'P' THEN 1 ELSE 0 END) DISB_STAT_CD_P
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' THEN 1 ELSE 0 END) DISB_STAT_CD_R
,MIN(A1.PYMT_START_DT) PYMT_START_DT_MIN
,MAX(A1.PYMT_START_DT) PYMT_START_DT_MAX
,COUNT(DISTINCT A1.SCHL_ENROLL_CD) SCHL_ENROLL_CD_CNT
,SUM(TO_NUMBER(A1.NET_TRANS_CD)) NET_TRANS_CD_SUM
,SUM(A1.NET_TRANS_AMT) NET_TRANS_AMT_SUM
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 AND A1.NET_C_OR_D_IND = 'D' THEN A1.NET_TRANS_AMT + A1.NET_TRANS_AMT
WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 AND A1.NET_C_OR_D_IND <> 'D' THEN A1.NET_TRANS_AMT - A1.NET_TRANS_AMT
WHEN (A1.DISB_STAT_CD <> 'R' OR A1.DISB_SEQ_NBR <> 1) AND A1.NET_C_OR_D_IND = 'D' THEN A1.SUM_NET_AMT + A1.NET_TRANS_AMT
WHEN (A1.DISB_STAT_CD <> 'R' OR A1.DISB_SEQ_NBR <> 1) AND A1.NET_C_OR_D_IND <> 'D' THEN A1.SUM_NET_AMT - A1.NET_TRANS_AMT
ELSE 0
END) SUM_NET_AMT_SUM
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'F' THEN 1 ELSE 0 END) ENRL_STATUS_CD_F
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'Q' THEN 1 ELSE 0 END) ENRL_STATUS_CD_Q
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'H' THEN 1 ELSE 0 END) ENRL_STATUS_CD_H
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'L' THEN 1 ELSE 0 END) ENRL_STATUS_CD_L
,SUM(CASE WHEN A1.ENRL_STATUS_CD IS NULL THEN 1 ELSE 0 END) ENRL_STATUS_CD_NULL
,COUNT(DISTINCT A1.PGM_CIP_CD) PGM_CIP_CD_CNT
,COUNT(DISTINCT A1.LEGACY_USER_ID) LEGACY_USER_ID_CNT
,COUNT(DISTINCT A1.MAINT_APP) MAINT_APP_CNT
,MIN(A1.MAINT_DTM) MAINT_DTM_MIN
,MAX(A1.MAINT_DTM) MAINT_DTM_MAX
,COUNT(DISTINCT A1.MAINT_USERID) MAINT_USERID_CNT
FROM
(
SELECT LM.TEACH_MASTER_ID
,SAM.APPL_NBR
,TO_NUMBER(SAM.AWARD_YR) AWARD_YR
,SAM.TSYS_SCHL_ID
,SAM.AWARD_NBR
,SAM.DISB_NBR
,SAM.DISB_SEQ_NBR
,SAM.CPC
,SAM.DISB_DT
,SAM.DISB_RCVD_DT
,SAM.DISB_STAT_CD
,SAM.PYMT_START_DT
,SAM.SCHL_ENROLL_CD
,SAM.NET_TRANS_CD
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.NET_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.NET_TRANS_AMT
END NET_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.GROSS_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.GROSS_TRANS_AMT
END GROSS_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.FEE_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.FEE_TRANS_AMT
END FEE_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.REBATE_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.REBATE_TRANS_AMT
END REBATE_TRANS_AMT
,SAM.MAINT_DTM
,SAM.MAINT_USERID
,SAM.ENRL_STATUS_CD
,SAM.PGM_CIP_CD
,'BATCH' AS LEGACY_USER_ID
,'BATCH' AS MAINT_APP
,SAM.NET_C_OR_D_IND
,ROW_NUMBER() OVER (PARTITION BY SAM.APPL_NBR, SAM.AWARD_YR, SAM.TSYS_SCHL_ID, SAM.AWARD_NBR, SAM.DISB_NBR
ORDER BY DTSEQ.INTNL_APPL_SEQ_NBR DESC) ROW_NUM_PART
,COALESCE(DTSEQ.INTNL_APPL_SEQ_NBR,0) INTNL_APPL_SEQ_NBR
,DTSEQ.CHG_GROSS_TRANS_AMT DT_CHG_GROSS_TRANS_AMT
,DTSEQ.CHG_NET_TRANS_AMT DT_CHG_NET_TRANS_AMT
,DTSEQ.CHG_FEE_TRANS_AMT DT_CHG_FEE_TRANS_AMT
,DTSEQ.CHG_REBATE_TRANS_AMT DT_CHG_REBATE_TRANS_AMT
,COALESCE(DTSEQ.SUM_GROSS_AMT,0) SUM_GROSS_AMT
,COALESCE(DTSEQ.SUM_NET_AMT,0) SUM_NET_AMT
,COALESCE(DTSEQ.SUM_FEE_AMT,0) SUM_FEE_AMT
,COALESCE(DTSEQ.SUM_REBATE_AMT,0) SUM_REBATE_AMT
FROM MYSCHEMA.TEACH_MASTER LM
JOIN MYSCHEMA.STG_DISB_HOLD SAM
ON LM.APP_NUM_LEGACY = SAM.APPL_NBR
AND LM.ATTEND_SCHL_MASTER_ID = SAM.TSYS_SCHL_ID
AND LM.AWARD_YR = TO_NUMBER(SAM.AWARD_YR)
LEFT JOIN MYSCHEMA.STG_DISB_TRANS DTSEQ
ON SAM.APPL_NBR = DTSEQ.APPL_NBR
AND SAM.AWARD_YR = DTSEQ.AWARD_YR
AND SAM.TSYS_SCHL_ID = DTSEQ.TSYS_SCHL_ID
AND SAM.AWARD_NBR = DTSEQ.AWARD_NBR
AND SAM.DISB_NBR = DTSEQ.DISB_NBR
WHERE SAM.DISB_STAT_CD IN ('R','P')
AND SAM.NET_TRANS_CD IN ('0102','0131','0161','0189','0200')
) A1
WHERE A1.ROW_NUM_PART = 1
GROUP BY TO_CHAR(SYSDATE,'YYYY-MM-DD')
,'STG_DISB_HOLD_TH'
,A1.DISB_DT
,A1.CPC
,A1.AWARD_YR
;"/>
You have to escape them ALL:
WHEN A1.DISB_STAT_CD [..snip..] A1.NET_C_OR_D_IND <> 'D' THEN A1.NET_TRANS_AMT - A1.NET_TRANS_AMT
^^--missed a bunch of these
Plus, this next bit makes no sense:
<property name="SQL" value="<><><><><><>"
^---start attribute end attribute --^
"SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') SQL_RUN_DT
^---attribute value with no name for it
I have a SP that was built for us that does a summary statement of tables in our DB. What I am trying to do is make it so the SP also pulls that last year/month of data as well based on the date entered. Below is the SQL code I am working with. What I am trying to get is a total and Volume field that is the sum based on the date parameter entered minus 1 month.
For example:
If I put in 2013 10 01 start and 2013 10 31 end I would get the total and volume for 2013-10-01 to 2013-10-31 and in 2 separate columns the total and volume for 2013-09-01 to 1013-09-30
Code
(
#Start DATETIME,
#End DATETIME
)
AS
DECLARE
#reference int,
#sSQL VARCHAR(2000)
BEGIN
select Convert(datetime,Cast(edi.Creation_dt as varchar(8)),103) as Date, ia.xref_no_tx, la.ldc_acct_no, la.serv_loc_nm
, a.acct_nm, c.company_last_nm
, Case RG.Rate_cd
When 'DLS' then 'HEDGE'
When 'STL' then 'STL'
WHen 'SPOT BILLING' then 'SPOT'
WHen 'SL SPOT' then 'STL SPOT'
Else null
End as Acct_type
, Convert(datetime,Cast(ia.start_dt as varchar(8)),103)as Start_dt
, Convert(datetime,Cast(ia.end_dt as varchar(8)),103) as End_dt
, edi.trans_sub_ty as Inv_type
, max( case when la.class_ty_cd = 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'FEES' then th.trans_qty
when la.class_ty_cd = 'MUNI' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'EXCS' then th.trans_qty
when la.class_ty_cd <> 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'BASE' then th.trans_qty
else 0 end) as Volume
, sum(th.trans_am) as Total
from invoice_advise_relate iar, transaction_history th
,invoice_advise ia, ldc_account la, account a, customer c, edi_transaction edi
, (select max(edi_trans_id) as m_edi_trans, relate_id from edi_transaction where class_nm = 'cInvoiceAdvise' group by relate_id) as edic
, (Select max(rating_group_id) as m_rate, ldc_acct_id from rating_group group by ldc_acct_Id) as C_Rate
, rating_group rg
where iar.trans_id = th.trans_id
and th.cancel_in = 'N'
and th.trans_ty_cd not in ('PAY', 'ANC')
and iar.inv_adv_id = ia.inv_adv_id
and ia.ldc_acct_id = la.ldc_acct_id
and la.acct_id = a.acct_id
and a.cust_id = c.cust_id
and la.ldc_acct_no not like 'E%'
and edi.Creation_dt >= convert(varchar,#Start,112)
and edi.Creation_dt <= convert(varchar,#End,112)
and edi.relate_id = ia.inv_adv_id
and edic.m_edi_trans = edi.edi_trans_id
and edi.response_cd = ''
and rg.rating_group_id = C_Rate.M_Rate
and C_Rate.LDC_Acct_Id = la.ldc_Acct_Id
and edi.trans_sub_ty <> '00'
group by edi.Creation_dt, ia.xref_no_tx, la.ldc_acct_no,la.serv_loc_nm, a.acct_nm, c.company_last_nm, ia.start_dt, ia.end_dt,edi.trans_sub_ty, rg.rate_cd
Start off by declaring and initializing a start date for the previous month.
DECLARE #PrevStart datetime
SELECT #PrevStart = dateadd(month, -1, #Start)
In your WHERE clause substitute the previous start date for start date so that you include last month's data as well as this month's.
and edi.Creation_dt >= convert(varchar,#PrevStart,112)
and edi.Creation_dt <= convert(varchar,#End,112)
Then you filter last month's data from this month's using CASE statement logic.
, max( case when la.class_ty_cd = 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'FEES'
AND edi.Creation_dt >= convert(varchar,#Start,112) then th.trans_qty
when la.class_ty_cd = 'MUNI' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'EXCS'
AND edi.Creation_dt >= convert(varchar,#Start,112) then th.trans_qty
when la.class_ty_cd <> 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'BASE'
AND edi.Creation_dt >= convert(varchar,#Start,112) then th.trans_qty
else 0 end) as Volume
, sum(CASE WHEN edi.Creation_dt >= convert(varchar,#Start,112) THEN th.trans_am ELSE 0 END) as Total
, max( case when la.class_ty_cd = 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'FEES'
AND edi.Creation_dt < convert(varchar,#Start,112) then th.trans_qty
when la.class_ty_cd = 'MUNI' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'EXCS'
AND edi.Creation_dt < convert(varchar,#Start,112) then th.trans_qty
when la.class_ty_cd <> 'COMM' and th.ppa_in = 'N' and th.trans_sub_ty_cd = 'BASE'
AND edi.Creation_dt < convert(varchar,#Start,112) then th.trans_qty
else 0 end) as PrevVolume
, sum(CASE WHEN edi.Creation_dt < convert(varchar,#Start,112) THEN th.trans_am ELSE 0 END) as PrevTotal