我有一个数据,我必须根据所选日期按月或按周或按日显示记录。
If the date selected is for a whole month for like january its 01-01-2015 to 31-01-2015
then it should fetch for the record for the whole month.
如果选择的日期类似于01-01-2015
到15-02-2015
。现在这里有一个多月,但Feburary
不是整月,所以januar
y将记录在一个,下一个第一周记录Feburary
将在一个中提取下周将有另一个记录。
他离开的那天(因为第二周将有14天)将会有另一个记录。
因此,将提取所有4 record
。在这里,我只是制作了4条记录而不是整个46天的记录。希望我给出一个恰当的例子,如果你不能发表评论我将用另一个例子来解释。我现在只是担心如何分割月,周和日。任何人都可以帮助我。提前致谢
我只担心如何按月或一天分割日期。如果e有整月,则该公式很简单,该条目应显示在单个记录中。如果我们在超过一周之间的日期,它应该周显示,少于一周它应该是在日级别
输入 这里Time_type_id是来自其他3个表的外键。这个time_type_id将有助于每天,每周或每月获取数据。根据用户传递的日期显示数据。 Time_id是表的主键,它指向其他3个事实表的id。
Dimesion Table
Time_type_id Time_id metric_id
1 1 100
1 2 400
1 3 300
1 4 100
1 5 250
2 6 1000
2 7 2000
2 8 3000
2 9 2000
2 10 5000
3 11 11000
3 12 20000
3 13 40000
3 14 50000
3 15 60000
Fact table
Fact1- This shows the day basis data.
Dim_type Id day_date month year
1 1 01-09-2015 September 2015
1 2 02-09-2015 September 2015
1 3 03-09-2015 September 2015
1 4 04-09-2015 September 2015
1 5 05-09-2015 September 2015
Fact2- This shows the weekly basis data.
Dim_type Id week_number month year
2 6 week1 September 2015
2 7 week2 September 2015
2 8 week3 September 2015
2 9 week4 September 2015
2 10 week1 October 2015
Fact3- This shows the monthly basis data.
Dim_type Id month year
3 11 September 2015
3 12 September 2015
3 13 September 2015
3 14 September 2015
3 15 October 2015
现在,用户刚刚通过了他希望查看数据的日期,并且根据已通过的日期,我们必须查看输出。 如果是月级,我们必须显示Fact3表,其中Dim_type与其他两个表相同。我只想要如何分割日期
输出 -
第二个例子 -
Sr no id data (from the date range)
1 1 data from whole January
2 2 data from first week of Feburary
3 3 data from second week of Feburary
4 4 data from 15 Febuaray
注意:如果一周从Thrusday开始,那么一周总是有7天可能没有7天
答案 0 :(得分:0)
您可以使用两个参数,因此可以选择日期,例如:
Declare @FromDate DATETIME,
@ToDate DATETIME
SELECT ...
FROM ...
WHERE DATE BETWEEN @FromDate AND @ToDate
答案 1 :(得分:0)
您的案例中可以使用两种解决方案:
解决方案#1
您需要一个表/ temptable,其中包含所有日期的数据,其中包含Date,DaySrNo,WeekSrNo,MonthSrNo和Further YearSrNo等列。在Join中使用此表并在三个表(也在JOIN中)中获取数据
解决方案#2 现有表结构中的日期范围需要额外的列: 像In Fact1表一样包含日期数据,所以你需要像StartDate和EndDate这样的日期范围列 相同的方式在其他表Fact2和Fact3
中相同的两列因此,在Join和where过滤器中使用此列可以获得所需的输出。
答案 2 :(得分:0)
IF OBJECT_ID('IAS_TEST_SCHEMA.DATESPLIT', 'P') IS NOT NULL
DROP PROCEDURE IAS_TEST_SCHEMA.DATESPLIT;
GO
CREATE PROCEDURE IAS_TEST_SCHEMA.DATESPLIT (@STARTDATE date, @ENDDATE date)
AS
BEGIN
DECLARE @DATEDIFF integer,
@MONTHEND integer = 0,
@MONTHSTART integer = 0,
@STARTWEEK integer = 0,
@ENDWEEK integer = 0;
IF @STARTDATE = CONVERT(date, DATEADD(M, DATEDIFF(M, 0, @STARTDATE), 0))
BEGIN
SET @MONTHSTART = 1;
END
IF MONTH(@ENDDATE) != MONTH(DATEADD(DAY, 1, @ENDDATE))
BEGIN
SET @MONTHEND = 1;
END
IF DATEPART(DW, @STARTDATE) = 2
BEGIN
SET @STARTWEEK = 1;
END
IF DATEPART(DW, @ENDDATE) = 1
BEGIN
SET @ENDWEEK = 1;
END
SET @DATEDIFF = DATEDIFF(DAY, @STARTDATE, @ENDDATE);
SELECT
CASE
WHEN @DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
WHEN @DATEDIFF > 30 AND
@DATEDIFF < 90 THEN CASE
WHEN @STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
END
WHEN @DATEDIFF >= 90 THEN CASE
WHEN @MONTHSTART = 1 THEN DATENAME(MONTH, @STARTDATE) + ' ' + DATENAME(YEAR, @STARTDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
END
END AS START_DATE,
CASE
WHEN @DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
WHEN @DATEDIFF > 30 AND
@DATEDIFF < 90 THEN CASE
WHEN @STARTWEEK = 1 THEN CASE
WHEN @ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -1)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -1))) + '/'
+ DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -1))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -1)))
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @STARTDATE), 6)))
END
WHEN @DATEDIFF >= 90 THEN CASE
WHEN @MONTHSTART = 1 THEN CASE
WHEN @MONTHEND = 0 THEN DATENAME(MONTH, DATEADD(MONTH, DATEDIFF(MONTH, -1, @ENDDATE) - 1, -1)) + ' ' + DATENAME(YEAR, DATEADD(MONTH, DATEDIFF(MONTH, -1, @ENDDATE) - 1, -1))
ELSE DATENAME(MONTH, @ENDDATE) + ' ' + DATENAME(YEAR, @ENDDATE)
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(S, -1, DATEADD(MM, DATEDIFF(M, 0, @STARTDATE) + 1, 0))))
END
END AS END_DATE
UNION
SELECT
CASE
WHEN @DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
WHEN @DATEDIFF > 30 AND
@DATEDIFF < 90 THEN CASE
WHEN @STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 7)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 7))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 7))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 7)))
END
WHEN @DATEDIFF >= 90 THEN CASE
WHEN @MONTHSTART = 1 THEN DATENAME(MONTH, @STARTDATE) + ' ' + DATENAME(YEAR, @STARTDATE)
ELSE DATENAME(MONTH, CONVERT(date, DATEADD(M, DATEDIFF(M, -1, @STARTDATE), 0))) + ' '
+ DATENAME(YEAR, CONVERT(date, DATEADD(M, DATEDIFF(M, -1, @STARTDATE), 0)))
END
END,
CASE
WHEN @DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
WHEN @DATEDIFF > 30 AND
@DATEDIFF < 90 THEN CASE
WHEN @ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -1)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -1))) + '/'
+ DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -1))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -1)))
END
WHEN @DATEDIFF > 90 THEN CASE
WHEN @MONTHEND = 0 THEN DATENAME(MONTH, CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, -1, @ENDDATE) - 1, -1))) + ' '
+ DATENAME(YEAR, CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, -1, @ENDDATE) - 1, -1)))
ELSE DATENAME(MONTH, @ENDDATE) + ' ' + DATENAME(YEAR, @ENDDATE)
END
END
UNION
SELECT
CASE
WHEN @DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
WHEN @DATEDIFF > 30 AND
@DATEDIFF < 90 THEN CASE
WHEN @ENDWEEK = 1 THEN CASE
WHEN @STARTWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 0)))
ELSE 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 7)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 7))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 7))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @STARTDATE), 7)))
END
ELSE CONVERT(varchar, CONVERT(date, DATEADD(WEEK, DATEDIFF(WEEK, 0, @ENDDATE), -0)))
END
WHEN @DATEDIFF >= 90 THEN CASE
WHEN @MONTHEND = 1 THEN DATENAME(MONTH, @STARTDATE) + ' ' + DATENAME(YEAR, @STARTDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DD, -(DAY(@ENDDATE) - 1), @ENDDATE)))
END
END,
CASE
WHEN @DATEDIFF <= 30 THEN CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
WHEN @DATEDIFF > 30 AND
@DATEDIFF < 90 THEN CASE
WHEN @ENDWEEK = 1 THEN 'WEEK ' + CAST(DATEPART(WEEK, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
- DATEPART(WEEK, DATENAME(YEAR, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0))) +
'/' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0))) + '/' + '1') AS nvarchar) +
' ' + DATENAME(MONTH, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
END
WHEN @DATEDIFF >= 90 THEN CASE
WHEN @MONTHEND = 1 THEN DATENAME(MONTH, @ENDDATE) + ' ' + DATENAME(YEAR, @ENDDATE)
ELSE CONVERT(varchar, CONVERT(date, DATEADD(DAY, DATEDIFF(DAY, 0, @ENDDATE), 0)))
END
END
END;
GO