Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T-SQL: flattening rows with counts

Posted on 2012-03-12
1
Medium Priority
?
365 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
[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 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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

730 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