Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

T-SQL: flattening rows with counts

Posted on 2012-03-12
1
Medium Priority
?
369 Views
Last Modified: 2012-03-13
Techies--

A simplified version of the data I have looks like this:

Store      DateHired      DateTerminated      
10      20090701
10      20110120                     20110817
10      20010601                     20120120
20      20030110                     20110817
20      20110802                     20110817
20              20110817      
20      20110817
20      20110817


What I want back looks like this:

Date            Store      HireCount      TerminatedCount
20010601                      10      1            NULL
20030110                      20      1            NULL
20090701                      10      1            NULL
20110120                      10      1            NULL
20110802                      20      1            NULL
20110817                      10                  1
20110817                      20      3            2
20110817                      20                  2


How do achieve this?  Please advise.
0
Comment
Question by:Paula DiTallo
1 Comment
 
LVL 15

Accepted Solution

by:
tim_cs earned 2000 total points
ID: 37711241
Something like this would work.  

DECLARE @Table TABLE(store INT,datehired VARCHAR(10), DateTerminated VARCHAR(10))


INSERT INTO @Table 
        (store, datehired, DateTerminated)
VALUES
        (10,'20090701',NULL)
		,(20,'20110120','20110817')
		,(20,'20110817',NULL)
		,(20,'20110817',NULL)


;WITH CTE AS (
SELECT
	Store
	,DateHired
	,1 Hired
FROM
	@Table
UNION ALL
SELECT
	Store
	,DateTerminated
	,0
FROM
	@Table
WHERE
	DateTerminated IS NOT NULL)

SELECT 
	c.DateHired
	,c.Store	
	,SUM(CASE WHEN Hired = 1 THEN 1 ELSE 0 END) HireCount
	,SUM(CASE WHEN Hired = 0 THEN 1 ELSE 0 END) TerminatedCount
FROM 
	CTE c
GROUP BY
	c.Store
	,c.DateHired

Open in new window

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

972 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