Hi,
Could anybody tell me please how to write the select statement where one field can be used under different names depending on the value of other field in the same table?
Thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
NelPosted Nov 16, 2011, 3:45 PM
NelPosted Nov 16, 2011, 8:44 AM
I tried like:
1.case when MAGACIN.SIFRA='0991000' then [MAGACIN.KOL] else null end as 'Gorivo', case when MAGACIN.SIFRA='0993050' then [MAGACIN.KOL] else null end as 'Maslo'
2. case when [MAGACIN.SIFRA]='0991000' then [MAGACIN.KOL] else null end as 'Gorivo', case when [MAGACIN.SIFRA]='0993050' then [MAGACIN.KOL] else null end as 'Maslo'
3.case when [MAGACIN.SIFRA]='0991000' then MAGACIN.KOL else null end as 'Gorivo', case when [MAGACIN.SIFRA]='0993050' then MAGACIN.KOL else null end as 'Maslo'
and I still get the same error in all the cases, in all my tries of this like the first one, then like the 2, and when tried like 3.
Pravin MorePosted Nov 16, 2011, 8:18 AM
brackets around the [ FieldName ] .
try n let me know...
NelPosted Nov 16, 2011, 8:12 AM
oleDBDataAdapter1.SelectCommand=command;
Pravin MorePosted Nov 16, 2011, 7:41 AM
solve......
try like this......
"SELECT NOVI.GBR as gbrnov, NOVI.AB as abnov, NALOG1.DATA as datanov,Min(NALOG1.POCKM) AS MinOfPOCKM, Max(NALOG1.KRAJKM) AS MaxOfKRAJKM,(Max(NALOG1.KRAJKM)-Min(NALOG1.POCKM)) AS RAZLIKA, (MAGACIN.KOL)/100 AS Potr100km,case when MAGACIN.SIFRA='0991000' then MAGACIN.KOL else null end as 'Gorivo', case when MAGACIN.SIFRA='0993050' then MAGACIN.KOL else null end as 'Maslo', NOVI.DATAP AS poslprov, NOVI.DATAS AS poslservis FROM (NALOG1 INNER JOIN MAGACIN ON NALOG1.GBRV=MAGACIN.GBR) INNER JOIN NOVI ON NALOG1.GBRV=NOVI.GBR where ( ( ( (NOVI.GBR)>=? And (NOVI.GBR)<=? ) ) AND ( (NOVI.AB)=? or (NOVI.AB)=?) AND ( ( (NALOG1.DATA)>=?) and ((NALOG1.DATA)<=?) ) ) GROUP BY NOVI.GBR, NOVI.AB, NALOG1.DATA, NOVI.DATAP, NOVI.DATAS"
NelPosted Nov 16, 2011, 7:18 AM
Pravin MorePosted Nov 16, 2011, 6:55 AM
what is datatype of MAGACIN.SIFRA?
if its not string then try like MAGACIN.SIFRA=0991000
in which enviroment you are writing this query i mean un sql editor or c# code?
Thanks,
Pravin.
NelPosted Nov 16, 2011, 6:28 AM
I tried your example Pravin and now I get this error:
IErrorInfo.GetDescription failed with E_FAIL(0x80004005).
and my code looks like this:
SELECT NOVI.GBR as gbrnov, NOVI.AB as abnov, NALOG1.DATA as datanov,Min(NALOG1.POCKM) AS MinOfPOCKM, Max(NALOG1.KRAJKM) AS MaxOfKRAJKM,(Max(NALOG1.KRAJKM)-Min(NALOG1.POCKM)) AS RAZLIKA, (MAGACIN.KOL)/100 AS Potr100km,( case when MAGACIN.SIFRA='0991000' then MAGACIN.KOL else null end as 'Gorivo', case when MAGACIN.SIFRA='0993050' then MAGACIN.KOL else null end as 'Maslo'), NOVI.DATAP AS poslprov, NOVI.DATAS AS poslservis FROM (NALOG1 INNER JOIN MAGACIN ON NALOG1.GBRV=MAGACIN.GBR) INNER JOIN NOVI ON NALOG1.GBRV=NOVI.GBR where ( ( ( (NOVI.GBR)>=? And (NOVI.GBR)<=? ) ) AND ( (NOVI.AB)=? or (NOVI.AB)=?) AND ( ( (NALOG1.DATA)>=?) and ((NALOG1.DATA)<=?) ) ) GROUP BY NOVI.GBR, NOVI.AB, NALOG1.DATA, NOVI.DATAP, NOVI.DATAS"
Pravin MorePosted Nov 16, 2011, 6:09 AM
use case when...then like below query........
select case when a.name='pravin' then a.Address else null end as 'Pravins Address',
case when a.name='Nel' then a.Address else null end as 'Nels Address'
it will work in you case...........
Note:- change sytax according to your enviroment i have written for sql server.
Thanks
Pravin.
NelPosted Nov 16, 2011, 5:58 AM
SELECT NOVI.GBR as gbrnov, NOVI.AB as abnov, NALOG1.DATA as datanov,Min(NALOG1.POCKM) AS MinOfPOCKM, Max(NALOG1.KRAJKM) AS MaxOfKRAJKM,(Max(NALOG1.KRAJKM)-Min(NALOG1.POCKM)) AS RAZLIKA, MAGACIN.KOL AS Gorivo, (MAGACIN.KOL)/100 AS Potr100km, MAGACIN.KOL AS Maslo, NOVI.DATAP AS poslprov, NOVI.DATAS AS poslservis FROM (NALOG1 INNER JOIN MAGACIN ON NALOG1.GBRV=MAGACIN.GBR) INNER JOIN NOVI ON NALOG1.GBRV=NOVI.GBR where ( ( ( (NOVI.GBR)>=? And (NOVI.GBR)<=? ) ) AND ( (NOVI.AB)=? or (NOVI.AB)=?) AND ( ( (NALOG1.DATA)>=?) and ((NALOG1.DATA)<=?) )AND ((MAGACIN.SIFRA)='0991000') AND ((MAGACIN.SIFRA)='0993050') ) GROUP BY NOVI.GBR, NOVI.AB, NALOG1.DATA, MAGACIN.KOL, NOVI.DATAP, NOVI.DATAS
So I suppose I have problem because I use the same column under different aliases depending on the value of SIFRA column in the same table. The dataset remains empty and so is the report.
Before I put the bold text it worked fine.
I hope now it is clearer.
Thanks in advance
Pravin MorePosted Nov 16, 2011, 5:57 AM
you can try this....
select case when a.name='pravin' then a.Address else null end as 'Pravins Address',
case when a.name='Nel' then a.Address else null end as 'Nels Address'
from UserTable
Thanks,
Pravin.
Karthik AgarwalPosted Nov 16, 2011, 5:18 AM
Check these links for more explanation:
http://msdn.microsoft.com/en-us/library/det4aw50%28v=VS.90%29.aspx
http://idealprogrammer.com/net-languages/code-samples/select-statement-source-code/
AartiPosted Nov 16, 2011, 5:17 AM
Please Describe your question in detail.
as i am getting .. you can use ALIAS....
Thanks.