SQL help?
Author
Discussion

Thudd

Original Poster:

3,100 posts

238 months

Friday 24th August 2007
quotequote all
I don't do this sort of thing very often and am being crap.

MS SQL. I have a (temp) table which looks like:

place datetime number
place datetime number
place datetime number

And I want
Place mondaymaxnumber tuesdaymaxnumber wednesdaymaxnumber etc.

If I do:

select place, (select max(count00) where datename(dw,datetim)='Monday')as Monday,
(select max(count00) where datename(dw,datetim)='Tuesday') as Tuesday,
(select max(count00) where datename(dw,datetim)='Wednesday') as Wednesday,
(select max(count00) where datename(dw,datetim)='Thursday') as Thursday,
(select max(count00) where datename(dw,datetim)='Friday') as Friday,
(select max(count00) where datename(dw,datetim)='Saturday') as Saturday,
(select max(count00) where datename(dw,datetim)='Sunday') as Sunday
FROM @TABLE
group by place, datename (dw,datetim)
ORDER BY place


and count00 is my number field, I get a load of crap:

Place1 56 NULL NULL NULL NULL NULL NULL
Place1 NULL 60 NULL NULL NULL NULL NULL
Place1 NULL NULL 56 NULL NULL NULL NULL
Place1 NULL NULL NULL 55 NULL NULL NULL
Place1 NULL NULL NULL NULL 39 NULL NULL

etc instead of all the numbers for "Place1" on the one line.

Where am I going wrong?

help!

wiggy001

7,404 posts

302 months

Sunday 26th August 2007
quotequote all
Thudd said:
I don't do this sort of thing very often and am being crap.

MS SQL. I have a (temp) table which looks like:

place datetime number
place datetime number
place datetime number

And I want
Place mondaymaxnumber tuesdaymaxnumber wednesdaymaxnumber etc.

If I do:

select place, (select max(count00) where datename(dw,datetim)='Monday')as Monday,
(select max(count00) where datename(dw,datetim)='Tuesday') as Tuesday,
(select max(count00) where datename(dw,datetim)='Wednesday') as Wednesday,
(select max(count00) where datename(dw,datetim)='Thursday') as Thursday,
(select max(count00) where datename(dw,datetim)='Friday') as Friday,
(select max(count00) where datename(dw,datetim)='Saturday') as Saturday,
(select max(count00) where datename(dw,datetim)='Sunday') as Sunday
FROM @TABLE
group by place, datename (dw,datetim)
ORDER BY place


and count00 is my number field, I get a load of crap:

Place1 56 NULL NULL NULL NULL NULL NULL
Place1 NULL 60 NULL NULL NULL NULL NULL
Place1 NULL NULL 56 NULL NULL NULL NULL
Place1 NULL NULL NULL 55 NULL NULL NULL
Place1 NULL NULL NULL NULL 39 NULL NULL

etc instead of all the numbers for "Place1" on the one line.

Where am I going wrong?

help!
You need to link your inline queries (select max... etc) to the main statement by using an alias for the main table and adding "and alias.keyfield = keyfield" to each of your inline statements. Otherwise you are just getting the max number for Monday, rather than the max number for Monday for each place.

So something like:

select place, (select max(count00) where datename(dw,datetim)='Monday' and A.place = place)as Monday,
(select max(count00) where datename(dw,datetim)='Tuesday' and A.place = place) as Tuesday,
(select max(count00) where datename(dw,datetim)='Wednesday' and A.place = place) as Wednesday,
(select max(count00) where datename(dw,datetim)='Thursday' and A.place = place) as Thursday,
(select max(count00) where datename(dw,datetim)='Friday' and A.place = place) as Friday,
(select max(count00) where datename(dw,datetim)='Saturday' and A.place = place) as Saturday,
(select max(count00) where datename(dw,datetim)='Sunday' and A.place = place) as Sunday
FROM @TABLE A
group by place, datename (dw,datetim)
ORDER BY place

Hope that makes sense...