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 |
|
giviglie
Starting Member
5 Posts |
Posted - 2010-03-22 : 06:46:34
|
| Hi...I have two tables...First Table "Event"ID | EventName |1 Event12 Event2Second Table "Period"ID | IDEvent | DateFrom | DateTo |1 1 01/01/2010 31/01/2010 2 1 01/03/2010 31/03/20103 2 01/02/2010 15/02/2010How can I get the 2 records orded by DateFrom whit a SELECT?If I try with a join I can order them but I get 3 records. If I try with a DISTINCT I can get two records but without dates and I can't order...Any help will be very appreciated...CheersGiacomoIn a situation like this |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-22 : 06:54:01
|
How is it possible that one event has two different Dates?What should be the output regarding your sample data? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-22 : 07:03:56
|
quote: Originally posted by giviglie Hi...I have two tables...First Table "Event"ID | EventName |1 Event12 Event2Second Table "Period"ID | IDEvent | DateFrom | DateTo |1 1 01/01/2010 31/01/2010 2 1 01/03/2010 31/03/20103 2 01/02/2010 15/02/2010How can I get the 2 records orded by DateFrom whit a SELECT?If I try with a join I can order them but I get 3 records. If I try with a DISTINCT I can get two records but without dates and I can't order...Any help will be very appreciated...CheersGiacomoIn a situation like this
try this.it is just my guess,you need this.select e.id,e.eventname,t.idevent,t.datefrom,t.dateto from Event as eOUTER APPLY(select top 1 IDEvent,datefrom,dateto from period where id=e.id order by DateFrom )as t edit:Added code tag |
 |
|
|
giviglie
Starting Member
5 Posts |
Posted - 2010-03-22 : 07:05:08
|
| Hi WEBFRED!The event could have different beginning dates...for example a simple local market that begins every first monday of the month and ends after a couple of days...OUTPUTEvent1Event2Because the Event1 has the first beginning date lower than Event2ThanxsGiacomo |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-22 : 07:07:42
|
| Have you tried my sample code. |
 |
|
|
giviglie
Starting Member
5 Posts |
Posted - 2010-03-22 : 07:21:09
|
| Hi Haroon2k9!Yes I've tried it and it works great! Thanks...brillant solution!Cheers Giacomo |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-22 : 07:25:07
|
quote: Originally posted by giviglie Hi Haroon2k9!Yes I've tried it and it works great! Thanks...brillant solution!Cheers Giacomo
Welcome |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-22 : 11:38:24
|
SELECT EventName, IDEvent ,DateFrom , DateToFROM(SELECT ROW_NUMBER() OVER(PARTITION BY p.IDEvent ORDER BY p.DateFrom ASC) AS Seq,e.EventName, p.IDEvent ,p.DateFrom , p.DateToFROM Event eJOIN Period pON p.IDEvent = e.ID)tWHERE Seq=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|