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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AartiRank #2,383 · 594 reputationPosted Nov 16, 2011, 12:17 AM EST
Please Describe your question in detail.
as i am getting .. you can use ALIAS....
Thanks.
Karthik AgarwalRank #2,270 · 620 reputationPosted Nov 16, 2011, 12:18 AM EST
private void GetRowsByFilter() { DataTable table = DataSet1.Tables["Orders"]; // Presuming the DataTable has a column named Date. string expression; expression = "Date > #1/1/00#"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = table.Select(expression); // Print column 0 of each returned row. for(int i = 0; i < foundRows.Length; i ++) { Console.WriteLine(foundRows[i][0]); } }http://msdn.microsoft.com/en-us/library/det4aw50%28v=VS.90%29.aspx
http://idealprogrammer.com/net-languages/code-samples/select-statement-source-code/
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 16, 2011, 12:57 AM EST
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.
NelRank #2,060 · 702 reputationPosted Nov 16, 2011, 12:58 AM EST
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 MoreRank #1,419 · 1.2K reputationPosted Nov 16, 2011, 1:09 AM EST
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.
NelRank #2,060 · 702 reputationPosted Nov 16, 2011, 1:28 AM EST
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 MoreRank #1,419 · 1.2K reputationPosted Nov 16, 2011, 1:55 AM EST
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.
NelRank #2,060 · 702 reputationPosted Nov 16, 2011, 2:18 AM EST
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 16, 2011, 2:41 AM EST
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"
NelRank #2,060 · 702 reputationPosted Nov 16, 2011, 3:12 AM EST
oleDBDataAdapter1.SelectCommand=command;
Pravin MoreRank #1,419 · 1.2K reputationPosted Nov 16, 2011, 3:18 AM EST
brackets around the [ FieldName ] .
try n let me know...
NelRank #2,060 · 702 reputationPosted Nov 16, 2011, 3:44 AM EST
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.
NelRank #2,060 · 702 reputationPosted Nov 16, 2011, 10:45 AM EST