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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Order by problem

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 Event1
2 Event2
Second Table "Period"
ID | IDEvent | DateFrom | DateTo |
1 1 01/01/2010 31/01/2010
2 1 01/03/2010 31/03/2010
3 2 01/02/2010 15/02/2010

How 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...
Cheers
Giacomo


In 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.
Go to Top of Page

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 Event1
2 Event2
Second Table "Period"
ID | IDEvent | DateFrom | DateTo |
1 1 01/01/2010 31/01/2010
2 1 01/03/2010 31/03/2010
3 2 01/02/2010 15/02/2010

How 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...
Cheers
Giacomo


In 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 e
OUTER APPLY(select top 1 IDEvent,datefrom,dateto from period where id=e.id order by DateFrom )as t


edit:Added code tag
Go to Top of Page

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...

OUTPUT

Event1
Event2

Because the Event1 has the first beginning date lower than Event2

Thanxs
Giacomo
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-22 : 07:07:42
Have you tried my sample code.
Go to Top of Page

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
Go to Top of Page

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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-22 : 11:38:24


SELECT EventName, IDEvent ,DateFrom , DateTo
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY p.IDEvent ORDER BY p.DateFrom ASC) AS Seq,
e.EventName, p.IDEvent ,p.DateFrom , p.DateTo
FROM Event e
JOIN Period p
ON p.IDEvent = e.ID
)t
WHERE Seq=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -