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 |
|
reddymade
Posting Yak Master
165 Posts |
Posted - 2005-11-28 : 13:12:47
|
| Select query using Top and distinct both is it possibleI am using the following query, and the resultset contains duplicate records with same CNID. All i am trying to do is show me the last three records updated by me, using the datetime of update_dt field, i am getting the records from Log table, it will have the history of all the changes done for all records.SELECT Top 3 CNID, Description,'CN' as Type,update_dtFROM tab_ccsnetcn_log where updatedby = 'thisuser' ORDER BY update_dt DESCI am getting the following result: it shows two record with same CNID: 149CNID Description Type Update_dt147 This isprocdatetest CN 2005-11-21 12:27:45.827149 testtitleLagos CN 2005-11-21 11:48:02.670149 testtitleLagos CN 2005-11-21 11:47:47.950 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-11-28 : 13:37:31
|
| select t.CNID, t.Description,'CN' as Type,t.update_dtfrom tab_ccsnetcn_log tjoin (select top 3 CNID, update_dt = max(update_dt) from tab_ccsnetcn_log where updatedby = 'thisuser' group by CNID order by update_dt desc) t2on t.CNID = t2.CNIDand t.update_dt = t2.update_dt==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
reddymade
Posting Yak Master
165 Posts |
Posted - 2005-11-28 : 14:13:35
|
Hello NR,Thank you very much, it works great.Can you please tell me, I want to do a UNION with 5 select querys with same fields etc. but with different tables:Can you please tell me how to use a Union with your solution.this is the next query:select t.RFID, t.Description,'RF' as Type,t.update_dtfrom tab_ccsnetRF_log tjoin (select top 3 RFID, update_dt = max(update_dt) from tab_ccsnetRF_log where updatedby = 'thisuser' group by RFID order by update_dt desc) t2on t.RFID = t2.RFIDand t.update_dt = t2.update_dtThank you very much for the help.quote: Originally posted by nr select t.CNID, t.Description,'CN' as Type,t.update_dtfrom tab_ccsnetcn_log tjoin (select top 3 CNID, update_dt = max(update_dt) from tab_ccsnetcn_log where updatedby = 'thisuser' group by CNID order by update_dt desc) t2on t.CNID = t2.CNIDand t.update_dt = t2.update_dt==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
|
 |
|
|
druer
Constraint Violating Yak Guru
314 Posts |
Posted - 2005-11-28 : 14:54:38
|
| Reddymade,UNIONS are very easy and the basic format is simply:Select whatever you wantUNIONSelect whatever else you wantUNION Select whatever else you wantThere are 2 types of UNIONS. The default is a DISTINCT type where it will combine all of the distinct values from all of your select clauses. This type causes a performance hit because it has to compare all of the result sets. The second type has to be identified by you which is UNION ALL and defines that you want all of the results from all of the selects and don't need to have any DISTINCT executed.Select whatever you wantUNION ALLselect whatever else you wantYou would simply run the exact query that they gave you, then put the words UNION ALL and then the complete query for the second table etc, through all 5 of your tables. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|
|