473,396 Members | 2,055 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.

Combining queries

2
I have a scenario like this in my application.
We can have values documented for each time column.
I have discrete timepoints in hand and I need to find the
documented values just before and just after the time columns.

Consider a scenario where I have 8 time points like
10:00,10:05,10:10,10:15,10:20,10:25,10:30 and 10:35.

I run a query for that like to find the documented value just before the 8 time points.
I also run the same big query to find the documented value just after the 8 time points by changing the
condition " FLOWSHEET_COLUMN_TIME < '2007-04-11 10:00:00' " to " FLOWSHEET_COLUMN_TIME > '2007-04-11 10:00:00' "
in all the 8 places. Is there any better way to do this. Can I combine these queries to one?
Thanks for any efforts put into thinking this.

Select top 2 PFS_ELEMENT_PARAM_ID, FLOWSHEET_COLUMN_TIME
from io_object_fluids_blood_products
where visit_id = 156 and
PFS_ELEMENT_PARAM_ID in (18845,18847) and
FLOWSHEET_COLUMN_TIME < '2007-04-11 10:00:00'
group by FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID

union

Select top 2 FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID
from io_object_fluids_blood_products
where visit_id = 156 and
PFS_ELEMENT_PARAM_ID in (18845,18847) and
FLOWSHEET_COLUMN_TIME < '2007-04-12 10:05:00'
group by FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID

union

Select top 2 FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID
from io_object_fluids_blood_products
where visit_id = 156 and
PFS_ELEMENT_PARAM_ID in (18845,18847) and
FLOWSHEET_COLUMN_TIME < '2007-04-12 10:10:00'
group by FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID

union

Select top 2 FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID
from io_object_fluids_blood_products
where visit_id = 156 and
PFS_ELEMENT_PARAM_ID in (18845,18847) and
FLOWSHEET_COLUMN_TIME < '2007-04-12 10:15:00'
group by FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID

union

Select top 2 FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID
from io_object_fluids_blood_products
where visit_id = 156 and
PFS_ELEMENT_PARAM_ID in (18845,18847) and
FLOWSHEET_COLUMN_TIME < '2007-04-12 10:20:00'
group by FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID

union

Select top 2 FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID
from io_object_fluids_blood_products
where visit_id = 156 and
PFS_ELEMENT_PARAM_ID in (18845,18847) and
FLOWSHEET_COLUMN_TIME < '2007-04-12 10:25:00'
group by FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID

union

Select top 2 FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID
from io_object_fluids_blood_products
where visit_id = 156 and
PFS_ELEMENT_PARAM_ID in (18845,18847) and
FLOWSHEET_COLUMN_TIME < '2007-04-12 10:30:00'
group by FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID

union

Select top 2 FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID
from io_object_fluids_blood_products
where visit_id = 156 and
PFS_ELEMENT_PARAM_ID in (18845,18847) and
FLOWSHEET_COLUMN_TIME < '2007-04-12 10:35:00'
group by FLOWSHEET_COLUMN_TIME, PFS_ELEMENT_PARAM_ID;
Apr 17 '07 #1
2 1689
iburyak
1,017 Expert 512MB
If it is SQL 2005 you can probably use Ranking functions.

Good Luck.
Apr 17 '07 #2
iburyak
1,017 Expert 512MB
See what I wrote for you.
Hope you would be able to modify it to suet your case:

[PHP]
declare @aa table
(Time datetime)
Declare @date1 datetime
Declare @date2 datetime
Declare @date3 datetime

Select @date1 = '2007-04-11 10:00:00',
@date2 = dateadd(n,5, '2007-04-11 10:00:00'),
@date3 = dateadd(n,10, '2007-04-11 10:00:00')


insert into @aa values ('2007-04-11 10:00')
insert into @aa values ('2007-04-11 10:01')
insert into @aa values ('2007-04-11 10:02')
insert into @aa values ('2007-04-11 10:03')

insert into @aa values ('2007-04-11 10:05')
insert into @aa values ('2007-04-11 10:06')
insert into @aa values ('2007-04-11 10:07')
insert into @aa values ('2007-04-11 10:09')

insert into @aa values ('2007-04-11 10:10')
insert into @aa values ('2007-04-11 10:12')
insert into @aa values ('2007-04-11 10:13')
insert into @aa values ('2007-04-11 10:14')

Select *
From (
select time,
coalesce(case when Time between @date1 and dateadd(n,4,@date1) then 1 end,
case when Time between @date2 and dateadd(n,4,@date2) then 2 end,
case when Time between @date3 and dateadd(n,4,@date3) then 3 end
) Rank from @aa) tmp1
Where time in (select top 2 time
from @aa where coalesce(case when Time between @date1 and dateadd(n,4,@date1) then 1 end,
case when Time between @date2 and dateadd(n,4,@date2) then 2 end,
case when Time between @date3 and dateadd(n,4,@date3) then 3 end) = tmp1.Rank
order by time) [/PHP]

Good Luck.
Apr 17 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: frizzle | last post by:
Hi, I know this might sound strange but i think(/hope) it's quite simple: I'm running 2 queries in a mysql DB, first one returns 20 results. Now how can i echo results from the second query...
2
by: SomeDude | last post by:
Lo group, I am wondering if there is a way of combining two SELECT statements into a single query. Here's the obligatory example to clarify things: SELECT id WHERE name=mike SELECT bills...
1
by: Bruce MacDonald | last post by:
I've got a question/request for the SQL gurus. I'm building a model of bandwidth demand in MS Access and want to get aggregated results for demand at each PCP in each time period. The two...
1
by: ravi | last post by:
I have created the following interest to calculate the interest for the following currency pairs. I have tried to combine them in macros using conditions but the next query that is run in the macro...
2
by: BeruthialsCat | last post by:
I have 4 crosstabs which are keyed on a managerID field. I want to combine them to provide the data for a graph. I currently have them combined in a union query to produce 4 rows of data for each...
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
2
by: billelev | last post by:
Does anyone know if it is possible to combine two queries that have the same fields? I basically want to combine a number of fields with the totals for those fields into one query (see example...
7
by: tcveltma | last post by:
Hi again, Ok, so I have about 15 crosstab queries. Each crosstab query is an employee's name. Within the query are the weeks as the column heading, the work order numbers as the row heading, and...
19
by: Gilberto | last post by:
Hello I have created TWO different queries (for products belonging to FRONT and REAR) which use product information to filter the total (sum all belonging to the same category) of all the FRONT...
3
by: billelev | last post by:
I am trying to combine two queries into one UNION query. The first sub-query works, but the second creates the following error at the end of the first iif statement: "Syntax error in union query" ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.