Click here to Skip to main content
15,915,094 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have data like :
My table


Month	    Nr of Projects	Amount	     Ab-,Zugang
2014-05-01	      8	        3004	       Abgang
2014-02-01	      5	        2314	       Abgang
2014-11-01	     10	        1366	       Zugang
2014-01-01	     1	        37443	       Zugang
2014-07-01	     7	        18400	       Zugang 
2014-12-01	     3	        1500	       Abgang
2014-06-01	     11	        2000	       Zugang
2014-09-01	     9	        8000	       Zugang
2014-03-01	     5	        2555	       Zugang
2014-01-01	     2	        5000	       Zugang
2014-03-01	     7	        7000	       Zugang


 My final results should be like 

Month	      Ab-,Zugang[Zugang]	  Ab-,Zugang[Abgang]
January	            2	                         0
Febuary	            1	                         1
March	            2                            0
….		


Month[Quarter]	   Ab-,Zugang[Zugang]	Ab-,Zugang[Abgang]
Q1	                   4	                 1
Q2	                   1	                 1
Q3	                   2	                 0
Q4	                   1	                 1



How can I go about it? I have no idea how 


I have tried something like this but I'm getting the wrong records

SQL
SELECT CASE WHEN [DATEPART] IS NULL THEN 'Quarter' + CONVERT(VARCHAR(10),QQ)
             ELSE [DATEPART] END [Month], [Zugänge] 
 FROM  (
         SELECT DATENAME(mm, Month ) [DATEPART],DATENAME(qq, Month ) [QQ] , COUNT([Zu-, Abgang]) [Zugänge] 
	        FROM Table 
		 WHERE [Ab-, Zugange] = 'Zugange'
         GROUP BY DATENAME(qq, Month ), DATENAME(mm, Month ) WITH ROLLUP )A
 WHERE ( [DATEPART] IS NOT NULL OR QQ IS NOT NULL )
Posted
Updated 4-Mar-15 23:53pm
v3
Comments
CHill60 5-Mar-15 7:05am    
As an aside to the solutions (I like Solution 2 by the way) you should avoid calling tables and columns names that match reserved words ... e.g. Table, Month (although Tabelle and Monat are ok)

Hi,

Check this...

SQL
SELECT DATEPART(q,GETDATE())


Datepart[^]

Hope this will help you.

Cheers
 
Share this answer
 
v2
Comments
mikybrain1 5-Mar-15 5:24am    
@Magic

Not really (: How can implement that to get my result :)
This is to produce monthly one. I am working on Quarterly one and update you soon ( I have updated it below). I am using a temporary table that you have to change to your table and columns.

SQL
SELECT [Month],COALESCE([Zugang],0) Zugang, COALESCE([Abgang],0) Abgang
FROM
(
   SELECT DATENAME(MONTH,MonthCol) [Month],
          DATEPART(MONTH,MonthCol) [MonthNo],
          Ab_Zugang,
          Count(NoProjects) NoProjects
   FROM #ProjectData
   GROUP BY DATENAME(MONTH,MonthCol),DATEPART(MONTH,MonthCol), Ab_Zugang

 ) proj
 PIVOT (SUM(NoProjects) FOR Ab_Zugang IN (Zugang, Abgang)) As pvt
 ORDER BY MonthNo


The output is


Month	      Zugang	Abgang
January	        2	0
February	0	1
March	       2	0
May	       0	1
June	       1	0
July	        1	0
September	1	0
November	1	0
December	0	1



This is for Quarterly one.

SELECT 'Q'+cast([Month_Quarter] as varchar) Month_Quarter,COALESCE([Zugang],0) Zugang, COALESCE([Abgang],0) Abgang  
FROM
(
   SELECT DATEPART(QUARTER,MonthCol) [Month_Quarter],
          Ab_Zugang,
          Count(NoProjects) NoProjects
   FROM #ProjectData
   GROUP BY DATEPART(QUARTER,MonthCol), Ab_Zugang

 ) proj
 PIVOT (SUM(NoProjects) FOR Ab_Zugang IN (Zugang, Abgang)) As pvt
 ORDER BY Month_QuarterThe output is



The output is

Month_Quarter	Zugang	Abgang
Q1	        4	1
Q2	        1	1
Q3	        2	0
Q4	        1	1
 
Share this answer
 
v2
Comments
CHill60 5-Mar-15 7:06am    
5'd
John C Rayan 5-Mar-15 7:11am    
Thank you :)
mikybrain1 5-Mar-15 7:47am    
@ CHill60
Thnx dude

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900