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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

710 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