Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2011-02-25 : 07:59:16
|
HiWe have a call logging database , where we log technical queries, these can stay open for long periods say 6 months or can be as short as a couple of weeks , we would like to see the trend to show at the beginning of every month of each year the count of how many tickets are still openThe table has a number of fields but typically looking at OPENDATE, CLOSEDATE, REFERENCE , if a ticket is still open then the default closedate is 1900-01-01 , if it is closed it has a closedate in format 2011-01-02 .OPENDATE CLOSEDATE REFERENCE2010-12-15 00:00:00.000 2011-02-08 00:00:00.000 GTS111712011-02-03 00:00:00.000 1900-01-01 00:00:00.000 GTS111862009-09-21 00:00:00.000 1900-01-01 00:00:00.000 GTS111042010-11-12 00:00:00.000 1900-01-01 00:00:00.000 GTS111582011-01-27 00:00:00.000 2011-02-11 00:00:00.000 GTS111842011-02-24 00:00:00.000 1900-01-01 00:00:00.000 GTS111912011-02-24 00:00:00.000 1900-01-01 00:00:00.000 GTS111922011-02-07 00:00:00.000 1900-01-01 00:00:00.000 GTS111882011-02-24 00:00:00.000 1900-01-01 00:00:00.000 GTS11193Here is my current query SELECT MONTH(OPENDATE) as month2,YEAR(OPENDATE) as year2,COUNT(REFERENCE) As TotalFROM suppdeskdev.F0007_SUPEVENTWHERE (MONTH (CLOSEDATE) > MONTH (OPENDATE) or YEAR(CLOSEDATE) > YEAR(OPENDATE)) or CLOSEDATE='1900-01-01'GROUP BY YEAR(OPENDATE) , MONTH(OPENDATE)ORDER BY YEAR(OPENDATE) Asc,MONTH(OPENDATE) Ascexample of current output is , this is the output i want but the totals are not correct .Month Year Total7 2010 48 2010 59 2010 110 2010 211 2010 1312 2010 51 2011 102 2011 7, i know for example that at the 1st Feb 2011 i have around 16 rows that have a closedate of 1900-01-01 , i think i am nearly there but cannot see what i have done wrong any help really appreciated |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-25 : 08:45:23
|
| post the desired output as per your sample table information listed above......CheersMIK |
 |
|
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2011-02-25 : 09:22:20
|
| Hi Mik , my desired output is in the same format as above , the only difference is the value for the total columns, i have 186 rows of data , would you want me to show you that data then manually work out the totals per month per year to show what the total column output should be ? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-02-25 : 09:27:37
|
can you based on the sample data that you have posted in your first thread and show us the expected result ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-02-25 : 10:41:06
|
SELECT YEAR(OPENDATE) as [year],MONTH(OPENDATE) as [month],COUNT(*) As TotalFROM suppdeskdev.F0007_SUPEVENTWHERE CLOSEDATE > '19000101'GROUP BY YEAR(OPENDATE), MONTH(OPENDATE)ORDER BY YEAR(OPENDATE), MONTH(OPENDATE) N 56°04'39.26"E 12°55'05.63" |
 |
|
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2011-02-25 : 10:49:01
|
Hi here is the data set and the expected outputThe thinking is , go through data to see how many rows have closedate either = 1900-01-01 ( not closed ) or their closedate is greater than that date ( so on the 1st of December how many calls have closedate='1900-01-01' or closedate is > 2010-01-12 .. group all this by 1st of every month hope this makes senseOpenDate CloseDate Reference24/02/2011 01/01/1900 GTS1119124/02/2011 01/01/1900 GTS1119223/02/2011 01/01/1900 GTS1118923/02/2011 01/01/1900 GTS1119007/02/2011 01/01/1900 GTS1118707/02/2011 01/01/1900 GTS1118803/02/2011 01/01/1900 GTS1118602/02/2011 21/02/2011 GTS1118527/01/2011 11/02/2011 GTS1118425/01/2011 01/01/1900 GTS1118225/01/2011 01/01/1900 GTS1118125/01/2011 01/01/1900 GTS1118321/01/2011 08/02/2011 GTS1118018/01/2011 01/01/1900 GTS1117917/01/2011 21/02/2011 GTS1117814/01/2011 01/01/1900 GTS1117514/01/2011 19/01/2011 GTS1117612/01/2011 07/02/2011 GTS1117312/01/2011 22/02/2011 GTS1117407/01/2011 14/01/2011 GTS1117215/12/2010 08/02/2011 GTS1117110/12/2010 08/02/2011 GTS1117003/12/2010 01/01/1900 GTS1116903/12/2010 01/01/1900 GTS1116802/12/2010 08/02/2011 GTS1116722/11/2010 08/02/2011 GTS1116619/11/2010 22/12/2010 GTS1116519/11/2010 22/02/2011 GTS1116418/11/2010 01/01/1900 GTS1116315/11/2010 17/12/2010 GTS1116115/11/2010 11/02/2011 GTS1116212/11/2010 01/01/1900 GTS1115912/11/2010 01/01/1900 GTS1115812/11/2010 16/12/2010 GTS1116012/11/2010 17/12/2010 GTS11157year month total2010 Nov 10 Dec 112011 Jan 21 Feb 17 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-02-25 : 10:56:42
|
[code]DECLARE @Sample TABLE ( OpenDate DATETIME NOT NULL, CloseDate DATETIME NOT NULL, Reference CHAR(8) NOT NULL )INSERT @Sample ( OpenDate, CloseDate, Reference )SELECT '20101215', '20110208', 'GTS11171' UNION ALLSELECT '20110203', '19000101', 'GTS11186' UNION ALLSELECT '20090921', '19000101', 'GTS11104' UNION ALLSELECT '20101112', '19000101', 'GTS11158' UNION ALLSELECT '20110127', '20110211', 'GTS11184' UNION ALLSELECT '20110224', '19000101', 'GTS11191' UNION ALLSELECT '20110224', '19000101', 'GTS11192' UNION ALLSELECT '20110207', '19000101', 'GTS11188' UNION ALLSELECT '20110224', '19000101', 'GTS11193'-- Solution by Peso;WITH cteCalendar(FromMonth, ToMonth)AS ( SELECT MIN(DATEADD(MONTH, DATEDIFF(MONTH, 0, OpenDate), 0)) AS FromMonth, MAX(DATEADD(MONTH, DATEDIFF(MONTH, 0, CloseDate), 0)) AS ToMonth FROM @Sample UNION ALL SELECT DATEADD(MONTH, 1, FromMonth) AS FromMonth, ToMonth FROM cteCalendar WHERE FromMonth <= ToMonth)SELECT c.FromMonth AS StartOfMonth, f.Items AS OpenCasesFROM cteCalendar AS cOUTER APPLY ( SELECT COUNT(*) AS Items FROM @Sample AS s WHERE s.OpenDate <= c.FromMonth AND ( s.CloseDate > c.FromMonth OR s.CloseDate = '19000101' ) ) AS f(Items)ORDER BY c.FromMonth[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2011-02-25 : 12:13:49
|
| wow PESO !!! , thanks for a quick reply ... is the code valid for sql2000 as i get some errorsMsg 156, Level 15, State 1, Line 25Incorrect syntax near the keyword 'WITH'.Msg 156, Level 15, State 1, Line 41Incorrect syntax near the keyword 'OUTER'.Msg 156, Level 15, State 1, Line 49Incorrect syntax near the keyword 'AS'. |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-25 : 14:35:55
|
No, that will work on SQL 2005 and onwards.... I know that you your desired output format is same as you mentioned in your original post. However I want you to manually fill in the Correct Count Information as per given sample data. So that we can understand how exactly you want your query should Count and to display the result set accordingly. We don't need All 186 Rows .. just few rows (10) and the desired result for them. Any how check the below query if this is what you are looking for select YEAR(OpenDate),datename(MONTH,MONTH(OpenDate)),COUNT(Reference)from <YourTableName>Where CloseDate<=OpenDategroup by YEAR(OpenDate),datename(MONTH,MONTH(OpenDate))This will give you all Tickets which are not yet SOLD, Or still Open CheersMIK |
 |
|
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2011-03-01 : 04:27:33
|
Hi Mik, unfortunately your query doesnt give me the correct totals ; here is my data and the expected results, one of the key points is that the total for the month can contain data from previous months , e.g i f i had a call opened in November , and it did not close until February then it will show in Nov, Dec and Jan figures OpenDate CloseDate Reference24/02/2011 01/01/1900 GTS1119124/02/2011 01/01/1900 GTS1119223/02/2011 01/01/1900 GTS1118923/02/2011 01/01/1900 GTS1119007/02/2011 01/01/1900 GTS1118707/02/2011 01/01/1900 GTS1118803/02/2011 01/01/1900 GTS1118602/02/2011 21/02/2011 GTS1118527/01/2011 11/02/2011 GTS1118425/01/2011 01/01/1900 GTS1118225/01/2011 01/01/1900 GTS1118125/01/2011 01/01/1900 GTS1118321/01/2011 08/02/2011 GTS1118018/01/2011 01/01/1900 GTS1117917/01/2011 21/02/2011 GTS1117814/01/2011 01/01/1900 GTS1117514/01/2011 19/01/2011 GTS1117612/01/2011 07/02/2011 GTS1117312/01/2011 22/02/2011 GTS1117407/01/2011 14/01/2011 GTS1117215/12/2010 08/02/2011 GTS1117110/12/2010 08/02/2011 GTS1117003/12/2010 01/01/1900 GTS1116903/12/2010 01/01/1900 GTS1116802/12/2010 08/02/2011 GTS1116722/11/2010 08/02/2011 GTS1116619/11/2010 22/12/2010 GTS1116519/11/2010 22/02/2011 GTS1116418/11/2010 01/01/1900 GTS1116315/11/2010 17/12/2010 GTS1116115/11/2010 11/02/2011 GTS1116212/11/2010 01/01/1900 GTS1115912/11/2010 01/01/1900 GTS1115812/11/2010 16/12/2010 GTS1116012/11/2010 17/12/2010 GTS11157year month total2010 Nov 10 Dec 112011 Jan 21 Feb 17 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-03-01 : 16:48:57
|
A SQL Server 2000 solutionDECLARE @Sample TABLE ( OpenDate DATETIME NOT NULL, CloseDate DATETIME NOT NULL, Reference CHAR(8) NOT NULL )INSERT @Sample ( OpenDate, CloseDate, Reference )SELECT '20101215', '20110208', 'GTS11171' UNION ALLSELECT '20110203', '19000101', 'GTS11186' UNION ALLSELECT '20090921', '19000101', 'GTS11104' UNION ALLSELECT '20101112', '19000101', 'GTS11158' UNION ALLSELECT '20110127', '20110211', 'GTS11184' UNION ALLSELECT '20110224', '19000101', 'GTS11191' UNION ALLSELECT '20110224', '19000101', 'GTS11192' UNION ALLSELECT '20110207', '19000101', 'GTS11188' UNION ALLSELECT '20110224', '19000101', 'GTS11193'DECLARE @Min DATETIME, @Max DATETIMESELECT @Min = MIN(DATEADD(MONTH, DATEDIFF(MONTH, 0, OpenDate), 0)), @Max = MAX(DATEADD(MONTH, DATEDIFF(MONTH, 0, CloseDate), 0))FROM @Sample-- Solution by PesoSELECT cal.theMonth AS StartOfMonth, COUNT(s.Reference) AS OpenCasesFROM ( SELECT DATEADD(MONTH, Number, @Min) AS theMonth FROM master..spt_values WHERE Type = 'P' AND Number <= 1 + DATEDIFF(MONTH, @Min, @Max) ) AS calLEFT JOIN @Sample AS s ON s.OpenDate <= cal.theMonth AND ( s.CloseDate > cal.theMonth OR s.CloseDate = '19000101' )GROUP BY cal.theMonthORDER BY cal.theMonth N 56°04'39.26"E 12°55'05.63" |
 |
|
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2011-03-02 : 08:24:55
|
Hi Peso, many thanks for your reply .. appreciate you taking the time to help me , i substituted the line data for the link to the table, but recieve the following error Msg 207, Level 16, State 3, Line 24Invalid column name 'Number'.Msg 207, Level 16, State 3, Line 24Invalid column name 'Type'.Msg 207, Level 16, State 3, Line 24Invalid column name 'Number'. DECLARE @Sample TABLE ( OpenDate DATETIME NOT NULL, CloseDate DATETIME NOT NULL, Reference CHAR(8) NOT NULL )INSERT @Sample ( OpenDate, CloseDate, Reference )SELECT OPENDATE, CLOSEDATE, REFERENCE from suppdeskdev.F0007_SUPEVENTDECLARE @Min DATETIME, @Max DATETIMESELECT @Min = MIN(DATEADD(MONTH, DATEDIFF(MONTH, 0, OpenDate), 0)), @Max = MAX(DATEADD(MONTH, DATEDIFF(MONTH, 0, CloseDate), 0))FROM @Sample-- Solution by PesoSELECT cal.theMonth AS StartOfMonth, COUNT(s.Reference) AS OpenCasesFROM ( SELECT DATEADD(MONTH, Number, @Min) AS theMonth FROM master..spt_values WHERE Type = 'P' AND Number <= 1 + DATEDIFF(MONTH, @Min, @Max) ) AS calLEFT JOIN @Sample AS s ON s.OpenDate <= cal.theMonth AND ( s.CloseDate > cal.theMonth OR s.CloseDate = '19000101' )GROUP BY cal.theMonthORDER BY cal.theMonth |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2011-03-02 : 17:08:32
|
Perhaps you are using a case sensitive collation?Try thisDECLARE @Min DATETIME, @Max DATETIMESELECT @Min = DATEADD(MONTH, DATEDIFF(MONTH, 0, MIN(OpenDate)), 0), @Max = DATEADD(MONTH, DATEDIFF(MONTH, 0, MAX(CloseDate)), 0)FROM SuppDeskDev.F0007_SupEvent-- Solution by PesoSELECT cal.theMonth AS StartOfMonth, COUNT(s.OpenDate) AS OpenCasesFROM ( SELECT DATEADD(MONTH, number, @Min) AS theMonth FROM master..spt_values WHERE type = 'P' AND number <= 1 + DATEDIFF(MONTH, @Min, @Max) ) AS calLEFT JOIN SuppDeskDev.F0007_SupEvent AS s ON s.OpenDate <= cal.theMonth AND ( s.CloseDate > cal.theMonth OR s.CloseDate = '19000101' )GROUP BY cal.theMonthORDER BY cal.theMonth N 56°04'39.26"E 12°55'05.63" |
 |
|
|
emailuser
Yak Posting Veteran
74 Posts |
Posted - 2011-03-17 : 09:48:33
|
| Peso , many many thanks works very well, thanks to everyone else who added advice much appreciated :) |
 |
|
|
|
|
|
|
|