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 |
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-23 : 16:53:03
|
| I am comparing 2 Dates (Start_Datetime and Stop_Datetime). I am trying to isolate how much of the distance in Days occurred in a particular month. For example: Start_Datetime 1/1/2009 and Stop_Datetime 3/1/2009.The Breakout in each column would be: January = 31 February = 28 March = 1 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-23 : 17:06:23
|
This ?Declare @date table(d datetime)Declare @startdate datetimeDeclare @enddate datetimeset @startdate='20090101'set @enddate='20090301'While @startdate<=@enddateBegin Insert into @date values (@startdate) set @startdate=@startdate+1EndSelect count(d) as NoOfDays,datename(month,d) as monthname from @date group by datename(month,d) |
 |
|
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-23 : 17:49:04
|
quote: Originally posted by gjohnso7 I am comparing 2 Dates (Start_Datetime and Stop_Datetime). I am trying to isolate how much of the distance in Days occurred in a particular month. For example: Start_Datetime 1/1/2009 and Stop_Datetime 3/1/2009.The Breakout in each column would be: January = 31 February = 28 March = 1
|
 |
|
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-23 : 17:52:34
|
| That is close. I am trying to incorporate this as column of an existing query. I essentially want 12 columns, 1 for each month days, (Jan_Days, Feb_Days.....)So, If I have the start_datetime "FIELD_006" and the stop datetime "FIELD_005" from the same table, can this be done? |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-23 : 18:03:24
|
| Yes you can use PIVOT or Cross tab for it.Show us Sample data and expected output? |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-23 : 18:20:45
|
From Saket's Example:Select max(Case When monthname ='January' then NoofDays Else Null End)January,max(case........,max(case....from(Select count(d) as NoOfDays,datename(month,d) as monthname from @date Group by datename(month,d))Z |
 |
|
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-23 : 18:25:09
|
| SELECT FIELD_005 AS Admit_Date, FIELD_006 AS Discharge_Date, FIELD_001 AS Unique_IDFROM dbo.Util_TableWHERE (FIELD_006 IS NOT NULL)On each row (Unique_id), I would like the number of Jan_Days, Feb_Days, etc by comparing the Admit_Date to the Discharge_Date. The results would need to show the number of month days for each row. My query only hits one table currently.Make Sense? Sorry if I am unclear, this is my first posting attempt. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-23 : 18:28:12
|
| Then read this:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-23 : 20:05:18
|
| How do I populate the Days that fall into each months results into the following table. Each row would represent a individual account and each row may have results that fall under multiple month columns. TABLE [dbo].[Monthy_Days]( [Account_Number] [varchar](80) NULL, [Admit_Date] [datetime] NULL, [Disch_Date] [datetime] NULL, [Jan_Days] [float] NULL, [Feb_Days] [float] NULL, [Mar_Days] [float] NULL Results should look like so:[Account_Number] | [Admit_Date]|[Disch_Date]|[Jan_Days]|[Feb_Days]|[Mar_Days]A12345 | 1/1/2009 | 3/1/2009 | 31 | 28 | 1I have already attempted a SQL View, but have been unable to get to where I need to be. In the end, the results will be used to show volume trends month over month. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-24 : 10:54:43
|
quote: Originally posted by gjohnso7 I am comparing 2 Dates (Start_Datetime and Stop_Datetime). I am trying to isolate how much of the distance in Days occurred in a particular month. For example: Start_Datetime 1/1/2009 and Stop_Datetime 3/1/2009.The Breakout in each column would be: January = 31 February = 28 March = 1
DECLARE @StartDate datetime,@EndDate= datetimeSELECT @StartDate='1/1/2009',@EndDate='3/1/2009';WIth Date_CTE (DateVal)AS(SELECT @StartDateUNION ALLSELECT DATEADD(dd,1,DateVal)FROM Date_CTEWHERE DATEADD(dd,1,DateVal)<=@EndDate)SELECT DATENAME(mm,DateVal),COUNT(*)FROM Date_CTEGROUP BY DATENAME(mm,DateVal),DATEPART(mm,DateVal)ORDER BY DATEPART(mm,DateVal)OPTION (MAXRECURSION 0) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-24 : 11:00:22
|
quote: Originally posted by gjohnso7 How do I populate the Days that fall into each months results into the following table. Each row would represent a individual account and each row may have results that fall under multiple month columns. TABLE [dbo].[Monthy_Days]( [Account_Number] [varchar](80) NULL, [Admit_Date] [datetime] NULL, [Disch_Date] [datetime] NULL, [Jan_Days] [float] NULL, [Feb_Days] [float] NULL, [Mar_Days] [float] NULL Results should look like so:[Account_Number] | [Admit_Date]|[Disch_Date]|[Jan_Days]|[Feb_Days]|[Mar_Days]A12345 | 1/1/2009 | 3/1/2009 | 31 | 28 | 1I have already attempted a SQL View, but have been unable to get to where I need to be. In the end, the results will be used to show volume trends month over month.
;WIth Date_CTE ([Account_Number], [Admit_Date], [Disch_Date],DateVal)AS(SELECT [Account_Number], [Admit_Date], [Disch_Date], [Admit_Date]FROM [dbo].[Monthy_Days]UNION ALLSELECT [Account_Number], [Admit_Date], [Disch_Date], DATEADD(dd,1,DateVal)FROM Date_CTEWHERE DATEADD(dd,1,DateVal)<=[Disch_Date])SELECT [Account_Number], [Admit_Date], [Disch_Date],[January] AS Jan_days,[February] AS Feb_days,....FROM(SELECT [Account_Number], [Admit_Date], [Disch_Date], DateVal, DATENAME(mm,DateVal) as Mnth FROM Date_CTE)mPIVOT(COUNT(DateVal) FOR Mnth IN ([January],[February],[March],...))pOPTION (MAXRECURSION 0) |
 |
|
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-24 : 12:38:22
|
| I receive the following error message:Msg 325, Level 15, State 1, Line 33Incorrect syntax near 'PIVOT'. You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the stored procedure sp_dbcmptlevel.Query is as follows:WIth Date_CTE ([Account_Number], [Admit_Date], [Disch_Date],DateVal)AS(SELECT Account_Number,Admit_Date,Disch_DateFROM Monthy_DaysUNION ALLSELECT Account_Number,Admit_Date,Disch_Date,DATEADD(dd,1,DateVal)FROM Date_CTEWHERE DATEADD(dd,1,DateVal)<=[Disch_Date])SELECT [Account_Number], [Admit_Date], [Disch_Date],[January] AS Jan_days,[February] AS Feb_daysFROM(SELECT [Account_Number], [Admit_Date], [Disch_Date], DateVal, DATENAME(mm,DateVal) as Mnth FROM Date_CTE)mPIVOT(COUNT(DateVal) FOR Mnth IN ([January],[February],[March]))pOPTION (MAXRECURSION 0) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-24 : 12:41:42
|
quote: Originally posted by gjohnso7 I receive the following error message:Msg 325, Level 15, State 1, Line 33Incorrect syntax near 'PIVOT'. You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the stored procedure sp_dbcmptlevel.Query is as follows:SELECT Account_Number,Admit_Date,Disch_DateFROM Monthy_DaysUNION ALLSELECT Account_Number,Admit_Date,Disch_Date,DATEADD(dd,1,DateVal)FROM Date_CTEWHERE DATEADD(dd,1,DateVal)<=[Disch_Date])SELECT [Account_Number], [Admit_Date], [Disch_Date],[January] AS Jan_days,[February] AS Feb_daysFROM(SELECT [Account_Number], [Admit_Date], [Disch_Date], DateVal, DATENAME(mm,DateVal) as Mnth FROM Date_CTE)mPIVOT(COUNT(DateVal) FOR Mnth IN ([January],[February],[March],...))pOPTION (MAXRECURSION 0)
it seems like your db is set to lower compatibility level. use belowEXEC sp_dbcmptlevel 'yourdb',90GO then run given query |
 |
|
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-24 : 12:52:42
|
| Ran the Query EXEC sp_dbcmptlevel 'yourdb',90GOand it suceeded. The error now states:Msg 102, Level 15, State 1, Line 25Incorrect syntax near '.'.Msg 102, Level 15, State 1, Line 32Incorrect syntax near 'm'.The query is as follows:;WIth Date_CTE ([Account_Number], [Admit_Date], [Disch_Date],DateVal)AS(SELECT [Account_Number], [Admit_Date], [Disch_Date], [Admit_Date]FROM [dbo].[Monthy_Days]UNION ALLSELECT [Account_Number], [Admit_Date], [Disch_Date], DATEADD(dd,1,DateVal)FROM Date_CTEWHERE DATEADD(dd,1,DateVal)<=[Disch_Date])SELECT [Account_Number], [Admit_Date], [Disch_Date],[January] AS Jan_days,[February] AS Feb_days,.....FROM(SELECT [Account_Number], [Admit_Date], [Disch_Date], DateVal, DATENAME(mm,DateVal) as Mnth FROM Date_CTE)mPIVOT(COUNT(DateVal) FOR Mnth IN ([January],[February],[March],...))pOPTION (MAXRECURSION 0)I appreciate the help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-24 : 12:55:16
|
| by ... i meant other columns. did you replace it by other month names in both places? |
 |
|
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-24 : 13:30:12
|
quote: Originally posted by visakh16 by ... i meant other columns. did you replace it by other month names in both places?
Worked like a charm! Thank you so very much. Not sure who it works, or what it does, but worked great! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-24 : 13:35:35
|
| read about PIVOT and CTE in books online |
 |
|
|
gjohnso7
Starting Member
11 Posts |
Posted - 2009-02-25 : 19:48:48
|
quote: Originally posted by visakh16 read about PIVOT and CTE in books online
Thanks for the Advice....I will and can already think of many use cases. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-26 : 03:37:54
|
Calculating is more efficient than counting.DECLARE @Start DATETIME, @Stop DATETIMESELECT @Start = '20090101', @Stop = '20090301'SELECT SUM(CASE WHEN Number = 1 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 1 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 1 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS January, SUM(CASE WHEN Number = 2 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 2 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 2 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS February, SUM(CASE WHEN Number = 3 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 3 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 3 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS March, SUM(CASE WHEN Number = 4 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 4 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 4 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS April, SUM(CASE WHEN Number = 5 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 5 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 5 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS May, SUM(CASE WHEN Number = 6 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 6 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 6 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS June, SUM(CASE WHEN Number = 7 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 7 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 7 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS July, SUM(CASE WHEN Number = 8 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 8 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 8 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS August, SUM(CASE WHEN Number = 9 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 9 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 9 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS September, SUM(CASE WHEN Number = 10 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 10 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 10 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS October, SUM(CASE WHEN Number = 11 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 11 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 11 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS November, SUM(CASE WHEN Number = 12 AND @Start BETWEEN p1 AND p2 THEN DATEDIFF(DAY, @Start, p2) + 1 WHEN Number = 12 AND @Stop BETWEEN p1 AND p2 THEN DATEDIFF(DAY, p1, @Stop) + 1 WHEN Number = 12 AND @Start < p1 AND @Stop > p2 THEN DATEDIFF(DAY, p1, p2) + 1 ELSE 0 END) AS DecemberFROM ( SELECT Number, DATEADD(MONTH, 12 * DATEPART(YEAR, @Start) - 22801 + Number, 0) AS p1, DATEADD(MONTH, 12 * DATEPART(YEAR, @Start) - 22800 + Number, -1) AS p2 FROM master..spt_values WHERE Type = 'P' AND Number BETWEEN 1 AND 12 ) AS d E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|
|