Subject | Re: SQL - what am I doing wrong |
---|---|
Author | zlatko.ivankovic |
Post date | 2011-10-04T22:15:26Z |
Hi Set,
select DjelatnikStatus
from KEDjelatnik
where <Search Condition 1>
and <Search Condition 2>
should became
select <SQL Expression> as DjelatnikStatus
from KEDjelatnik
where <Search Condition 1>
and <Search Condition 2>
for one record in table KEDjelatnik I can have 1..n records in table KEStazZap
If I need DjelatnikStatus for one DjelatnikID I would have to calculate DjelatnikID, mdz pairs for all DjelatnikID in table KEStazZap.
As I already wrote to Arno, I didn't see that result of query which returns no records has null value only in context of KEDjelatnik.
Thank you for your answer.
Regards,
Zlatko
> > selectSQL expression above is taken out of the context. I have to change value of field from table KEDjelatnik with calculated value.
> > ( select case when (a.mdz is null) then 'A'
> > when (cast(a.mdz as timestamp) > cast('2011-09-30' as timestamp)) then 'B'
> > when (cast(a.mdz as timestamp) < cast('2011-09-30' as timestamp)) then 'C'
> > else 'D'
> > end
> >
> > from (
> > select max(KEStazZap.stazzapdatumzaposljavanja) as mdz
> > from KEStazZap
> > where KEStazZap.DjelatnikID = KEDjelatnik.DjelatnikID
> > and cast( '2011-09-30' as timestamp) <=
> > cast(KEStazZap.stazzapdatumprekida as timestamp)
> > group by djelatnikID
> > ) a
> >)
> >
> >from
> >KEDjelatnik
> >
> >in results I get Null values where I think it should be 'A'. What am I doing wrong ?
>
select DjelatnikStatus
from KEDjelatnik
where <Search Condition 1>
and <Search Condition 2>
should became
select <SQL Expression> as DjelatnikStatus
from KEDjelatnik
where <Search Condition 1>
and <Search Condition 2>
> Rather, I'd recommend you to try something like:I was considering this approach but I had following problem (not visible from my example SQL):
>
> WITH TMP (djelatnikID, MDZ) as
> (select DjelatnikID, max(cast(stazzapdatumzaposljavanja as TimeStamp))
> from KEStazZap
> where cast(stazzapdatumprekida as timestamp) >= '2011-09-30'
> group by djelatnikID)
>
> SELECT case
> when a.mdz is null then 'A'
> when a.mdz > '2011-09-30' then 'B'
> when a.mdz < '2011-09-30' then 'C'
> else 'D'
> end
> FROM KEDjelatnik K
> LEFT JOIN TMP T ON K.DjelatnikID = T.DjelatnikID
>
for one record in table KEDjelatnik I can have 1..n records in table KEStazZap
If I need DjelatnikStatus for one DjelatnikID I would have to calculate DjelatnikID, mdz pairs for all DjelatnikID in table KEStazZap.
As I already wrote to Arno, I didn't see that result of query which returns no records has null value only in context of KEDjelatnik.
Thank you for your answer.
Regards,
Zlatko