Sql server 分组日期周期(按天数),但不包括周末?
我有一张有开始和结束日期的桌子。我的目标是创建一个表,将这些日期分组为周期跨越的天数。我以为我有一个简单的SQL语句(MS SQL Server 2005)的解决方案,但我想排除周末Sql server 分组日期周期(按天数),但不包括周末?,sql-server,asp-classic,Sql Server,Asp Classic,我有一张有开始和结束日期的桌子。我的目标是创建一个表,将这些日期分组为周期跨越的天数。我以为我有一个简单的SQL语句(MS SQL Server 2005)的解决方案,但我想排除周末 SELECT DATEDIFF(D, StartDate, EndDate)+1 AS Days, COUNT(ID) as Count FROM myDateTable GROUP BY DATEDIFF(D, StartDate, EndDate) 这将提供一组记录,其中包括: Days Count 1
SELECT DATEDIFF(D, StartDate, EndDate)+1 AS Days,
COUNT(ID) as Count
FROM myDateTable
GROUP BY DATEDIFF(D, StartDate, EndDate)
这将提供一组记录,其中包括:
Days Count
1 4
2 2
4 1
7 2
这可以在SQL语句中排除周末吗?如果不可以,可以使用ASP和数组来完成吗?那么,使用SQL Server 2005,您可以尝试以下操作
DECLARE @Table TABLE(
ID INT,
StartDate DATETIME,
EndDate DATETIME
)
INSERT INTO @Table (ID,StartDate,EndDate) SELECT 1, '25 Jan 2009', '31 Jan 2009'
INSERT INTO @Table (ID,StartDate,EndDate) SELECT 2, '01 Jan 2009', '07 Jan 2009'
INSERT INTO @Table (ID,StartDate,EndDate) SELECT 3, '01 Jan 2009', '14 Jan 2009'
DECLARE @MinDate DATETIME,
@MaxDate DATETIME
SELECT @MinDate = MIN(StartDate) ,
@MaxDate = MAX(EndDate)
FROM @Table
--Create a temp result set between the Min and Max dates, with all dates, and their weekday names
;WITH DayValues AS(
SELECT @MinDate DateVal,
DATENAME(dw, @MinDate) DateValName
UNION ALL
SELECT DateVal + 1,
DATENAME(dw, DateVal + 1) DateValName
FROM DayValues
WHERE DateVal + 1 <= @MaxDate
),
--select the count of days for each StartDate and EndDate pair, excluding Saturdays and Sundays
DateCounts AS(
SELECT ID,
(
SELECT COUNT(1)
FROM DayValues
WHERE DateVal BETWEEN StartDate AND EndDate
AND DateValName NOT IN ('Saturday', 'Sunday')
) DateCount
FROM @Table
)
--Now group and count
SELECT DateCount,
COUNT(ID) TotalCount
FROM DateCounts
GROUP BY DateCount
OPTION (MAXRECURSION 0)
编辑:简要解释
您需要确定两个日期之间(包括两个日期)的天数,这两个日期不是周末
SELECT DATEDIFF(D, StartDate, EndDate)+1 AS Days,
COUNT(ID) as Count
FROM myDateTable
GROUP BY DATEDIFF(D, StartDate, EndDate)
因此,使用a,我创建了一个临时结果集,其中包含了最小日期和最大日期,以及它们的工作日名称(例如星期一、星期二、星期天)
然后,对于每个日期对,我计算与周六和周日不对应的条目数。这里有一个ASP函数,用于计算两个日期之间的天数和工作日
<%
Dim StartDate, EndDate
StartDate = CDate("1/1/2010")
EndDate = CDate("2/1/2010")
Response.Write(WeekDayCount(StartDate, EndDate))
Function WeekDayCount(StartDate, EndDate)
dim tempDate, dayCount
tempDate = StartDate
dayCount = 0
'Step forward one day, counting non-week days
While tempDate <> EndDate
'The 1 and 7 might need to be tweaked depending on the locale of your
'server. 1 = Sunday, 7 = Saturday
If DatePart("w", tempDate) <> 1 And DatePart("w", tempDate) <> 7 Then
dayCount = dayCount + 1
End If
tempDate = DateAdd("d", 1, tempDate)
Wend
WeekDayCount = dayCount
End Function
%>
查看一下@DATEFIRST 看看这个 选择DATEPART(DW,GETDATE())
您应该能够在“DW”不等于周末数字的情况下运行查询。您可以向数据源添加表吗?我可以通过SQLServer内部的特殊查找表找到一种方法。此外,您还可以看到一种在ASP内部使用循环执行此操作的方法。您喜欢哪种方式?是的,我可以向数据源添加表,但我想我更喜欢ASP解决方案,而不是添加表。您使用的是哪个版本的Sql Server?ASP解决方案将遇到的问题是,您必须将整个数据集拉入ASP页面。也就是说,您失去了SQL Server的分组功能。好吧,仅使用SQL的解决方案是什么?嗨,astander,您能解释一下这里发生了什么吗?我并不是一个SQL server专家!我作了简短的解释。如果你需要更多的帮助,请告诉我。