Discussion
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!
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!
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.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!
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...
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


