Solved

A difficult SQL query

Posted on 2007-11-28
7
563 Views
Last Modified: 2011-04-14
I have a table like following, it has three columns:
ID          Status                Date
1             13        3/8/2007 18:55:58
2             14        3/9/2007 19:00:00
3             13        3/10/2007 19:00:00
4             13        3/11/2007  19:00:00
I want to make a sql query to get the following record:
status 13 insert to table after status 14(base on date column), such as ID 3 record  in above sample table.
and if the lastest record is not 14, like row 4, the lastest record is 13, so row 4 shouldn't be selected.
In sample table, only row 3 should be selected.
I am not sure I have explained it very clear, pls. help me. Thanks a lot!
btw, server is sql-server, so t-sql can be used.
0
Comment
Question by:d_chenxin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:JakeAnvil
ID: 20364007
It is a bit tricky to understand. I suggest you make up a table of data that should be retrieved by the query in subject. Just the way you typed in the initial data table in your question. More details on what you want to accomplish could be helpfull, too.
0
 
LVL 9

Expert Comment

by:auke_t
ID: 20364039
Something like this I think:
SELECT
	*
FROM
	yourtable t3
WHERE
	t3.[date] in
(
SELECT
	(SELECT min(t2.[date]) FROM yourtable t2 WHERE t2.[date] > t1.[date]
FROM
	yourtable t1
WHERE
	status = 14
)
AND
	status = 13

Open in new window

0
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 20364040

SELECT ID,Status,DATE from
MyTable M where M.ID >=
 (Select min(id) from Mytable M1 where M1.Status = 
(select Max(M2.Status) from MyTable M2))

Open in new window

0
 
LVL 9

Accepted Solution

by:
auke_t earned 500 total points
ID: 20364053
Forgot one )
CREATE TABLE #yourtable
(
ID integer,
Status integer,
Date DATETIME
)
INSERT INTO #yourtable VALUES (1, 13, '3/8/2007 18:55:58')
INSERT INTO #yourtable VALUES (2, 14, '3/9/2007 19:00:00')
INSERT INTO #yourtable VALUES (3, 13, '3/10/2007 19:00:00')
INSERT INTO #yourtable VALUES (4, 13, '3/11/2007  19:00:00')
 
SELECT
	*
FROM
	#yourtable t3
WHERE
	t3.[date] in
(
SELECT
	(SELECT min(t2.[date]) FROM #yourtable t2 WHERE t2.[date] > t1.[date]) 
FROM
	#yourtable t1
WHERE
	status = 14
)
AND
	status = 13

Open in new window

0
 

Author Comment

by:d_chenxin
ID: 20370591
Hi auke_t,

Thank you so much for your solution, it's very good!
I want to accept your solution, but it always shows an error: EmailTemplateTag not found (Tag: GRADING_COMMENTS)
So do you know what's wrong with me? Thanks!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question