473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

select from row1 to row n

Hey
I have made an sql.
But it is not what I want. I hope someone can help me create the right sql.

Explanation:
Every activity have more activitydetail. ex activity 500

aID act.detailID statusID datetime
c)500 1000 20 2008-04-28
500 1001 19 2008-04-28
500 1002 10 2008-04-28
a) 500 1003 26 2008-05-02
b) 500 1004 19 2008-05-15
500 1005 21 2008-05-15

This sql will output activities over 9 days from a) to b)

I want from c) to b)
from row1
to the row after status 26

Here is my sql:
SELECT
T3.ActivityID,
T4.TimeStamp AS FirstDateTimeStamp,
T5.TimeStamp AS NextDateTimeStamp,
T4.QueueName AS Technican,
DATEDIFF(dd, T4.TimeStamp, T5.TimeStamp) AS Days
FROM
ActivityDetailsAll AS T4 INNER JOIN
(SELECT
T1.ActivityID,
MIN(T2.ActivityDetailID) AS FirstActivityDetailID,
MIN(T1.ActivityDetailID) AS NextActivityDetailID
FROM
ActivityDetailsAll AS T1 INNER JOIN
(SELECT
ActivityID,
MAX(ActivityDetailID) AS ActivityDetailID
FROM
ActivityDetailsAll
WHERE
(TimeStamp BETWEEN '2008-05-01' AND '2008-05-31') AND
(StatusID = 26)
GROUP BY
ActivityID, ActivityDetailID) AS T2 ON T1.ActivityID =
T2.ActivityID
AND T1.ActivityDetailID T2.ActivityDetailID
GROUP BY T1.ActivityID) AS T3 ON T3.FirstActivityDetailID
= T4.ActivityDetailID
INNER JOIN
ActivityDetails AS T5 ON T3.NextActivityDetailID =
T5.ActivityDetailID
WHERE
(DATEDIFF(dd, T4.TimeStamp, T5.TimeStamp) 9)
In other words
a) 20
b) 19
c) 10
d) 26
e) 19
f) 21

want a) to e) (e is just after statusid 26 )

/henrik
Jun 27 '08 #1
6 2564
Henrik Hartig (he**********@varmmail.dk) writes:
Thank you very much for your answer. I haven't tried your solution,
witch I look forward too.
I have done what you tell me to do. Create a table and insert-statements
plus my own sql (that i describe in my first message).
I hope you will make an afford to look agin at my question.
I'm sorry, but I still don't understanding what you want to achieve.
my own sql return this:
205372 2008-05-02 08:04:58.143 2008-05-15
10:43:09.580 13
from b) to c).
What you mean with "from b) to c)"? Your query return two rows,
which makes sense as far that in your list, b and c comes after
each other, but the second row has a different activity id, and
that does not match your list.
I wan't from a) to c) [the row after statusid 26]

a) 853187 205372 20 2008-04-28
12:29:50.627

853465 205372 19 2008-04-28
19:27:05.383

853466 205372 10 2008-04-28
19:27:09.180

b) 855032 205372 26 2008-05-02
08:04:58.143

c) 860439 205372 19 2008-05-15
10:43:09.580

860685 205372 21 2008-05-15
14:27:06.670
So you want to output five rows? But these rows have different columns
from your query produces? Or do you mean that the aggregation is to
include these five rows?

I could probably make some guess of what you really mean, but I would
really prefer if you could make an effort to explain from the beginning
what these columns means and what your query is supposed to achieve.
The query decently complex, and since you say it's wrong I have not dug
into it.

I'm awfully, but if you cannot express your business problem clearly,
it is very difficult to help you. You may know what you are talking
about when you say "from a) to c)", but I don't.

And did you ever say which version of SQL Server you are using?


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #2
I'm using ms sql 2005 server

I will try to explain the case:

The system is used in a workshop that repair computers.
a) 853187 205372 20 2008-04-28
12:29:50.627

853465 205372 19 2008-04-28
19:27:05.383

853466 205372 10 2008-04-28
19:27:09.180

b) 855032 205372 26 2008-05-02
08:04:58.143

c) 860439 205372 19 2008-05-15
10:43:09.580

860685 205372 21 2008-05-15
14:27:06.670
The above shows the activities for one repairment. The activityID=205372
is the identifier for that repairment and 853187 to 860685 is the
details from we get the computer until we have repair the computer. Only
statusID=26 is interesting in this case. StatusID=26 indicate that the
technician have received some parts for the computer (ex. a soundcard).
There might get parts more than one time pr. activity.

If you run my test-data you will get this output:
*205372 2008-05-02 08:04:58.143 2008-05-15 10:43:09.580 13*
*205375 2008-05-06 08:10:51.777 2008-05-16 14:03:29.580 10
*The output for activityID 205372 (row1) shows the interval from b) to
c) that is 13 days.
What I want is the output from the start to the status right after
StatutID=26. That is from a) to c):
28.april to 15.may: 17 days.
I wan't all the activities that is over 9 days from start to the
statusID just after statusID=26.

I hope I have cleared up the case.
/henrik
Erland Sommarskog wrote:
Henrik Hartig (he**********@varmmail.dk) writes:
>Thank you very much for your answer. I haven't tried your solution,
witch I look forward too.
I have done what you tell me to do. Create a table and insert-statements
plus my own sql (that i describe in my first message).
I hope you will make an afford to look agin at my question.

I'm sorry, but I still don't understanding what you want to achieve.

>my own sql return this:
205372 2008-05-02 08:04:58.143 2008-05-15
10:43:09.580 13
from b) to c).

What you mean with "from b) to c)"? Your query return two rows,
which makes sense as far that in your list, b and c comes after
each other, but the second row has a different activity id, and
that does not match your list.

>I wan't from a) to c) [the row after statusid 26]

a) 853187 205372 20 2008-04-28
12:29:50.627

853465 205372 19 2008-04-28
19:27:05.383

853466 205372 10 2008-04-28
19:27:09.180

b) 855032 205372 26 2008-05-02
08:04:58.143

c) 860439 205372 19 2008-05-15
10:43:09.580

860685 205372 21 2008-05-15
14:27:06.670

So you want to output five rows? But these rows have different columns
from your query produces? Or do you mean that the aggregation is to
include these five rows?

I could probably make some guess of what you really mean, but I would
really prefer if you could make an effort to explain from the beginning
what these columns means and what your query is supposed to achieve.
The query decently complex, and since you say it's wrong I have not dug
into it.

I'm awfully, but if you cannot express your business problem clearly,
it is very difficult to help you. You may know what you are talking
about when you say "from a) to c)", but I don't.

And did you ever say which version of SQL Server you are using?



Jun 27 '08 #3
Henrik Hartig wrote:
The above shows the activities for one repairment. The
activityID=205372 is the identifier for that repairment and 853187 to
860685 is the details from we get the computer until we have repair
the computer. Only statusID=26 is interesting in this case.
StatusID=26 indicate that the technician have received some parts for
the computer (ex. a soundcard).
There might get parts more than one time pr. activity.
But I only wan't one for the output. Even if two repairment is over 9 days

Jun 27 '08 #4
Erland Sommarskog wrote:
8.0 sounds like SQL 2000. The above solution will not run on SQL 2000.
There is no row_number on SQL 2000, and there are no CTEs.

row_number can be emulated with a correlated subquery that computes a
COUNT, but it is very inefficient with large volumes of data.

If you need a solution for SQL 2000, I will have to look into that later,
unless someone beats me to it.

I like to add one thing about the query above: it uses outer joins to b
and c. As the query is written, it works equally well with inner join.
I used outer join because I thought that maybe you wanted to list
activities for which there was no status = 26 yet, or no row after status
= 26. Then I read your requirements a little closer, and saw that you
only wanted to list those that were nine days apart. But you may need
to write similar queries, but where you want to include more data, and
in that case, the outer joins may be the right thing.

Sql can be complicated - I now experience

I would VERY much like a solution for sql 2000.

Looking forward to hear from you
Jun 27 '08 #5
Henrik Hartig (he**********@varmmail.dk) writes:
I would VERY much like a solution for sql 2000.
OK, here it is:

SELECT b.ActivityID, a.firsttime, c.TimeStamp,
datediff(DAY, a.firsttime, c.TimeStamp)
FROM (SELECT ActivityID, firsttime = MIN(TimeStamp)
FROM TESTActivityDetailsAll
GROUP BY ActivityID) AS a
JOIN (SELECT ActivityID, stat26time = MIN(TimeStamp)
FROM TESTActivityDetailsAll
WHERE StatusID = 26
GROUP BY ActivityID
HAVING MIN(TimeStamp) BETWEEN '20080501' AND '20080531') AS b
ON a.ActivityID = b.ActivityID
JOIN TESTActivityDetailsAll c
ON a.ActivityID = c.ActivityID
AND c.TimeStamp = (SELECT MIN(d.TimeStamp)
FROM TESTActivityDetailsAll d
WHERE d.ActivityID = b.ActivityID
AND d.TimeStamp b.stat26time)
WHERE datediff(DAY, b.stat26time, c.TimeStamp) 9

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #6
Erland Sommarskog wrote:
Henrik Hartig (he**********@varmmail.dk) writes:
>I would VERY much like a solution for sql 2000.

OK, here it is:

SELECT b.ActivityID, a.firsttime, c.TimeStamp,
datediff(DAY, a.firsttime, c.TimeStamp)
FROM (SELECT ActivityID, firsttime = MIN(TimeStamp)
FROM TESTActivityDetailsAll
GROUP BY ActivityID) AS a
JOIN (SELECT ActivityID, stat26time = MIN(TimeStamp)
FROM TESTActivityDetailsAll
WHERE StatusID = 26
GROUP BY ActivityID
HAVING MIN(TimeStamp) BETWEEN '20080501' AND '20080531') AS b
ON a.ActivityID = b.ActivityID
JOIN TESTActivityDetailsAll c
ON a.ActivityID = c.ActivityID
AND c.TimeStamp = (SELECT MIN(d.TimeStamp)
FROM TESTActivityDetailsAll d
WHERE d.ActivityID = b.ActivityID
AND d.TimeStamp b.stat26time)
WHERE datediff(DAY, b.stat26time, c.TimeStamp) 9

Thank you. You have been very helpfull :-)

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JT | last post by:
I have an input form for which I've created a "matrix" for user input. Basically, the user chooses a radio button and then through javascript, a select box is displayed to define a value for that...
21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
6
by: Ian Davies | last post by:
Hello all I have the following PHP script to place records from my MySql db into a table. However I now wish users to beable to select the multiple records from the table and on clicking a button...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
1
by: Ryann | last post by:
Hello. I have a form with a series 5 combo-boxes. All 4 contain the same query which lists Parts based on a productID from a previous form. i.e. cboPart1 = Part ID, Part, ProductID Where...
1
by: serena.delossantos | last post by:
Trying to insert into a history table. Some columns will come from parameters sent to the store procedure. Other columns will be filled with a separate select statement. I've tried storing the...
6
by: Apaxe | last post by:
In the database i have a table with this information: key_id =1 key_desc =43+34+22+12 I want sum the values in key_desc. Something like: SELECT key_desc FROM table But the result of...
0
by: cmrhema | last post by:
Hi, I have two controls one html select control and one asp.net dropdownlist control. In html select control i have three maps loaded. It is loaded in the following manner. <SELECT...
1
by: loix | last post by:
I'm trying to Insert 300.000 rows (or thereabouts) into a table (via QMF) using the following Select clause: # Insert into table_name ( Select row1, row2, row..x from table_name2 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.