| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-03-15 : 20:00:01
|
| Hi,I have a fairly simple query as seen below. The problem that I only want to select unique values (based on the long/lat combo). I want to bring back all 4 columns(city,longitude,latitude,viewDate).I also want to mention that eliminating duplicate inserts is not what I am looking to do either.Does anybody know how can I write this query?Thanks very much!mike123SELECT TOP 450 city, longitude, latitude, viewDate FROM tblMapViews_Log |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-03-15 : 20:04:14
|
| So where's the sample data and expected result set? With over 700 posts, you should know the drill.You probably will need a GROUP BY with a derived table.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-03-15 : 20:24:55
|
| Hey Tara,Sorry I wasn't sure how simple or complex this would be. Let me type up some sample data right now for you.Hopefully this helps! Sample Data:VANCOUVER -123.133000 49.250000 2007-01-18 14:21:00VANCOUVER -123.133000 49.250000 2007-01-18 15:00:00PERTH 115.833000 -31.933000 2007-01-18 15:18:00VANCOUVER -123.133000 49.250000 2007-01-18 15:20:00HERNDON -77.391100 38.942400 2007-01-18 15:27:00GUILDFORD -.575000 51.235000 2007-01-17 08:24:00ST. LOUIS -90.283100 38.635500 2007-01-18 15:43:00NORTH VANCOUVER -123.067000 49.317000 2007-01-18 15:59:00RESTON -77.344200 38.962300 2007-01-18 14:23:00NASHVILLE -86.787800 36.153800 2007-01-17 08:32:00NASHVILLE -86.787800 36.153800 2007-01-17 08:33:00NASHVILLE -86.787800 36.153800 2007-01-17 08:34:00NASHVILLE -86.787800 36.153800 2007-01-17 08:36:00SEATTLE -122.330000 47.601400 2007-01-18 14:23:00Desired Result Set:(all duplicate long/lat values are filtered) If it comes down to only the viewDate column as being different, the max can be selected or if its not too much extra work on the query. Otherwise it actually is not too big of a deal which result is returned. This column MIGHT be dropped on the return.Hope this helps.Thanks very much!mike123VANCOUVER -123.133000 49.250000 2007-01-18 15:20:00PERTH 115.833000 -31.933000 2007-01-18 15:18:00HERNDON -77.391100 38.942400 2007-01-18 15:27:00GUILDFORD -.575000 51.235000 2007-01-17 08:24:00ST. LOUIS -90.283100 38.635500 2007-01-18 15:43:00NORTH VANCOUVER -123.067000 49.317000 2007-01-18 15:59:00RESTON -77.344200 38.962300 2007-01-18 14:23:00NASHVILLE -86.787800 36.153800 2007-01-17 08:36:00SEATTLE -122.330000 47.601400 2007-01-18 14:23:00 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-03-15 : 23:44:14
|
| Perhaps it's just me, but I can't read data when it's like this, especially since no code tags were used to retain formatting. Please post insert into statements for your data into your table plus the create table statement for the table.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-03-16 : 02:17:09
|
| Hey Guys,Sorry about the confusing post. Here is the create table, insert values, and desired output. Please let me know if any more questions. Your helps much appreciated!thanks,mike123declare @a table (city varchar(50), latitude numeric (18,6), longitude numeric (18,6), viewDate datetime)insert @aselect 'VANCOUVER', -123.133000, 49.250000,'2007-01-18 14:21:00' union allselect 'VANCOUVER', -123.133000, 49.250000,'2007-01-18 15:00:00' union allselect 'PERTH', 115.833000, -31.933000,'2007-01-18 15:18:00' union allselect 'VANCOUVER', -123.133000, 49.250000,'2007-01-18 15:20:00' union allselect 'HERNDON', -77.391100, 38.942400,'2007-01-18 15:27:00' union allselect 'GUILDFORD', -.575000, 51.235000,'2007-01-17 08:24:00' union allselect 'ST. LOUIS', -90.283100, 38.635500,'2007-01-18 15:43:00' union allselect 'NORTH VANCOUVER', -123.067000, 49.317000,'2007-01-18 15:59:00' union allselect 'RESTON', -77.344200, 38.962300,'2007-01-18 14:21:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:32:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:33:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:34:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:36:00' union allselect 'SEATTLE', -122.330000, 47.601400,'2007-01-18 14:23:00' select * from @a Results should be:select 'PERTH', 115.833000, -31.933000,'2007-01-18 15:18:00' union allselect 'VANCOUVER', -123.133000, 49.250000,'2007-01-18 15:20:00' union allselect 'HERNDON', -77.391100, 38.942400,'2007-01-18 15:27:00' union allselect 'GUILDFORD', -.575000, 51.235000,'2007-01-17 08:24:00' union allselect 'ST. LOUIS', -90.283100, 38.635500,'2007-01-18 15:43:00' union allselect 'NORTH VANCOUVER', -123.067000, 49.317000,'2007-01-18 15:59:00' union allselect 'RESTON', -77.344200, 38.962300,'2007-01-18 14:21:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:36:00' union allselect 'SEATTLE', -122.330000, 47.601400,'2007-01-18 14:23:00' |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-16 : 02:34:01
|
| [code]-- prepare sample datadeclare @a table (city varchar(50), latitude numeric (18,6), longitude numeric (18,6), viewDate datetime)insert @aselect 'VANCOUVER', -123.133000, 49.250000,'2007-01-18 14:21:00' union allselect 'VANCOUVER', -123.133000, 49.250000,'2007-01-18 15:00:00' union allselect 'PERTH', 115.833000, -31.933000,'2007-01-18 15:18:00' union allselect 'VANCOUVER', -123.133000, 49.250000,'2007-01-18 15:20:00' union allselect 'HERNDON', -77.391100, 38.942400,'2007-01-18 15:27:00' union allselect 'GUILDFORD', -.575000, 51.235000,'2007-01-17 08:24:00' union allselect 'ST. LOUIS', -90.283100, 38.635500,'2007-01-18 15:43:00' union allselect 'NORTH VANCOUVER', -123.067000, 49.317000,'2007-01-18 15:59:00' union allselect 'RESTON', -77.344200, 38.962300,'2007-01-18 14:21:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:32:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:33:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:34:00' union allselect 'NASHVILLE', -86.787800, 36.153800,'2007-01-17 08:36:00' union allselect 'SEATTLE', -122.330000, 47.601400,'2007-01-18 14:23:00'-- Show the expected outputSELECT City, Latitude, Longitude, MAX(ViewDate)FROM @aGROUP BY City, Latitude, LongitudeORDER BY City[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-03-16 : 09:28:36
|
thx once again Peso!!! :) works perfect it appears mike123 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-16 : 09:30:50
|
| You do not need numeric (18,6), change to numeric (10,6) instead.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|