Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
feejaz
Yak Posting Veteran
68 Posts |
Posted - 2008-07-01 : 09:42:35
|
| HI ALL,I HAVE CREATED A QUERY LIKE BELOW AND FOUND ERRORIncorrect syntax near the keyword 'union'.select top 1 [Call].*, unit.unitname FROM [Call] left join unit on [Call].unitid=unit.unitid where CheckCall.unitid= 5 order by CallDate desc union SELECT top 1 [Call].*, unit.unitname FROM [Call] left join unit on [Call].unitid=unit.unitid where Call.unitid= 7 order by CallDate desc BUT WHEN I COMMIT THE FIRST ORDER BY COMMAND THE ERROR IS CLEAR BUT THE RECORD ARE NOT IN DESC ORDER. ANY HELP.Navi |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-01 : 09:44:37
|
[code]select *from(select top 1 [Call].*, unit.unitname FROM [Call]left join unit on [Call].unitid=unit.unitidwhere CheckCall.unitid= 5order by CallDate desc) aunionselect *from(SELECT top 1 [Call].*, unit.unitname FROM [Call]left join unit on [Call].unitid=unit.unitidwhere Call.unitid= 7order by CallDate desc ) b[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-01 : 12:57:23
|
quote: Originally posted by khtan
select *from(select top 1 [Call].*, unit.unitname FROM [Call]left join unit on [Call].unitid=unit.unitidwhere CheckCall.unitid= 5order by CallDate desc) aunionselect *from(SELECT top 1 [Call].*, unit.unitname FROM [Call]left join unit on [Call].unitid=unit.unitidwhere Call.unitid= 7order by CallDate desc ) b KH[spoiler]Time is always against us[/spoiler]
is there really a need of union here? is this equivalent toSELECT [Call].*, unit.unitname FROM [Call] cinner join (select pk,max(calldate) as maxdate from call group by pk)ton t.pk=c.pkand t.maxdate=c.calldateleft join unit u on c.unitid=u.unitidwhere c.unitid in(5,7) where pk is primary key of call table |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-07-01 : 14:53:47
|
quote: Originally posted by visakh16
quote: Originally posted by khtan
select *from(select top 1 [Call].*, unit.unitname FROM [Call]left join unit on [Call].unitid=unit.unitidwhere CheckCall.unitid= 5order by CallDate desc) aunionselect *from(SELECT top 1 [Call].*, unit.unitname FROM [Call]left join unit on [Call].unitid=unit.unitidwhere Call.unitid= 7order by CallDate desc ) b KH[spoiler]Time is always against us[/spoiler]
is there really a need of union here? is this equivalent toSELECT [Call].*, unit.unitname FROM [Call] cinner join (select pk,max(calldate) as maxdate from call group by pk)ton t.pk=c.pkand t.maxdate=c.calldateleft join unit u on c.unitid=u.unitidwhere c.unitid in(5,7) where pk is primary key of call table
Also add order by c.unitid, c.calldate descMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|