Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Transact SQL teaser

 
   Database Help (Home) -> Server RSS
Next:  sync combox boxes  
Author Message
Jocke

External


Since: Mar 10, 2010
Posts: 2



(Msg. 1) Posted: Wed Mar 10, 2010 11:11 am
Post subject: Transact SQL teaser
Archived from groups: microsoft>public>sqlserver>server (more info?)

Hi Gurus!
I need your immediate help with a brainteaser…
My problem:
I need a setbased SQL for loading a Data warehouse. I have a dataflow with
many rows per category. These rows should be grouped per date interval and ID
and category. min (from_date) and max (to_date) should be delivered as in the
result below.

I can easily handle this with a cursor, but that’s to slow…

What would you suggest?

Best, Jocke

The example:
ID Category from_date to_date
1691789 43 2004-09-01 00:00:00.000 2005-03-01 00:00:00.000
1691789 43 2005-03-01 00:00:00.000 2005-09-01 00:00:00.000
1691789 43 2005-09-01 00:00:00.000 2007-08-02 00:00:00.000
1691789 43 2007-08-02 00:00:00.000 2007-08-30 00:00:00.000
1691789 28 2007-08-30 00:00:00.000 2008-08-26 00:00:00.000
1691789 28 2008-08-26 00:00:00.000 2008-09-01 00:00:00.000
1691789 28 2008-09-01 00:00:00.000 2008-11-06 00:00:00.000
1691789 28 2008-11-06 00:00:00.000 2009-07-23 00:00:00.000
1691789 43 2009-07-23 00:00:00.000 2009-09-01 00:00:00.000
1691789 43 2009-09-01 00:00:00.000 2010-09-01 00:00:00.000
2222222 1 2008-08-26 00:00:00.000 2008-09-01 00:00:00.000


Should result in:
ID Category from_date to_date
1691789 43 2004-09-01 00:00:00.000 2007-08-30 00:00:00.000
1691789 28 2007-08-30 00:00:00.000 2009-07-23 00:00:00.000
1691789 43 2009-07-23 00:00:00.000 2010-09-01 00:00:00.000
2222222 1 2008-08-26 00:00:00.000 2008-09-01 00:00:00.000

 >> Stay informed about: Transact SQL teaser 
Back to top
Login to vote
Plamen Ratchev

External


Since: Aug 20, 2008
Posts: 672



(Msg. 2) Posted: Wed Mar 10, 2010 5:12 pm
Post subject: Re: Transact SQL teaser [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Here is one set based solution:

SELECT id, category, from_date,
(SELECT MIN(to_date)
FROM Foo AS C
WHERE NOT EXISTS
(SELECT *
FROM Foo AS D
WHERE C.id = D.id
AND C.category = D.category
AND C.to_date >= DATEADD(DAY, -1, D.from_date)
AND C.to_date < D.to_date)
AND C.to_date >= A.from_date
AND C.id = A.id
AND C.category = A.category) AS to_date
FROM Foo AS A
WHERE NOT EXISTS
(SELECT *
FROM Foo AS B
WHERE A.id = B.id
AND A.category = B.category
AND A.from_date > B.from_date
AND A.from_date <= DATEADD(DAY, 1, B.to_date));

--
Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: Transact SQL teaser 
Back to top
Login to vote
Jocke

External


Since: Mar 10, 2010
Posts: 2



(Msg. 3) Posted: Wed Mar 10, 2010 10:37 pm
Post subject: Re: Transact SQL teaser [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Case closed!

Thanks Planen!
Your solution works fine!
I worked paralell with you and my solution resulted in this code:


Select ID,
category = min(category),
from_date = Min(from_date),
to_date = max(to_date)
from
(SELECT ID,
category,
from_date,
to_date,
-- I create a dummycolumn (Bryt) that gives a "group identity" for those
rows that should have a low/high-date
-- since the last "group" will have a NULL-value I use coalesce to replce
the null value with the IDs Max to_date
Bryt = coalesce((select min(from_date) from Role1 x where x.ID = a.ID
and x.from_date > a.from_date
and x.category <> a.category),
(select max(to_date) from Role1 x where x.ID = a.ID))
FROM role1 a) z
group by ID, Bryt -- I use the dummycolumn as group by
order by 1,3

// Jocke



"Plamen Ratchev" wrote:

> Here is one set based solution:
>
> SELECT id, category, from_date,
> (SELECT MIN(to_date)
> FROM Foo AS C
> WHERE NOT EXISTS
> (SELECT *
> FROM Foo AS D
> WHERE C.id = D.id
> AND C.category = D.category
> AND C.to_date >= DATEADD(DAY, -1, D.from_date)
> AND C.to_date < D.to_date)
> AND C.to_date >= A.from_date
> AND C.id = A.id
> AND C.category = A.category) AS to_date
> FROM Foo AS A
> WHERE NOT EXISTS
> (SELECT *
> FROM Foo AS B
> WHERE A.id = B.id
> AND A.category = B.category
> AND A.from_date > B.from_date
> AND A.from_date <= DATEADD(DAY, 1, B.to_date));
>
> --
> Plamen Ratchev
> http://www.SQLStudio.com
> .
>
 >> Stay informed about: Transact SQL teaser 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
There isn't a "Format" function in Transact-SQL!!! - Hello all, There doesn't appear to be a "Format" function in Transact-SQL (like the Format function in VBA or the Text function in XL), so that I can format my numbers any way I like (and turn them into strings). Please correct me if I'm w...

How to get the server's IP address using Transact-SQL? - Is there a way to get the server's IP address using Transact-SQL? Thanks, Felipe

Insert output of procedure inside table - Hello, Can you tell me how can i put the output if the procedure sp_helpdb inside one table? Thanks and best regards

SQL Agent won't start - Our installation of SQLServer 2000 has been working fine until last week when SQLAgent cannot start due to a login problem with SQLServer. SQLAgent properties have not changed since the problem began, but the event log reports that SQLAgent cannot log...

Format the column with - Hello, Can you tell me how can i format the column with in the output of a query? The results must be returned as text and my output is like this: y x -------------------- ----------------------- 1 2 the way im treating the output is to..
   Database Help (Home) -> Server All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]