SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL statement for new vs duplicate data
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

avitron2020
Starting Member

2 Posts

Posted - 05/08/2013 :  15:11:51  Show Profile  Reply with Quote
I'm looking for a SQL statement that will output 'new vs duplicate' data on a monthly bases, but I can't seem to come up SQL statement that can accomplish what I need. See example below to get a better idea of what I'm trying to do since this is hard to explain.

Example data:

id, num, create_date

// January
1, 34250, 2012-01-01
2, 25383, 2012-01-03
3, 93482, 2012-01-13
4, 52934, 2012-01-20
5, 34823, 2012-01-23

// Febuary
6, 09943, 2012-02-03
7, 32845, 2012-02-05
8, 34250, 2012-02-16 -- same num as id 1
9, 57325, 2012-02-28
10, 34823, 2012-02-28 -- same num as id 5

// March
11, 95234, 2012-03-05
12, 25383, 2012-03-03 -- same num as id 2
13, 32845, 2012-03-16 -- same num as id 7
14, 34250, 2012-03-28 -- same num as id 1, 8
15, 12412, 2012-03-28

// April
16, 53535, 2012-04-05
17, 23232, 2012-04-03
18, 57325, 2012-04-16 -- same num as id 9
19, 34250, 2012-04-28 -- same num as id 1, 8, 14
20, 12343, 2012-04-28

Example results from data above would be something like below (just looking for new vs duplicate numbers):

January: 5 new and 0 duplicates (no duplicates will ever be found the first month)
Febuary: 3 new and 2 duplicates (checks for duplicates in the month of Jan only)
March: 2 new and 3 duplicates (checks for duplicates in the month of Jan & Feb only)
April: 3 new and 2 duplicates (checks for duplicates in the month of Jan, Feb & March only)
...and so on for each month after that

Hopefully you can see what I'm trying to do. Also, the total amount (new + duplicate) will always be 5 based on the data above. Any real smart person out there that can help?

Note: I'm using SQL Server 2005

James K
Flowing Fount of Yak Knowledge

3744 Posts

Posted - 05/08/2013 :  16:30:28  Show Profile  Reply with Quote
Give this a try
SELECT 
	YEAR(create_date),
	MONTH(create_date),
	COUNT(*) - COUNT(DISTINCT b.dups) AS New,
	COUNT(DISTINCT b.dups) AS Dups
FROM
	YourTable a
	OUTER APPLY
	(
		SELECT num FROM 
		YourTable b
		WHERE 
		b.create_date < DATEADD(mm,DATEDIFF(mm,0,a.create_date),0)
		--AND b.create_date >= DATEADD(yy,DATEDIFF(yy,0,a.create_date),0)
		AND a.num = b.num

	) b
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/09/2013 :  00:51:44  Show Profile  Reply with Quote

SELECT YEAR(create_date) AS [Year],
MONTH(create_date) AS [Month],
SUM(CASE WHEN Rn=1 THEN 1 ELSE 0 END) AS New,
SUM(CASE WHEN Rn > 1 THEN 1 ELSE 0 END) AS Duplicate
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY num ORDER BY create_Date) AS Rn,*
FROM Table
)t
GROUP BY YEAR(create_date),
MONTH(create_date)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/09/2013 :  01:03:30  Show Profile  Reply with Quote
quote:
Originally posted by James K

Give this a try
SELECT 
	YEAR(create_date),
	MONTH(create_date),
	COUNT(*) - COUNT(DISTINCTSUM(CASE WHEN b.dups>0 THEN 1 ELSE 0 END) AS New,
	COUNT(DISTINCT b.dups)SUM(CASE WHEN COALESCE(b.dups,0)=0 THEN 1 ELSE 0 END) AS Dups
FROM
	YourTable a
	OUTER APPLY
	(
		SELECT numCOUNT(*) AS dups FROM 
		YourTable b
		WHERE 
		b.create_date < DATEADD(mm,DATEDIFF(mm,0,a.create_date),0)
		--AND b.create_date >= DATEADD(yy,DATEDIFF(yy,0,a.create_date),0)
		AND a.num = b.num

	) b
GROUP BY YEAR(create_date),
	MONTH(create_date)




Some tweaks to get expected result

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

avitron2020
Starting Member

2 Posts

Posted - 05/09/2013 :  10:39:33  Show Profile  Reply with Quote
Thanks. I'll try it out today and let you know how it works.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/10/2013 :  00:17:37  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000