473,804 Members | 3,250 Online
Bytes | Software Development & Data Engineering Community
+ 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 1575
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
1633
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 table called Product, which contains a complete list of products. I have another table called Date, which contains a complete list of dates. I have a table called sales, which contains Product, date and Qty.
4
1462
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 'and' part of the clause. Basically I want select from table1, table2 where LCNT in table1 is not 0 and LCNT in table2 is 0.
2
3513
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 attrdata_3 etc, etc...
1
1380
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 Thanks in advance bbdobuddy
3
9301
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 fields in design view and the Select statement in SQL view looks good. The query runs perfectly and shows the result I want but when I save the query and close it, re-opening in design view shows the error message "Microsoft Office Access can't
5
1701
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 first field always has data in it, but the 2nd field is sometimes null. So my problem is if both fields have data in them and they both match to the data in the fields that I am linking them to, then it returns
4
1566
by: Rnt6872 | last post by:
Table A Table B BOL# B_BOL# Chargeback# Hi All, I have been struggling with this for the past few months. I have two tables that I'm inner joining on BOL#=B_BOL#. This works fine. Now for the problem....When there are chargeback# fields associated with B_BOL# they aren't being captured as additional records. None of my tables
7
20336
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, if they do not already exist. In terms of ASP/ADO, that would be fine in a SQL Server Sense by a simply ASP/Server-Side JavaScript as such: var cnTemp = Server.CreateObject("ADODB.Connection");
3
1233
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 retrieve table1(name,add) and table2( status, responseDate, reason) with projId only.. my query is Select ap.name, ap.add, p.status, p.responseDate, p.reason
0
9708
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...
0
10589
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
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
9161
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
7625
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
5527
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
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.