473,671 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combining queries

2 New Member
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_COLUM N_TIME < '2007-04-11 10:00:00' " to " FLOWSHEET_COLUM N_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_PAR AM_ID, FLOWSHEET_COLUM N_TIME
from io_object_fluid s_blood_product s
where visit_id = 156 and
PFS_ELEMENT_PAR AM_ID in (18845,18847) and
FLOWSHEET_COLUM N_TIME < '2007-04-11 10:00:00'
group by FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID

union

Select top 2 FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID
from io_object_fluid s_blood_product s
where visit_id = 156 and
PFS_ELEMENT_PAR AM_ID in (18845,18847) and
FLOWSHEET_COLUM N_TIME < '2007-04-12 10:05:00'
group by FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID

union

Select top 2 FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID
from io_object_fluid s_blood_product s
where visit_id = 156 and
PFS_ELEMENT_PAR AM_ID in (18845,18847) and
FLOWSHEET_COLUM N_TIME < '2007-04-12 10:10:00'
group by FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID

union

Select top 2 FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID
from io_object_fluid s_blood_product s
where visit_id = 156 and
PFS_ELEMENT_PAR AM_ID in (18845,18847) and
FLOWSHEET_COLUM N_TIME < '2007-04-12 10:15:00'
group by FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID

union

Select top 2 FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID
from io_object_fluid s_blood_product s
where visit_id = 156 and
PFS_ELEMENT_PAR AM_ID in (18845,18847) and
FLOWSHEET_COLUM N_TIME < '2007-04-12 10:20:00'
group by FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID

union

Select top 2 FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID
from io_object_fluid s_blood_product s
where visit_id = 156 and
PFS_ELEMENT_PAR AM_ID in (18845,18847) and
FLOWSHEET_COLUM N_TIME < '2007-04-12 10:25:00'
group by FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID

union

Select top 2 FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID
from io_object_fluid s_blood_product s
where visit_id = 156 and
PFS_ELEMENT_PAR AM_ID in (18845,18847) and
FLOWSHEET_COLUM N_TIME < '2007-04-12 10:30:00'
group by FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID

union

Select top 2 FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID
from io_object_fluid s_blood_product s
where visit_id = 156 and
PFS_ELEMENT_PAR AM_ID in (18845,18847) and
FLOWSHEET_COLUM N_TIME < '2007-04-12 10:35:00'
group by FLOWSHEET_COLUM N_TIME, PFS_ELEMENT_PAR AM_ID;
Apr 17 '07 #1
2 1701
iburyak
1,017 Recognized Expert Top Contributor
If it is SQL 2005 you can probably use Ranking functions.

Good Luck.
Apr 17 '07 #2
iburyak
1,017 Recognized Expert Top Contributor
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,@da te1) then 1 end,
case when Time between @date2 and dateadd(n,4,@da te2) then 2 end,
case when Time between @date3 and dateadd(n,4,@da te3) 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,@da te1) then 1 end,
case when Time between @date2 and dateadd(n,4,@da te2) then 2 end,
case when Time between @date3 and dateadd(n,4,@da te3) 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
2791
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 in the 1st query's result, like:
2
2103
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 WHERE nameid=12 (12 being the result of the first query) Right now I am executing two separate calls and I would ofcourse like to
1
6004
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 queries below are used to count the number of households of each Socio-Economic Type (each postcode has been allocated an SE-Type), and use that to count the number of connections requiring a bandwidth less than 512 that will be required at the PCP in...
1
3202
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 ends up deleting the previous interest value that has been generated by the query. For example if query 1 is run on the table with currency pair USD/CHF then the interest will be updated without any problem but if there is another entry in the...
2
4150
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 manager by month. What i think i need is to change the orientation so i have a column heading that represenst each crosstab, a manager ID column and a month column. So far ive thought of making a temp table using make table on one of the...
1
2062
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 query language, which forces me to do the join manually via arrays. Right now, I'm having trouble getting these query results (which are in
2
1474
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 below). eg. If the original query contained four items: a, b, c and d, the resulting query or table combination would be: Name Amount a 1 b 2 c 3 d 4 ALL 10
7
2744
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 the hours as a value. Each query comes from a different employee table (1 table per employee). Is there any way I can combine all the queries into 1 query/report? So far it seems to me that the only way I'll have a chance at making every...
19
1830
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 products belonging to every category. Same thing applies to the second query but just with products belonging to the REAR. The type of categories are the same for both queries (RED, BLUE, BLACK) and i just change the "CRITERIA" under the SEATING...
3
1544
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" Does anybody know why? Select Symbol, MarketValue AS , Type, RiskType FROM . AS tmpVarPositions UNION SELECT Symbol, Sum(MarketValue), AlteredType as Type, RiskType
0
8483
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8605
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8676
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7445
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6237
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.