help with SQL conditional report

Posted on 2014-02-28
Last Modified: 2014-03-07
i have a table (stacked) with variables cipcode, awlevel, unitid, and year.  

im trying to generate a report that will tell me the distinct count of unitid where awlevel=5 and cipcode=45.0601 in 2001 but not 2011.  Basically i need results where cipcode only exists when year=2001 and doesnt exist when year=2011.

so far i have this, which gives me the results for 2001 only
	select count(distinct(unitid)) as Grad11 label="Schools with Graduations",
	from stacked
	where awlevel=5 and cipcode='45.0601' and year=2001;

Open in new window

I think the answer involves nested selects but i cant wrap my head around it.
Question by:jmichael18
LVL 34

Accepted Solution

Brian Crowe earned 500 total points
ID: 39896757
   @awlevel INT;

SELECT @cipcode = 45.0201,
   @awlevel = 5

FROM stacked AS s2001
LEFT OUTER JOIN stacked AS s2011
   ON s2001.cipcode = s2011.cipcode
WHERE s2001.cipcode = @cipcode
   AND s2001.awlevel = 5
   AND s2011.cipcode IS NULL

Author Closing Comment

ID: 39912835

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL 10 36
MS SQL Server select from Sub Table 14 27
Where is the Help Section? 8 30
A trigger that informs the user when they have not entered the VAT number 6 22
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question