Solved

T-SQL: flattening rows with counts

Posted on 2012-03-12
1
363 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:ditallop
[X]
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
1 Comment
 
LVL 15

Accepted Solution

by:
tim_cs earned 500 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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 …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

615 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