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 |
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2007-12-07 : 13:30:21
|
| Hi,I'm currently have a problem with a query using a top clause. When I run it by itself as a single query, I have no problems and the results are valid. However, if I try duplicate the query after a union clause, the order by ... desc doesn't order properly.The following is the query I'm using along with the results. Then I'll have the query I was trying to unite and the results (date ranges selected were the same in both):QUERY 1select top 1 (s.ldate), v.mdtid, po.odometer, pi.odometer, (pi.odometer-po.odometer) as 'Total Miles'from EventStrings ES JOIN schedules s ON ES.SchId=S.SchId JOIN vehicles v ON v.vehicleid=es.vehicleid JOIN Events PO ON PO.schid=es.schid AND PO.EvStrId=ES.EvStrId AND po.activity=4 JOIN Events PI ON PI.schid=es.schid AND PI.EvStrId=ES.EvStrId AND pi.activity=3WHERE es.providerid in (0,1,4)and s.ldate>=?and s.ldate<=?and v.mdtid=20411order by s.ldate descRESULTS 1DATE MDT ID PU Odometer DO Odometer Total Miles12/6/2007 20411 12810.6 12874.5 63.9QUERY 2 (with Union)select top 1 (s.ldate), v.mdtid, po.odometer, pi.odometer, (pi.odometer-po.odometer) as 'Total Miles'from EventStrings ES JOIN schedules s ON ES.SchId=S.SchId JOIN vehicles v ON v.vehicleid=es.vehicleid JOIN Events PO ON PO.schid=es.schid AND PO.EvStrId=ES.EvStrId AND po.activity=4 JOIN Events PI ON PI.schid=es.schid AND PI.EvStrId=ES.EvStrId AND pi.activity=3WHERE es.providerid in (0,1,4)and s.ldate>=[From Date,Date]and s.ldate<=[To Date,Date]and v.mdtid=20411Unionselect top 1 (s.ldate), v.mdtid, po.odometer, pi.odometer, (pi.odometer-po.odometer) as 'Total Miles'from EventStrings ES JOIN schedules s ON ES.SchId=S.SchId JOIN vehicles v ON v.vehicleid=es.vehicleid JOIN Events PO ON PO.schid=es.schid AND PO.EvStrId=ES.EvStrId AND po.activity=4 JOIN Events PI ON PI.schid=es.schid AND PI.EvStrId=ES.EvStrId AND pi.activity=3WHERE es.providerid in (0,1,4)and s.ldate>=?and s.ldate<=?and v.mdtid=2642order by s.ldate descRESULTS 2DATE MDT ID PU Odometer DO Odometer Total Miles4/10/2007 20411 1207.2 1252.5 45.31/2/2007 2642 193652.6 193817 164.4As you can see, the results are sorted very differently. Is there any way to have the order by apply to both queries?Thanks!Craig |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2007-12-07 : 13:57:58
|
| select * from ( select top 1 s.ldate, v.mdtid, po.odometer, pi.odometer, (pi.odometer-po.odometer) as 'Total Miles' from EventStrings ES JOIN schedules s ON ES.SchId=S.SchId JOIN vehicles v ON v.vehicleid=es.vehicleid JOIN Events PO ON PO.schid=es.schid AND PO.EvStrId=ES.EvStrId AND po.activity=4 JOIN Events PI ON PI.schid=es.schid AND PI.EvStrId=ES.EvStrId AND pi.activity=3 WHERE es.providerid in (0,1,4) and s.ldate>=[From Date,Date] and s.ldate<=[To Date,Date] and v.mdtid=20411 order by s.ldate descUnion select top 1 s.ldate, v.mdtid, po.odometer, pi.odometer, (pi.odometer-po.odometer) as 'Total Miles' from EventStrings ES JOIN schedules s ON ES.SchId=S.SchId JOIN vehicles v ON v.vehicleid=es.vehicleid JOIN Events PO ON PO.schid=es.schid AND PO.EvStrId=ES.EvStrId AND po.activity=4 JOIN Events PI ON PI.schid=es.schid AND PI.EvStrId=ES.EvStrId AND pi.activity=3 WHERE es.providerid in (0,1,4) and s.ldate>=? and s.ldate<=? and v.mdtid=2642 order by s.ldate desc) a order by ldate desc"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2007-12-07 : 14:12:37
|
| jhocutt, Thanks for the reply. I tried your query on both interfaces that I have to work with and got the following errors:1stSQLExecDirect returned SQL_ERROR (from HSTMT), SqlState=37000, Native Error=16945, CODBC_ERROR=0Message=[Microsoft][ODBC SQL Server Driver][SQL Server]The cursor was not declared.2nd[Microsoft][ODBC SQL Server Driver]The column 'odometer' was specified multiple times for 'a'Any ideas? |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2007-12-07 : 14:18:51
|
| That is what I get for not looking closerselect * from (select top 1 s.ldate as LDATE, v.mdtid as MTID, po.odometer as PO_ODOMETER, pi.odometer as PI_ODOMETER, (pi.odometer-po.odometer) as 'Total Miles'from EventStrings ESJOIN schedules sON ES.SchId=S.SchIdJOIN vehicles vON v.vehicleid=es.vehicleidJOIN Events POON PO.schid=es.schidAND PO.EvStrId=ES.EvStrIdAND po.activity=4JOIN Events PION PI.schid=es.schidAND PI.EvStrId=ES.EvStrIdAND pi.activity=3WHERE es.providerid in (0,1,4)and s.ldate>=[From Date,Date]and s.ldate<=[To Date,Date]and v.mdtid=20411order by s.ldate descUnionselect top 1 s.ldate as LDATE, v.mdtid as MTID, po.odometer as PO_ODOMETER, pi.odometer as PI_ODOMETER, (pi.odometer-po.odometer) as 'Total Miles'from EventStrings ESJOIN schedules sON ES.SchId=S.SchIdJOIN vehicles vON v.vehicleid=es.vehicleidJOIN Events POON PO.schid=es.schidAND PO.EvStrId=ES.EvStrIdAND po.activity=4JOIN Events PION PI.schid=es.schidAND PI.EvStrId=ES.EvStrIdAND pi.activity=3WHERE es.providerid in (0,1,4)and s.ldate>=?and s.ldate<=?and v.mdtid=2642order by s.ldate desc) a order by LDATE desc"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2007-12-07 : 14:28:09
|
| jhocutt,That fixed the error, but then I get the incorrect order by again.Although the correct results should be:DATE MDT ID PU Odometer DO Odometer Total Miles12/6/2007 20411 12810.6 12874.5 63.902/28/2007 2642 197868.50 197833.50 35.00It returns the following results:DATE MDT ID PU Odometer DO Odometer Total Miles4/10/2007 20411 1207.2 1252.5 45.32/12/2007 2642 196595.4 196670.9 75.5Any more thoughts? I'm not sure why the order by isn't working in the union?? |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2007-12-07 : 15:02:04
|
| Thanks for your help...looking at your response, I tried something. I added order by s.ldate desc after each query, but before each union and somehow it worked. I thought that the order by had to be after the last union, but I'm not gonna complain! Thanks again for your help. |
 |
|
|
|
|
|
|
|