I have the following query to optimize.
Following used to get the details from the profit table.
First inner SELECT: in the first select statement I have to get the details from the profit table and assign the row number for each row.
Second inner SELECT: in the second select statement I have to do some calculation (sum).
Outer SELECT: And get the result by combining them on id and also do some manipulation with data.
Code:
SELECT
a.p_id, p_Name,
convert(varchar, a.EndDate, 107) EndDate,
convert(varchar, a.EndDate, 106) NewEndDate,
LTRIM(a.p_id)) + '' + REPLACE(LEFT(CONVERT(VARCHAR, a.EndDate, 106), 6) + '' + RIGHT(CONVERT(VARCHAR, a.EndDate, 106), 2), ' ', '') as Compo,
a.GP,
b.fpro FirstProfit,
(b.fpro - b.spro) prodiff,
a.Qtity * a.GP as Ov,
a.Qtity,
b.fproChanPer
FROM
(SELECT
p_Name, p_id,
EndDate,
GP, FirstProfit,
prodiff,
Qtity,
ROW_NUMBER() OVER (PARTITION By p_Name, p_id ORDER BY EndDate) Rown
FROM
tbl_profit) a,
(SELECT
p_id,
CAST(SUM(FirstProfit) AS DECIMAL(24,2)) fpro,
CAST(SUM(SecondProfit) AS DECIMAL(24,2)) spro,
CAST(CAST(SUM(prodiff) AS DECIMAL(24,2)) / CAST(SUM(SecondProfit) AS DECIMAL(24,2)) * 100 AS DECIMAL(24,2)) fproChanPer
FROM
tbl_profit
GROUP BY
p_id) b
WHERE
b.p_id = a.p_id
AND Rown = 1
My question: can I combine those two (alias a and b) inner SELECT statements for query optimization?
My attempt: I have tried by using the following query but getting different calculation result.
SELECT p_Name,
p_id,
EndDate,
GP,
FirstProfit,
prodiff,
Qtity
ROW_NUMBER()OVER (PARTITION By p_Name,p_id ORDER By EndDate ) Rown,
CAST(SUM(FirstProfit) AS DECIMAL(24,2)) fpro,
CAST(SUM(SecondProfit) AS DECIMAL(24,2)) spro,
CAST(CAST(SUM(prodiff) AS DECIMAL(24,2)) /CAST(SUM(SecondProfit) AS DECIMAL(24,2)) * 100 AS DECIMAL(24,2)) fproChanPer
FROM tbl_profit
GROUP By p_id,p_Name,EndDate,GP,FirstProfit,prodiff,Qtity
tbl_profit must have multiple P_Name for a single p_id , when you are grouping by p_id only you are getting correct aggregate value whereas because of multiple p_name for a single p_id when you are grouping by p_id and p_name your aggregate sum values are getting double for wherever single p_id has more than one p_name.
The way you written query to get Rown = 1 records in this case is perfect, thought still have scope to optimize I have done few things please check.
SELECT
a.p_id,
p_Name,
CONVERT(varchar, a.EndDate, 107) EndDate,
CONVERT(varchar, a.EndDate, 106) NewEndDate,
LTRIM(a.p_id) + '' + REPLACE(LEFT(CONVERT(varchar, a.EndDate, 106), 6) + '' + RIGHT(CONVERT(varchar, a.EndDate, 106), 2), ' ', '') AS Compo,
a.GP,
b.fpro AS FirstProfit,
(b.fpro - b.spro) prodiff,
a.Qtity * a.GP AS Ov,
a.Qtity,
b.fproChanPer
FROM (
SELECT
a.p_Name,
a.p_id,
a.EndDate,
a.GP,
a.FirstProfit,
a.prodiff,
a.Qtity,
ROW_NUMBER() OVER (PARTITION BY a.p_Name, a.p_id ORDER BY a.EndDate) Rown
FROM tbl_profit AS a WITH (NOLOCK)
) AS a
INNER JOIN
(
SELECT
b.p_id,
CAST(SUM(b.FirstProfit) AS decimal(24, 2)) fpro,
CAST(SUM(b.SecondProfit) AS decimal(24, 2)) spro,
CAST(CAST(SUM(b.prodiff) AS decimal(24, 2)) / CAST(SUM(b.SecondProfit) AS decimal(24, 2)) * 100 AS decimal(24, 2)) fproChanPer
FROM tbl_profit AS b WITH (NOLOCK)
GROUP BY b.p_id
) AS b
ON b.p_id = a.p_id
AND a.Rown = 1
I'm struggling with an algorithm in SQL server , and I have no clue how to solve this .
I have a column with timestamps on which I've ordered by desc .
I need to find out the differences between de 2nd row and the 1st , the 3rd row and the 2 nd as so on and display this in a new row .The final table should look like this :
ID Type Time_Stamp Difference
xxx YYY 00:03:12 00:00:02
xxx ZZZ 00:03:14 00:00:02
xxx ZZZ 00:03:16
Can I use some kind of SQL function ? Please let me know if you have any ideas .
Cheers ,
Use Window Function
;WITH cte
AS (SELECT Row_number()
OVER(
ORDER BY time_stamp) rn,
*
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), Datediff(second, a.Time_Stamp, b.Time_Stamp)/3600)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (Datediff(second, a.Time_Stamp, b.Time_Stamp)%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), Datediff(second, a.Time_Stamp, b.Time_Stamp)%60), 2) AS [Difference]
FROM cte a
LEFT JOIN cte b
ON a.rn = b.rn - 1
If you are using Sql Server 2012+ then use Lead Function
;WITH cte
AS (SELECT *,
Datediff(second, time_stamp, Lead(time_stamp)
OVER(
ORDER BY time_stamp)) AS Sec
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), sec/3600) + ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (sec%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), sec%60), 2) AS [Difference]
FROM cte a
Update : To insert into temp table do this.
;WITH cte
AS (SELECT Row_number()
OVER(
ORDER BY time_stamp) rn,
*
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), Datediff(second, a.Time_Stamp, b.Time_Stamp)/3600)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (Datediff(second, a.Time_Stamp, b.Time_Stamp)%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), Datediff(second, a.Time_Stamp, b.Time_Stamp)%60), 2) AS [Difference]
into #tempTable --Here you need to use into temptable
FROM cte a
LEFT JOIN cte b
ON a.rn = b.rn - 1
or create the temp table and use
......
Insert into #temptable
SELECT a.ID,
a.Type,
a.Time_Stamp
......
SAMPLE TABLE
CREATE TABLE #TEMP(ID VARCHAR(10),[TYPE] VARCHAR(10),TIME_STAMP TIME)
INSERT INTO #TEMP
SELECT 'xxx' , 'YYY', '00:03:12'
UNION ALL
SELECT 'xxx', 'ZZZ', '00:03:14'
UNION ALL
SELECT 'xxx', 'ZZZ', '00:03:16'
You need to do self join logic to get next row's record
QUERY
;WITH CTE AS
(
SELECT ROW_NUMBER()OVER(ORDER BY TIME_STAMP)RNO,*
FROM #TEMP
)
SELECT C1.*,'00:00:'+CAST(DATEDIFF(S,C1.TIME_STAMP,C2.TIME_STAMP)AS VARCHAR(2)) D
FROM CTE C1
LEFT JOIN CTE C2 ON C1.RNO=C2.RNO-1
Click here to view result
Table structure:
This is the data for which i want to use pivot table
i want to display the result as
Teacher Activity [2013-11-22] and so on The date field will display the count of the attendance.
Below is the code which i am using, result is coming as 0 under every date, which is not correct:
DECLARE #temp AS TABLE(dates datetime)
;with cte (datelist, maxdate) as
(
select DATEADD(dd,-3,GETDATE()), GETDATE()
union all
select dateadd(dd, 1, datelist), maxdate
from cte
where datelist < maxdate
)
INSERT INTO #temp
SELECT c.datelist
FROM cte c
DECLARE #listdate nvarchar(MAX)
SELECT #listdate = (select(STUFF( (SELECT ', ' + QUOTENAME(convert(CHAR(10), dates, 120))
from #temp ORDER BY dates asc
FOR XML PATH('')),1, 1, '')))
print #listdate
exec('(SELECT * INTO ##temptable from
(select distinct teachername as Teacher,activityname AS Activity,memberid as attendance,
QUOTENAME(convert(CHAR(10), attendancedate, 120)) AS DATES from
tbteachertimelog inner join tbteacher on
teacherid = tbteacher.id
inner join tbactivity on tbactivity.id = tbteachertimelog.activityid
left join tbattendance on tbattendance.activityid = tbteachertimelog.activityid and
convert(CHAR(10), tbattendance.attendancedate, 120) = convert(CHAR(10), tbteachertimelog.date, 120)
group by teachername,activityname,memberid,
attendancedate
) p
PIVOT
(
count(attendance)
FOR DATES IN ('+#listdate+')) AS pvt
)
')
alter table ##temptable add TotalStudents int,meetings int,total64 int
update ##temptable set TotalStudents =
(SELECT SUM(memcount) FROM (select count(distinct memberid) as memcount from tbteachertimelog
inner join tbattendance on tbattendance.activityid = tbteachertimelog.activityid and
convert(CHAR(10), tbattendance.attendancedate, 120) = convert(CHAR(10), tbteachertimelog.date, 120)
where --teacherid = ##temptable.teacherid and tbteachertimelog.activityid = ##temptable.activityid
--and
tbattendance.attendancedate >= dateadd(dd,-7,getdate()) and tbattendance.attendancedate <= getdate()
group by convert(CHAR(10), tbattendance.attendancedate, 120)) x)
update ##temptable set meetings =
(select count(distinct convert(CHAR(10), tbattendance.attendancedate, 120)) as dayscount from tbteachertimelog
inner join tbattendance on tbattendance.activityid = tbteachertimelog.activityid and
convert(CHAR(10), tbattendance.attendancedate, 120) = convert(CHAR(10), tbteachertimelog.date, 120)
where teacherid = ##temptable.teacherid and tbteachertimelog.activityid = ##temptable.activityid)
select * from ##temptable
drop table ##temptable
The problem is with the following line in your subquery:
QUOTENAME(convert(CHAR(10), attendancedate, 120)) AS DATES
The QUOTENAME is required when you are creating the list of dates as the column headers but since you added this to your subquery your dates in the table appear:
DATES
[2013-11-22]
[2013-11-23]
[2013-11-24]
But you want the dates being returned inside your subquery should be without the brackets:
DATES
2013-11-22
2013-11-23
2013-11-24
The PIVOT function is looking for dates that match your data but since you have the dates surrounded with square brackets you don't get any matches, as a result you are returning zeros for everything.
Your columns don't match your data because of the bracket -- for example:
Header -- YourDate
2013-11-22 -- does not match [2013-11-22]
2013-11-23 -- does not match [2013-11-23]
2013-11-24 -- does not match [2013-11-24]
The query should be:
exec('
with data as
(
SELECT *
from
(
select distinct teachername as Teacher,
activityname AS Activity,
memberid as attendance,
convert(CHAR(10), attendancedate, 120) AS DATES
from tbteachertimelog
inner join tbteacher
on teacherid = tbteacher.id
inner join tbactivity
on tbactivity.id = tbteachertimelog.activityid
inner join tbattendance
on tbattendance.activityid = tbactivity.id
) p
PIVOT
(
count(attendance)
FOR DATES IN ('+#listdate+')
) AS pvt
)
select *
from data
')
My question is similar to this MySQL question, but intended for SQL Server:
Is there a function or a query that will return a list of days between two dates? For example, lets say there is a function called ExplodeDates:
SELECT ExplodeDates('2010-01-01', '2010-01-13');
This would return a single column table with the values:
2010-01-01
2010-01-02
2010-01-03
2010-01-04
2010-01-05
2010-01-06
2010-01-07
2010-01-08
2010-01-09
2010-01-10
2010-01-11
2010-01-12
2010-01-13
I'm thinking that a calendar/numbers table might be able to help me here.
Update
I decided to have a look at the three code answers provided, and the results of the execution - as a % of the total batch - are:
Rob Farley's answer : 18%
StingyJack's answer : 41%
KM's answer : 41%
Lower is better
I have accepted Rob Farley's answer, as it was the fastest, even though numbers table solutions (used by both KM and StingyJack in their answers) are something of a favourite of mine. Rob Farley's was two-thirds faster.
Update 2
Alivia's answer is much more succinct. I have changed the accepted answer.
this few lines are the simple answer for this question in sql server.
WITH mycte AS
(
SELECT CAST('2011-01-01' AS DATETIME) DateValue
UNION ALL
SELECT DateValue + 1
FROM mycte
WHERE DateValue + 1 < '2021-12-31'
)
SELECT DateValue
FROM mycte
OPTION (MAXRECURSION 0)
Try something like this:
CREATE FUNCTION dbo.ExplodeDates(#startdate datetime, #enddate datetime)
returns table as
return (
with
N0 as (SELECT 1 as n UNION ALL SELECT 1)
,N1 as (SELECT 1 as n FROM N0 t1, N0 t2)
,N2 as (SELECT 1 as n FROM N1 t1, N1 t2)
,N3 as (SELECT 1 as n FROM N2 t1, N2 t2)
,N4 as (SELECT 1 as n FROM N3 t1, N3 t2)
,N5 as (SELECT 1 as n FROM N4 t1, N4 t2)
,N6 as (SELECT 1 as n FROM N5 t1, N5 t2)
,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N6)
SELECT DATEADD(day,num-1,#startdate) as thedate
FROM nums
WHERE num <= DATEDIFF(day,#startdate,#enddate) + 1
);
You then use:
SELECT *
FROM dbo.ExplodeDates('20090401','20090531') as d;
Edited (after the acceptance):
Please note... if you already have a sufficiently large nums table then you should use:
CREATE FUNCTION dbo.ExplodeDates(#startdate datetime, #enddate datetime)
returns table as
return (
SELECT DATEADD(day,num-1,#startdate) as thedate
FROM nums
WHERE num <= DATEDIFF(day,#startdate,#enddate) + 1
);
And you can create such a table using:
CREATE TABLE dbo.nums (num int PRIMARY KEY);
INSERT dbo.nums values (1);
GO
INSERT dbo.nums SELECT num + (SELECT COUNT(*) FROM nums) FROM nums
GO 20
These lines will create a table of numbers containing 1M rows... and far quicker than inserting them one by one.
You should NOT create your ExplodeDates function using a function that involves BEGIN and END, as the Query Optimizer becomes unable to simplify the query at all.
This does exactly what you want, modified from Will's earlier post. No need for helper tables or loops.
WITH date_range (calc_date) AS (
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, '2010-01-13') - DATEDIFF(DAY, '2010-01-01', '2010-01-13'), 0)
UNION ALL SELECT DATEADD(DAY, 1, calc_date)
FROM date_range
WHERE DATEADD(DAY, 1, calc_date) <= '2010-01-13')
SELECT calc_date
FROM date_range;
DECLARE #MinDate DATETIME = '2012-09-23 00:02:00.000',
#MaxDate DATETIME = '2012-09-25 00:00:00.000';
SELECT TOP (DATEDIFF(DAY, #MinDate, #MaxDate) + 1) Dates = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, #MinDate)
FROM sys.all_objects a CROSS JOIN sys.all_objects b;
I'm an oracle guy, but I believe MS SQL Server has support for the connect by clause:
select sysdate + level
from dual
connect by level <= 10 ;
The output is:
SYSDATE+LEVEL
05-SEP-09
06-SEP-09
07-SEP-09
08-SEP-09
09-SEP-09
10-SEP-09
11-SEP-09
12-SEP-09
13-SEP-09
14-SEP-09
Dual is just a 'dummy' table that comes with oracle (it contains 1 row and the word 'dummy' as the value of the single column).
A few ideas:
If you need the list dates in order to loop through them, you could have a Start Date and Day Count parameters and do a while loop whilst creating the date and using it?
Use C# CLR Stored Procedures and write the code in C#
Do this outside the database in code
Would all these dates be in the database already or do you just want to know the days between the two dates? If it's the first you could use the BETWEEN or <= >= to find the dates between
EXAMPLE:
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
OR
SELECT column_name(s)
FROM table_name
WHERE column_name
value1 >= column_name
AND column_name =< value2
All you have to do is just change the hard coded value in the code provided below
DECLARE #firstDate datetime
DECLARE #secondDate datetime
DECLARE #totalDays INT
SELECT #firstDate = getDate() - 30
SELECT #secondDate = getDate()
DECLARE #index INT
SELECT #index = 0
SELECT #totalDays = datediff(day, #firstDate, #secondDate)
CREATE TABLE #temp
(
ID INT NOT NULL IDENTITY(1,1)
,CommonDate DATETIME NULL
)
WHILE #index < #totalDays
BEGIN
INSERT INTO #temp (CommonDate) VALUES (DATEADD(Day, #index, #firstDate))
SELECT #index = #index + 1
END
SELECT CONVERT(VARCHAR(10), CommonDate, 102) as [Date Between] FROM #temp
DROP TABLE #temp
A Bit late to the party, but I like this solution quite a bit.
CREATE FUNCTION ExplodeDates(#startDate DateTime, #endDate DateTime)
RETURNS table as
return (
SELECT TOP (DATEDIFF(DAY, #startDate, #endDate) + 1)
DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, #startDate) AS DATE
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
)
Before you use my function, you need to set up a "helper" table, you only need to do this one time per database:
CREATE TABLE Numbers
(Number int NOT NULL,
CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
DECLARE #x int
SET #x=0
WHILE #x<8000
BEGIN
SET #x=#x+1
INSERT INTO Numbers VALUES (#x)
END
here is the function:
CREATE FUNCTION dbo.ListDates
(
#StartDate char(10)
,#EndDate char(10)
)
RETURNS
#DateList table
(
Date datetime
)
AS
BEGIN
IF ISDATE(#StartDate)!=1 OR ISDATE(#EndDate)!=1
BEGIN
RETURN
END
INSERT INTO #DateList
(Date)
SELECT
CONVERT(datetime,#StartDate)+n.Number-1
FROM Numbers n
WHERE Number<=DATEDIFF(day,#StartDate,CONVERT(datetime,#EndDate)+1)
RETURN
END --Function
use this:
select * from dbo.ListDates('2010-01-01', '2010-01-13')
output:
Date
-----------------------
2010-01-01 00:00:00.000
2010-01-02 00:00:00.000
2010-01-03 00:00:00.000
2010-01-04 00:00:00.000
2010-01-05 00:00:00.000
2010-01-06 00:00:00.000
2010-01-07 00:00:00.000
2010-01-08 00:00:00.000
2010-01-09 00:00:00.000
2010-01-10 00:00:00.000
2010-01-11 00:00:00.000
2010-01-12 00:00:00.000
2010-01-13 00:00:00.000
(13 row(s) affected)
Perhaps if you wish to go an easier way, this should do it.
WITH date_range (calc_date) AS (
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP) - 6, 0)
UNION ALL SELECT DATEADD(DAY, 1, calc_date)
FROM date_range
WHERE DATEADD(DAY, 1, calc_date) < CURRENT_TIMESTAMP)
SELECT calc_date
FROM date_range;
But the temporary table is a very good approach also. Perhaps shall you also consider a populated calendar table.
Definately a numbers table, though tyou may want to use Mark Redman's idea of a CLR proc/assembly if you really need the performance.
How to create the table of dates (and a super fast way to create a numbers table)
/*Gets a list of integers into a temp table (Jeff Moden's idea from SqlServerCentral.com)*/
SELECT TOP 10950 /*30 years of days*/
IDENTITY(INT,1,1) as N
INTO #Numbers
FROM Master.dbo.SysColumns sc1,
Master.dbo.SysColumns sc2
/*Create the dates table*/
CREATE TABLE [TableOfDates](
[fld_date] [datetime] NOT NULL,
CONSTRAINT [PK_TableOfDates] PRIMARY KEY CLUSTERED
(
[fld_date] ASC
)WITH FILLFACTOR = 99 ON [PRIMARY]
) ON [PRIMARY]
/*fill the table with dates*/
DECLARE #daysFromFirstDateInTheTable int
DECLARE #firstDateInTheTable DATETIME
SET #firstDateInTheTable = '01/01/1998'
SET #daysFromFirstDateInTheTable = (SELECT (DATEDIFF(dd, #firstDateInTheTable ,GETDATE()) + 1))
INSERT INTO
TableOfDates
SELECT
DATEADD(dd,nums.n - #daysFromFirstDateInTheTable, CAST(FLOOR(CAST(GETDATE() as FLOAT)) as DateTime)) as FLD_Date
FROM #Numbers nums
Now that you have a table of dates, you can use a function (NOT A PROC) like KM's to get the table of them.
CREATE FUNCTION dbo.ListDates
(
#StartDate DATETIME
,#EndDate DATETIME
)
RETURNS
#DateList table
(
Date datetime
)
AS
BEGIN
/*add some validation logic of your own to make sure that the inputs are sound.Adjust the rest as needed*/
INSERT INTO
#DateList
SELECT FLD_Date FROM TableOfDates (NOLOCK) WHERE FLD_Date >= #StartDate AND FLD_Date <= #EndDate
RETURN
END
Declare #date1 date = '2016-01-01'
,#date2 date = '2016-03-31'
,#date_index date
Declare #calender table (D date)
SET #date_index = #date1
WHILE #date_index<=#date2
BEGIN
INSERT INTO #calender
SELECT #date_index
SET #date_index = dateadd(day,1,#date_index)
IF #date_index>#date2
Break
ELSE
Continue
END
-- ### Six of one half dozen of another. Another method assuming MsSql
Declare #MonthStart datetime = convert(DateTime,'07/01/2016')
Declare #MonthEnd datetime = convert(DateTime,'07/31/2016')
Declare #DayCount_int Int = 0
Declare #WhileCount_int Int = 0
set #DayCount_int = DATEDIFF(DAY, #MonthStart, #MonthEnd)
select #WhileCount_int
WHILE #WhileCount_int < #DayCount_int + 1
BEGIN
print convert(Varchar(24),DateAdd(day,#WhileCount_int,#MonthStart),101)
SET #WhileCount_int = #WhileCount_int + 1;
END;
In case you want to print years starting from a particular year till current date. Just altered the accepted answer.
WITH mycte AS
(
SELECT YEAR(CONVERT(DATE, '2006-01-01',102)) DateValue
UNION ALL
SELECT DateValue + 1
FROM mycte
WHERE DateValue + 1 < = YEAR(GETDATE())
)
SELECT DateValue
FROM mycte
OPTION (MAXRECURSION 0)
This query works on Microsoft SQL Server.
select distinct format( cast('2010-01-01' as datetime) + ( a.v / 10 ), 'yyyy-MM-dd' ) as aDate
from (
SELECT ones.n + 10 * tens.n + 100 * hundreds.n + 1000 * thousands.n as v
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),
(VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) tens(n),
(VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) hundreds(n),
(VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) thousands(n)
) a
where format( cast('2010-01-01' as datetime) + ( a.v / 10 ), 'yyyy-MM-dd' ) < cast('2010-01-13' as datetime)
order by aDate asc;
Now let's look at how it works.
The inner query merely returns a list of integers from 0 to 9999. It will give us a range of 10,000 values for calculating dates. You can get more dates by adding rows for ten_thousands and hundred_thousands and so forth.
SELECT ones.n + 10 * tens.n + 100 * hundreds.n + 1000 * thousands.n as v
FROM (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),
(VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) tens(n),
(VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) hundreds(n),
(VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) thousands(n)
) a;
This part converts the string to a date and adds a number to it from the inner query.
cast('2010-01-01' as datetime) + ( a.v / 10 )
Then we convert the result into the format you want. This is also the column name!
format( cast('2010-01-01' as datetime) + ( a.v / 10 ), 'yyyy-MM-dd' )
Next we extract only the distinct values and give the column name an alias of aDate.
distinct format( cast('2010-01-01' as datetime) + ( a.v / 10 ), 'yyyy-MM-dd' ) as aDate
We use the where clause to filter in only dates within the range you want. Notice that we use the column name here since SQL Server does not accept the column alias, aDate, within the where clause.
where format( cast('2010-01-01' as datetime) + ( a.v / 10 ), 'yyyy-MM-dd' ) < cast('2010-01-13' as datetime)
Lastly, we sort the results.
order by aDate asc;
if you're in a situation like me where procedures and functions are prohibited, and your sql user does not have permissions for insert, therefore insert not allowed, also "set/declare temporary variables like #c is not allowed", but you want to generate a list of dates in a specific period, say current year to do some aggregation, use this
select * from
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) gen_date from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where gen_date between '2017-01-01' and '2017-12-31'
WITH TEMP (DIA, SIGUIENTE_DIA ) AS
(SELECT
1,
CAST(#FECHAINI AS DATE)
FROM
DUAL
UNION ALL
SELECT
DIA,
DATEADD(DAY, DIA, SIGUIENTE_DIA)
FROM
TEMP
WHERE
DIA < DATEDIFF(DAY, #FECHAINI, #FECHAFIN)
AND DATEADD(DAY, 1, SIGUIENTE_DIA) <= CAST(#FECHAFIN AS DATE)
)
SELECT
SIGUIENTE_DIA AS CALENDARIO
FROM
TEMP
ORDER BY
SIGUIENTE_DIA
The detail is on the table DUAL but if your exchange this table for a dummy table this works.
SELECT dateadd(dd,DAYS,'2013-09-07 00:00:00') DATES
INTO #TEMP1
FROM
(SELECT TOP 365 colorder - 1 AS DAYS from master..syscolumns
WHERE id = -519536829 order by colorder) a
WHERE datediff(dd,dateadd(dd,DAYS,'2013-09-07 00:00:00'),'2013-09-13 00:00:00' ) >= 0
AND dateadd(dd,DAYS,'2013-09-07 00:00:00') <= '2013-09-13 00:00:00'
SELECT * FROM #TEMP1
Answer is avialbe here
How to list all dates between two dates
Create Procedure SelectDates(#fromDate Date, #toDate Date)
AS
BEGIN
SELECT DATEADD(DAY,number,#fromDate) [Date]
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(DAY,number,#fromDate) < #toDate
END
DECLARE #StartDate DATE = '2017-09-13', #EndDate DATE = '2017-09-16'
SELECT date FROM ( SELECT DATE = DATEADD(DAY, rn - 1, #StartDate) FROM (
SELECT TOP (DATEDIFF(DAY, #StartDate, DATEADD(DAY,1,#EndDate)))
rn = ROW_NUMBER() OVER (ORDER BY s1.[object_id])
FROM sys.all_objects AS s1
CROSS JOIN sys.all_objects AS s2
ORDER BY s1.[object_id] ) AS x ) AS y
Result:
2017-09-13
2017-09-14
2017-09-15
2017-09-16