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 |
|
nagham
Starting Member
2 Posts |
Posted - 2008-03-20 : 12:40:53
|
| Hello,can anyone help me on this:I have a table with 3 columns:id,time,descriptionlet's say that I have this set of records:ID TIME DESC1 8:04 aa1 8:05 aa1 8:06 aa2 8:07 ab2 8:08 --1 8:09 bbI need a query that return1 8:04 aa2 8:07 ab1 8:09 bbmeans that from every set of same ID a need the distinct one,coz the first set of "1" refere to same person,the 4th "1" refere to different person.Help me plz |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-03-20 : 13:43:33
|
| There is nothing complex about this. Just use GROUP BY.SELECT id, MIN(time) as time, descriptionFROM YourTableGROUP BY id, descriptionTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
modi_sanjay
Starting Member
9 Posts |
Posted - 2008-03-20 : 14:16:40
|
i think your query not return the desire resultits return 2 8:08 -- 1 8.05 aa 2 8:07 ab 1 8:09 bb quote: Originally posted by tkizer There is nothing complex about this. Just use GROUP BY.SELECT id, MIN(time) as time, descriptionFROM YourTableGROUP BY id, descriptionTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/
Thanks & RegardsSanjay Modi |
 |
|
|
modi_sanjay
Starting Member
9 Posts |
Posted - 2008-03-20 : 14:19:20
|
I have one quick question How you recognize the first set of id "1" is different from the 4th "1"first set of "1" refere to same person,the 4th "1" refere to different person.quote: Originally posted by nagham Hello,can anyone help me on this:I have a table with 3 columns:id,time,descriptionlet's say that I have this set of records:ID TIME DESC1 8:04 aa1 8:05 aa1 8:06 aa2 8:07 ab2 8:08 --1 8:09 bbI need a query that return1 8:04 aa2 8:07 ab1 8:09 bbmeans that from every set of same ID a need the distinct one,coz the first set of "1" refere to same person,the 4th "1" refere to different person.Help me plz
Thanks & RegardsSanjay Modi |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-03-20 : 16:04:43
|
| What version of SQL server are you using? |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-03-20 : 18:51:31
|
| You have to convert the varchar time to a real time first, then it will work.Something like CONVERT(VARCHAR(5),CONVERT(DATETIME,'01/01/1900 ' + 'field that looks like a time but isn't),121)WHERE DESC <> '--'Jim |
 |
|
|
nagham
Starting Member
2 Posts |
Posted - 2008-03-21 : 12:16:27
|
quote: Originally posted by modi_sanjay I have one quick question How you recognize the first set of id "1" is different from the 4th "1"first set of "1" refere to same person,the 4th "1" refere to different person.quote: Originally posted by nagham Hello,can anyone help me on this:I have a table with 3 columns:id,time,descriptionlet's say that I have this set of records:ID TIME DESC1 8:04 aa1 8:05 aa1 8:06 aa2 8:07 ab2 8:08 --1 8:09 bbI need a query that return1 8:04 aa2 8:07 ab1 8:09 bbmeans that from every set of same ID a need the distinct one,coz the first set of "1" refere to same person,the 4th "1" refere to different person.Help me plz
Thanks & RegardsSanjay Modi
well I have nothing help me to know that the first set is different from the other one,I just want that if at the next record the id is the same I don't want to take it when the id is different I take it,that is why I need1 8:042 8:071 8:09 |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-03-21 : 13:00:20
|
| DECLARE @Table TABLE (ID int, [TIME] varchar(5),[DESC] varchar(5))INSERT INTO @TableSELECT 1, '8:04', 'aa' union allSELECT 1,' 8:05', 'aa' union allSELECT 1, '8:06', 'aa' union allSELECT 2, '8:07', 'ab' union allSELECT 2,'8:08', '--' union allSELECT 1,'8:09', 'bb'SELECT ID, CONVERT(varchar(5),MIN(CONVERT(DATETIME,'01/01/1900 ' + [TIME])),108),[DESC]FROM @tableWHERE [DESC] <> '--' GROUP BY ID,[DESC] |
 |
|
|
|
|
|
|
|