I have a history table, and I want to select the first date occurrence
of a type 'A' event after the last event of type other than 'A', if
there is one. The best I can come up with is below. This seems
convoluted, especially compared with a cursor over the history in
descending order. Is this the best I can do in sql? Would a UDF using a
cursor, returning the chosen date be faster or more effective? How do I
extend this to work in the case the are no type<>'A' events? To make the
task more challenging, the type='A' phrase is significantly more complex
and this is just one column in a select over a 13-way join.
select startdate as first_contiguous
from history
where type='A'
and startdate=
(select min(startdate)
from history
where type='A'
and startdate>=
(select max(startdate)
from history
where type<>'A')) 4 1319
How about:
with last_non_a(startdate) as (select max(startdate) from history where
type != 'A'),
recent_events(start_date, rownum) as (select startdate, row_number()
over (order by startdate asc) from history h, last_non_a l where h.type
= 'A' and h.startdate > l.startdate)
select startdate form recent_events where rownum = 1
I guess that isn't really any less convoluted, but it might possibly be
quicker.
-Chris
ChrisC wrote: How about:
with last_non_a(startdate) as (select max(startdate) from history where type != 'A'), recent_events(start_date, rownum) as (select startdate, row_number() over (order by startdate asc) from history h, last_non_a l where h.type = 'A' and h.startdate > l.startdate) select startdate form recent_events where rownum = 1
I guess that isn't really any less convoluted, but it might possibly be quicker.
-Chris
Thanks, I'll try it.
Bob Stearns wrote: I have a history table, and I want to select the first date occurrence of a type 'A' event after the last event of type other than 'A', if there is one. The best I can come up with is below. This seems convoluted, especially compared with a cursor over the history in descending order. Is this the best I can do in sql? Would a UDF using a cursor, returning the chosen date be faster or more effective? How do I extend this to work in the case the are no type<>'A' events? To make the task more challenging, the type='A' phrase is significantly more complex and this is just one column in a select over a 13-way join.
select startdate as first_contiguous from history where type='A' and startdate= (select min(startdate) from history where type='A' and startdate>= (select max(startdate) from history where type<>'A'))
SELECT
MIN(StartDate)
FROM
History
WHERE
Type = 'A'
AND (
NOT EXISTS(SELECT * FROM History WHERE Type <> 'A')
OR (
EXISTS(SELECT * FROM History WHERE Type <> 'A')
AND StartDate > (SELECT MAX(StartDate) FROM History
WHERE Type <> 'A')
)
)
A less confusing query could be something like:
SELECT
MIN(StartDate)
FROM
History
WHERE
Type = 'A'
AND StartDate >
(SELECT COSLESCE(MAX(StartDate), DATE('1/1/1900')) FROM History
WHERE Type <> 'A')
B.
Brian Tkatch wrote: Bob Stearns wrote:
I have a history table, and I want to select the first date occurrence of a type 'A' event after the last event of type other than 'A', if there is one. The best I can come up with is below. This seems convoluted, especially compared with a cursor over the history in descending order. Is this the best I can do in sql? Would a UDF using a cursor, returning the chosen date be faster or more effective? How do I extend this to work in the case the are no type<>'A' events? To make the task more challenging, the type='A' phrase is significantly more complex and this is just one column in a select over a 13-way join.
select startdate as first_contiguous from history where type='A' and startdate= (select min(startdate) from history where type='A' and startdate>= (select max(startdate) from history where type<>'A'))
SELECT MIN(StartDate) FROM History WHERE Type = 'A' AND ( NOT EXISTS(SELECT * FROM History WHERE Type <> 'A') OR ( EXISTS(SELECT * FROM History WHERE Type <> 'A') AND StartDate > (SELECT MAX(StartDate) FROM History WHERE Type <> 'A') ) )
A less confusing query could be something like:
SELECT MIN(StartDate) FROM History WHERE Type = 'A' AND StartDate > (SELECT COSLESCE(MAX(StartDate), DATE('1/1/1900')) FROM History WHERE Type <> 'A')
B.
Very good thought. Put a fence value in in the missing case. I've used
the technique many times, just never in sql. Thank you. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Elroyskimms |
last post by:
Using SQL 2000...
tblCustomer:
CustomerID int
CompanyName varchar(20)
HasRetailStores bit
HasWholesaleStores bit
HasOtherStores bit
tblInvoiceMessages:
|
by: bobsawyer |
last post by:
I've been building a series of SELECT lists that are populated
dynamically using HTTPRequest. Things are going pretty well, and I've
got the whole thing working flawlessly in Mozilla/Firebird....
|
by: dumbledad |
last post by:
Hi All,
I'm confused by how to replace a SELECT statement in a SQL statement
with a specific value. The table I'm working on is a list of words (a
column called "word") with an index int...
|
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...
|
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...
|
by: chunk1978 |
last post by:
hey everyone,
i've been trying to solve this problem for 2 days straight, with no end in sight. i would greatly appreciate anyone's help.
EXPLANATION: There are 3 Select Menus. The 1st and...
|
by: naima.mans |
last post by:
Hello,
i want to select 2 following brothers nodes wich are one under another
(one closed to another)... i have done one xslt file... but it's not
really good..
for example:
the xml file:...
|
by: rn5a |
last post by:
A Form has 2 select lists. The 1st one whose size is 5 (meaning 5
options are shown at any given time) allows multiple selection whereas
the 2nd one allows only 1 option to be selected at a time.
...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |