Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Group By with 2 parameters and datetime and add next dateTime. Expand / Collapse
Posted Saturday, January 24, 2009 10:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 31, 2011 8:28 AM
Points: 27, Visits: 55
Hello everybody,

In first, this is my first topic on your forum(I'm french), but I read more (interesting) articles, this is a very good SQL website and forum ;)

My probem :
Since last Week, I have a problem with data and I don't try to resolve it, but when I read your newsletter and the "Linking to the Previous Row" I thinks this is the good way, so I hope that you can help me, because I don't try it by myself ;)
(I'm on SQLServer2000, not 2005 so I cannot use the CTE.)

An exemple of data :
(I have add "id" column but only to help comprehension, in my process table, I have only dT as primary key, thus I cannot order by Id)
--===== If the test table already exists, drop it
IF OBJECT_ID('TempDB..#mytableDamien','U') IS NOT NULL
DROP TABLE #mytableDamien

--===== Create the test table with
CREATE TABLE #mytableDamien
param1 VARCHAR(8),
param2 VARCHAR(8)

--===== Setup any special required conditions especially where dates are concerned

--===== All Inserts into the IDENTITY column

INSERT INTO #mytableDamien
(id, dT, param1, param2)
SELECT '1','2009-01-13 18:46:02.000','p1_5','1' UNION ALL
SELECT '2','2009-01-13 16:07:38.000','p1_5','1' UNION ALL
SELECT '3','2009-01-12 16:17:23.000','p1_3','1' UNION ALL
SELECT '4','2009-01-12 13:34:07.000','p1_3','1' UNION ALL
SELECT '5','2009-01-12 12:49:31.000','p1_2','1' UNION ALL
SELECT '6','2009-01-11 18:52:09.000','p1_5','1' UNION ALL
SELECT '7','2009-01-11 18:51:49.000','p1_5','2' UNION ALL
SELECT '8','2009-01-11 17:54:51.000','p1_5','2' UNION ALL
SELECT '9','2009-01-11 14:57:09.000','p1_3','2' UNION ALL
SELECT '10','2009-01-10 14:25:05.000','p1_3','2' UNION ALL
SELECT '11','2009-01-10 14:14:59.000','p1_1','2' UNION ALL
SELECT '12','2009-01-08 22:08:26.000','p1_5','1'


--===== Data selection
SELECT id, dT, param1, param2 FROM #mytableDamien ORDER BY dT DESC

-01--2009-01-13 18:46:02.000---p1_5-----1
-02--2009-01-13 16:07:38.000---p1_5-----1
-03--2009-01-12 16:17:23.000---p1_3-----1
-04--2009-01-12 13:34:07.000---p1_3-----1
-05--2009-01-12 12:49:31.000---p1_2-----1
-06--2009-01-11 18:52:09.000---p1_5-----1
-07--2009-01-11 18:51:49.000---p1_5-----2
-08--2009-01-11 17:54:51.000---p1_5-----2
-09--2009-01-11 14:57:09.000---p1_3-----2
-10--2009-01-10 14:25:05.000---p1_3-----2
-11--2009-01-10 14:14:59.000---p1_1-----2
-12--2009-01-08 22:08:26.000---p1_5-----1

I want to GROUP BY param1, Param2, but only when an other couple of Param1, Param2 exists between it. So, the Dt of result will be the start/first date time. There is no sequentiel logic on the Dt, it's possible to have only few minuts between 2 rows, or X days...

The result of my request will be :
-02--2009-01-13 16:07:38.000---p1_5-----1
-04--2009-01-12 13:34:07.000---p1_3-----1
-05--2009-01-12 12:49:31.000---p1_2-----1
-06--2009-01-11 18:52:09.000---p1_5-----1
-08--2009-01-11 17:54:51.000---p1_5-----2
-10--2009-01-10 14:25:05.000---p1_3-----2
-11--2009-01-10 14:14:59.000---p1_1-----2
-12--2009-01-08 22:08:26.000---p1_5-----1

Moreover, I want to add the end date time witch will be the Dt of the first next row of group by Param1, Param2 and order by Dt DESC.
So the result will be :
-02--2009-01-13 16:07:38.000---GETDATE()-----------------p1_5-----1
-04--2009-01-12 13:34:07.000---2009-01-13 16:07:38.000---p1_3-----1
-05--2009-01-12 12:49:31.000---2009-01-12 13:34:07.000---p1_2-----1
-06--2009-01-11 18:52:09.000---2009-01-12 12:49:31.000---p1_5-----1
-08--2009-01-11 17:54:51.000---2009-01-11 18:52:09.000---p1_5-----2
-10--2009-01-10 14:25:05.000---2009-01-11 17:54:51.000---p1_3-----2
-11--2009-01-10 14:14:59.000---2009-01-10 14:25:05.000---p1_1-----2
-12--2009-01-08 22:08:26.000---2009-01-10 14:14:59.000---p1_5-----1

An example would be the appropriate measurements using equipment from a not fixed time(minuts, hours or days) on 2 characteristics (Param1 and Param2) of a product, and I would like to response at question : "Was type of products was manufactured between a given time (Between dT) and the order of production with the period (startdT and endDt) ?

I hope that you will be help me because I don't find the good way by myself,
Best regards,

PS :
I hope that I have a "good english" and that you understand me as well ;)
Post #642999
Posted Saturday, January 24, 2009 7:10 PM



Group: General Forum Members
Last Login: Today @ 6:44 AM
Points: 42,035, Visits: 39,412
I believe that the reason you're not getting any bites on this problem is that most people don't want to work so hard trying to test your data. Please see the link in my signature below for the best way to post readily consumable data.

Also, people like to know WHY you need to do something like this. And just saying "It's a requirement" doesn't quite get it. People like to know the business reasons for doing such things because, a lot of times, there's better ways to do things than the way you may be thinking.

And, welcome aboard.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

Helpful Links:
How to post code problems
How to post performance problems
Post #643078
Posted Sunday, January 25, 2009 8:10 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 31, 2011 8:28 AM
Points: 27, Visits: 55
Hello RBAR,

Thanks for your answer and for your link about how post best data/code.
I have edit my first post according it.
Post #643156
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse