473,473 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Joining Statements in a Query

62 New Member
Hello everyone,

I am new to SQL Server, and am having trouble joining several SELECT statements together.

I have three SQL Select statements that do separate count operations to get the status of many projects. (One statement counts the number of open projects, one counts the number of completed projects, and the third one counts the number of late projects.) The count operations use GROUP BY project to operate.

An example of one of these queries is:
Expand|Select|Wrap|Line Numbers
  1.     -- open
  2.     SELECT PROJECT, COUNT(*) AS OPENAI
  3.     FROM dbo.ACTION_ITEM sel_open
  4.     WHERE (ACTUAL_DATE IS NULL) AND (DUE_DATE >= GETDATE()) 
  5.     GROUP BY PROJECT
Expand|Select|Wrap|Line Numbers
  1.  -- complete
  2.     SELECT PROJECT, COUNT(*) AS COMPLETEAI
  3.     FROM dbo.ACTION_ITEM sel_comp
  4.     WHERE (ACTUAL_DATE IS NOT NULL)
  5.     GROUP BY PROJECT

I would like to create a query that joins these three select statements together so my result has the form

PROJECT | OPEN | CLOSED | LATE
projectA | 3 | 1 | 2
projectB | 4 | 6 | 3

etc.

I have tried:

Expand|Select|Wrap|Line Numbers
  1. SELECT PROJECT, OPENAI
  2. FROM sel_comp LEFT JOIN sel_open
  3. ON sel_comp.PROJECT= sel_open.EFFECTIVITY
but it comes back with a MSG 208, Level 16, State 1, Line 21
"Invalid object name 'sel_all' " and is unhappy with the FROM statement.

Can anyone tell me what I'm doing wrong?

Thanks

nickvans
Oct 22 '08 #1
3 1559
ck9663
2,878 Recognized Expert Specialist
Could you post some sample data coming from the three tables?

-- CK
Oct 23 '08 #2
nickvans
62 New Member
Could you post some sample data coming from the three tables?

-- CK
Thanks for taking a look for me ck.

The data in each of the three queries looks like

PROJECT varchar(50)
OPENAI int

and has data that looks like

PROJECT | OPENAI
Bill, 3
John, 1
NULL, 1
Phil, 2

Similarly the two other queries have columns (PROJECT | CLOSEDAI) and (PROJECT | LATEAI) with the same types of data in them.

I think basically what I'm trying to do is avoid creating three views and joining them together. I'd prefer to just have one view that joins three subqueries together.

Thanks again.
Oct 23 '08 #3
nickvans
62 New Member
Well, yet again I figured it out after posting the question. The secret (a very well publicized secret if you know to look for it) is to create a table variable, then select from that.

Irritatingly, you have to give the table variable an alias to use a where clause, but such is the microsoft way.

Thanks for looking.
Oct 23 '08 #4

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

Similar topics

5
by: Ciar?n | last post by:
I have about 7 tables I need to join, but am having a lot of difficulty with the joins, that I need some help on. I'll provide the details of four tables to illustrate the scenario. I have one...
4
by: sdowney717 | last post by:
Select LOCGeneralHave.LSCH, LOCSubClassHave.LSCH from LOCGeneralHave , LOCSubClassHave Where (LOCGeneralHave.LCNT <> '0' and LOCSubClassHave.LCNT = '0') This query seems to be ignoring the...
2
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2...
1
by: bbdobuddy | last post by:
Hi, I am creating a visual basic application that connects to an access database. I want to do a query by outer joining all of the tables. I just want to know if it is possible and if so how ...
3
by: Reader | last post by:
Hello all, I am joining two tables in a query and the output should be all those records where two fields in table1 match two corresponding fields in table2. I joined the tables using both...
5
by: mike | last post by:
Question. I have a new table that I am adding to a script that I wrote. This table has 3 fields, the first 2 fields are used in the on statement as being = other fields in the script. The...
4
by: Rnt6872 | last post by:
Table A Table B BOL# B_BOL# Chargeback# Hi All, I have been struggling with this for...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g. Please help, I'm trying to write an ASP page to use ADO to run a long query against an Oracle 10g database, to create tables,...
3
by: dotNetDummi | last post by:
I've two tables where their composite keys are foreign keys that references each other. table1 (projId, userId, name, add) table2 (projId, userId, status, responseDate, reason) I need to...
1
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...
0
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...
0
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
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
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.