Solved

help with SQL conditional report

Posted on 2014-02-28
2
365 Views
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.
0
Comment
Question by:jmichael18
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 39896757
DECLARE @cipcode FLOAT,
   @awlevel INT;

SELECT @cipcode = 45.0201,
   @awlevel = 5

SELECT COUNT (DISTINCT unitid) AS Grad1
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
0
 

Author Closing Comment

by:jmichael18
ID: 39912835
Thanks!
0

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

773 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