473,507 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple Queries

I'm writing an ASP page for a project and it requires multiple queries.
However, I'm trying to combine multiple SELECT statements, but can't
figure out a way that actually works.

Basically, here are the SELECT statements I want to combine:

SELECT * FROM schoolrequest WHERE SLid=10

SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLsendToId=academicoffice.AOid
SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLbillToId=academicoffice.AOid
SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLrequestorId=academicoffice.AOid

SELECT * FROM diaryentry WHERE diaryentry.DEkeyValue=10
AND diaryentry.DEdeletedDate IS NULL

The academicoffice table just contains basic contact info, but needs to
be used 3 times in the query to get specific contact info for 3
different contacts(SLsendToId, SLbillToId, SLrequestorId)

The diaryentry table contains info entered in by students. The
DEkeyValue is actually the primary id of the schoolrequest table(SLid).

Any ideas?

Thanks,
M

Jul 23 '05 #1
4 1690

<ct***@litsol.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I'm writing an ASP page for a project and it requires multiple queries.
However, I'm trying to combine multiple SELECT statements, but can't
figure out a way that actually works.

Basically, here are the SELECT statements I want to combine:

SELECT * FROM schoolrequest WHERE SLid=10

SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLsendToId=academicoffice.AOid
SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLbillToId=academicoffice.AOid
SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLrequestorId=academicoffice.AOid

SELECT * FROM diaryentry WHERE diaryentry.DEkeyValue=10
AND diaryentry.DEdeletedDate IS NULL

The academicoffice table just contains basic contact info, but needs to
be used 3 times in the query to get specific contact info for 3
different contacts(SLsendToId, SLbillToId, SLrequestorId)

The diaryentry table contains info entered in by students. The
DEkeyValue is actually the primary id of the schoolrequest table(SLid).

Any ideas?

Thanks,
M


What do you mean by "combine"? If you want to return all the results in one
result set, then you can use UNION ALL, assuming that each query's result
set has the same columns and data type (although that doesn't seem to be the
case in your example). If that isn't helpful, then you'll have to give some
more information - DDL for the tables, some sample data and the expected
results.

http://www.aspfaq.com/etiquette.asp?id=5006

Simon
Jul 23 '05 #2
Simon, well I mean I need to pull all the data from the schoolrequest
table. But the schoolrequest table has some columns with ID numbers
that correspond to rows in the academicoffice and diaryentry tables.

CREATE TABLE [schoolrequest] (
[SLid] [int] IDENTITY (1, 1) NOT NULL ,
[SLjobTypeID] [varchar] (7) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[SLcopyTypeID] [varchar] (7) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[SLreceivedDate] [datetime] NULL ,
[SLrequestorID] [int] NULL ,
[SLbillToID] [int] NULL ,
[SLsendToID] [int] NULL ,
[SLclaimNumber] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL ,
[SLbillToFile] [varchar] (39) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL ,
[SLsubjectName] [varchar] (63) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL ,
[SLsetsRequested] [int] NULL ,
[SLrequestedDate] [smalldatetime] NULL ,
[SLestimatedUnits] [int] NULL ,
[SLactualUnits] [int] NULL ,
[SLcolorUnits] [decimal](2, 0) NOT NULL ,
[SLinstructions] [varchar] (198) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL ,
[SLnotes] [varchar] (183) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[SLcurrentStatus] [decimal](3, 0) NULL ,
[SLstatusUpdated] [datetime] NULL ,
[SLenteredDate] [smalldatetime] NULL ,
[SLenteredBy] [varchar] (11) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[SLmodifiedDate] [datetime] NOT NULL ,
[SLmodifiedBy] [varchar] (11) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL
) ON [PRIMARY]
GO

Sample of one row:

5334
TRJTINT
TRRTALL
2005-01-17 15:32:26.000
870
NULL
1059
5837
D
433498
10-18-02
NULL
Deborah
Schirmer
1
NULL
NULL
185
0
Subpeona
NULL
100
2005-01-17 15:35:07.000
2005-01-17 00:00:00
djohns
2005-01-17 15:35:07.000
djohns

Jul 23 '05 #3
Stu
I think I get what you're driving at; you need to use aliases to rename
both the columns and the tables to retrieve a meaningful dataset.

SELECT sr.ColumnList,
a1.Name as SendToName,
a2.Name as BillToName,
a3.Name as RequestorName,
d.ColumnList
FROM schoolrequest sr
LEFT JOIN academicoffice a1 ON sr.SLsendToID=a1.AOid
LEFT JOIN academicoffice a2 ON sr.SLBillToID=a2.AOid
LEFT JOIN academicoffice a3 ON sr.SLrequestorID=a3.AOid
LEFT JOIN (SELECT *
FROM diaryentry
WHERE DEdeletedDate IS NULL) d ON sr.SLid=d.DEkeyValue
WHERE sr.SLid=10

If that ain't it, try to post some sample data as well as an example of
the desired result.

HTH,
Stu

Jul 23 '05 #4
(ct***@litsol.com) writes:
I'm writing an ASP page for a project and it requires multiple queries.
However, I'm trying to combine multiple SELECT statements, but can't
figure out a way that actually works.

Basically, here are the SELECT statements I want to combine:

SELECT * FROM schoolrequest WHERE SLid=10

SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLsendToId=academicoffice.AOid
SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLbillToId=academicoffice.AOid
SELECT SLlastName FROM academicoffice
WHERE schoolrequest.SLrequestorId=academicoffice.AOid

SELECT * FROM diaryentry WHERE diaryentry.DEkeyValue=10
AND diaryentry.DEdeletedDate IS NULL

The academicoffice table just contains basic contact info, but needs to
be used 3 times in the query to get specific contact info for 3
different contacts(SLsendToId, SLbillToId, SLrequestorId)

The diaryentry table contains info entered in by students. The
DEkeyValue is actually the primary id of the schoolrequest table(SLid).


As a complete guess, this could be what you are looking for:

SELECT s.*, a1.SLlastname, a2.SLlastName, a3.SLlastName
FROM schoolrequest s
JOIN academicoffice a1 ON s.SLsendToId = a1.AOid
JOIN academicoffice a2 ON s.SLbillToId = a2.AOid
JOIN academicoffice a3 ON s.SLrequestorToId = a3.AOid
WHERE s.SLid = 10

Here, I have not included the diaryentry table. I leave that as a exercise
to find that out yourself.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5

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

Similar topics

4
16735
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
1
3425
by: Jeremy | last post by:
I have built a form that calls queries. I have the first 2 set up as select queries, and the third set up as a make table query. When multiple users are on this application at the same time, they...
0
8748
by: MHenry | last post by:
Hi, I know virtually nothing about creating Macros in Access. I would appreciate some help in creating a Macro or Macros that automatically run(s) 14 Queries (three Make Table Queries, and 11...
11
4501
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
4
3737
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
1
1605
by: mattcatmattcat | last post by:
I have a VB7 aspx file I am creating that requires multiple queries each dependant on the previous queries results. If I run these queries in foxpro, I just run a query then create a cursor with...
8
3172
by: beretta819 | last post by:
Ok, so I apologize in advance for the wordiness of what follows... (I am not looking for someone to make this for me, but to point me in the right direction for the steps I need to take.) I was...
7
4719
by: vaiism | last post by:
I am creating a report that outputs the contact information and details about a water treatment plant, and needs to include information about people who work there. If I tie all the information...
11
3649
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
14
6658
by: Supermansteel | last post by:
My team at work uses Cognos to run multiple queries to pull in data. Then they take that data and import into Access and then usually run an Append Query to run a RND function to pull out a few...
0
7223
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
7111
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
7319
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
7376
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
5623
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,...
1
5042
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.