Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dsscholar
Starting Member
6 Posts |
Posted - 2011-09-11 : 01:03:34
|
| Hi all, I have this following query. This needs to be changed to a single query with case statement. Please advise.Select t1.a,t2.b,t3.c as sfrom t1,t2,t3 where t1.x = t2.xand t2.y = t3.yand t1.a <> 0union Select t1.a,t2.b,null as sfrom t1,t2 where t1.x = t2.xand t1.a = 0In the second sql statement one join condition is not there and condition t1.a = 0 is there instead of t1.a <> 0. Is it posssible to replace this with case statement.Thanks in advance. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-11 : 11:32:24
|
its possible assuming the relationship b/w t2 and t3 is one to one. make it likeSelect t1.a,t2.b,case when t1.a <> 0 then t3.c else null end as sfrom t1inner join t2on t1.x = t2.xleft join t3 on t2.y = t3.y ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dsscholar
Starting Member
6 Posts |
Posted - 2011-09-12 : 02:10:54
|
| select BALANCES.*, COUNTRY.code AS InternalCounterpatyCountryCode, COUNTRY.name AS InternalCounterpatyCountry, (Left(BALANCES.[BankAccountNumber],Len(BALANCES.[BankAccountNumber])-4)) AS InternalCounterparty, CPARTY.ext_name AS InternalCounterpartyName, Year(BALANCES.CashflowDate) AS [Year], Month(BALANCES.CashflowDate) AS [Month], TRANS.deal_dt AS DealDate, TRANS.mature_dt AS MaturityDate, TRANS.Comments, ACCTS.BankExtName as InHouseBankName, ACCTS.BankName as InHouseBankCodefrom j0808_live_risk.dbo.jci_internal_balances BALANCES, j0808_live_qtm.dbo.jci_bankaccounts ACCTS, j0808_live_qtm.dbo.cparty CPARTY, j0808_live_qtm.dbo.country COUNTRY, j0808_live_qtm.dbo.jci_transactions TRANSwhere balances.bankaccountnumber = accts.bankaccountnumberand balances.dealnumber = trans.deal_noand country.thekey = cparty.countryand (Left(BALANCES.[BankAccountNumber],Len(BALANCES.[BankAccountNumber])-4))=CPARTY.[name]AND CPARTY.deal_int='Y' AND BALANCES.DealNumber<>0and balances.reportnumber in (select max(reportnumber) from j0808_live_risk.dbo.jci_internal_balances group by year(cashflowdate), month(cashflowdate))UNION select BALANCES.*, COUNTRY.code AS InternalCounterpatyCountryCode, COUNTRY.name AS InternalCounterpatyCountry, Left(BALANCES.BankAccountNumber,Len(BALANCES.BankAccountNumber)-4) AS InternalCounterparty, CPARTY.ext_name AS InternalCounterpartyName, Year(BALANCES.CashflowDate) AS [Year], Month(BALANCES.CashflowDate) AS [Month], null AS DealDate, null AS MaturityDate, null AS Comments, ACCTS.BankExtName AS InHouseBankName, ACCTS.BankName AS InHouseBankCodefrom j0808_live_risk.dbo.jci_internal_balances BALANCES, j0808_live_qtm.dbo.jci_bankaccounts ACCTS, j0808_live_qtm.dbo.cparty CPARTY, j0808_live_qtm.dbo.country COUNTRYwhere balances.bankaccountnumber = accts.bankaccountnumberand country.thekey = cparty.countryand (Left(BALANCES.[BankAccountNumber],Len(BALANCES.[BankAccountNumber])-4))=CPARTY.[name]and cparty.deal_int = 'Y'and balances.dealnumber = 0and balances.reportnumber in (select max(reportnumber) from j0808_live_risk.dbo.jci_internal_balances group by year(cashflowdate), month(cashflowdate))This is the query. I want to make it as one statement with case statement or someother means if possible. In second query join between TRANS and BALANCWES is not there. please note it. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-12 : 02:55:03
|
| do as i suggested by replacing the extra join with left join and case expression in the select------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dsscholar
Starting Member
6 Posts |
Posted - 2011-09-12 : 14:55:54
|
| Logic is wrong.Please check it once. It produce wrong results. I did exactly as u said.Thanks. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-13 : 00:28:19
|
quote: Originally posted by dsscholar Logic is wrong.Please check it once. It produce wrong results. I did exactly as u said.Thanks.
do you want somone to check your logic and fix your code? we dont have much idea so i dont think anybody will take the pain to do that.as i suggested try to rewrite query,test and fix your logic. if you're having issues in implemeting any particular scenario then post it and somebody will help you out------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|