How to weekly group if I have daily data - sql

i have following my table structure and data
CREATE TABLE [dbo].[Pairs_Details](
[sno] [int] IDENTITY(1,1) NOT NULL,
[userid] [nvarchar](50) NULL,
[date] [datetime] NULL,
[ljoin] [int] NULL,
[rjoin] [int] NULL
) ON [PRIMARY]
sno userid date ljoin rjoin
1 LDS 2014-02-17 00:00:00.000 1 NULL
2 LDS 2014-02-17 00:00:00.000 NULL 1
3 LDS1 2014-02-18 00:00:00.000 1 NULL
4 LDS 2014-02-18 00:00:00.000 1 NULL
5 LDS1 2014-02-18 00:00:00.000 NULL 1
6 LDS 2014-02-18 00:00:00.000 1 NULL
7 SUNIL1 2014-02-19 00:00:00.000 1 NULL
8 LDS1 2014-02-19 00:00:00.000 1 NULL
9 LDS 2014-02-19 00:00:00.000 1 NULL
10 SUNIL1 2014-02-19 00:00:00.000 NULL 1
11 LDS1 2014-02-19 00:00:00.000 1 NULL
12 LDS 2014-02-19 00:00:00.000 1 NULL
13 SUNIL2 2014-02-19 00:00:00.000 1 NULL
14 LDS1 2014-02-19 00:00:00.000 NULL 1
15 LDS 2014-02-19 00:00:00.000 1 NULL
16 rajesh123 2014-02-19 00:00:00.000 1 NULL
17 SUNIL1 2014-02-19 00:00:00.000 NULL 1
18 LDS1 2014-02-19 00:00:00.000 1 NULL
19 LDS 2014-02-19 00:00:00.000 1 NULL
20 SUNIL2 2014-02-19 00:00:00.000 NULL 1
21 LDS1 2014-02-19 00:00:00.000 NULL 1
22 LDS 2014-02-19 00:00:00.000 1 NULL
23 LDS2 2014-02-19 00:00:00.000 1 NULL
24 LDS 2014-02-19 00:00:00.000 NULL 1
25 SUNIL1 2014-02-20 00:00:00.000 NULL 1
26 LDS1 2014-02-20 00:00:00.000 1 NULL
27 LDS 2014-02-20 00:00:00.000 1 NULL
28 rajesh123 2014-02-20 00:00:00.000 NULL 1
29 SUNIL1 2014-02-20 00:00:00.000 NULL 1
30 LDS1 2014-02-20 00:00:00.000 1 NULL
31 LDS 2014-02-20 00:00:00.000 1 NULL
32 LDS 2014-02-24 00:00:00.000 NULL 1
33 Jitendra123 2014-02-27 00:00:00.000 1 NULL
34 LDS2 2014-02-27 00:00:00.000 1 NULL
35 LDS 2014-02-27 00:00:00.000 NULL 1
36 rajeev123 2014-02-27 00:00:00.000 1 NULL
37 Jitendra123 2014-02-27 00:00:00.000 1 NULL
40 jyoti123 2014-02-27 00:00:00.000 1 NULL
41 SUNIL1 2014-02-27 00:00:00.000 1 NULL
42 LDS1 2014-02-27 00:00:00.000 1 NULL
43 LDS 2014-02-27 00:00:00.000 1 NULL
44 meeta 2014-03-01 00:00:00.000 1 NULL
45 jyoti123 2014-03-01 00:00:00.000 1 NULL
46 SUNIL1 2014-03-01 00:00:00.000 1 NULL
47 LDS1 2014-03-01 00:00:00.000 1 NULL
48 LDS 2014-03-01 00:00:00.000 1 NULL
38 LDS2 2014-02-27 00:00:00.000 1 NULL
39 LDS 2014-02-27 00:00:00.000 NULL 1
and this is my stored procdure .
create proc [dbo].[pair_Scounting]
(
#userid nvarchar(50),
#start_date datetime,
#end_date datetime
)
as
begin
SET DATEFIRST 1;
SELECT userid,
Sum(ISNULL(ljoin,0)) AS ljoin,
Sum(ISNULL(rjoin,0)) AS rjoin, DATEPART(wk, Date) AS WeekNumber,
CASE
WHEN YEAR(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date]))) < YEAR(Min([date]))
THEN CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0,DATEADD(YEAR, 0 ,GETDATE())), 0) AS Varchar(50)) + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date])) AS Varchar(50))
ELSE
Cast(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date])) AS Varchar(50)) + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date])) AS Varchar(50))
END DateRange,
Case
When Sum(ISNULL(ljoin,0)) < Sum(ISNULL(rjoin,0)) Then Sum(ISNULL(ljoin,0))
When Sum(ISNULL(rjoin,0)) < Sum(ISNULL(ljoin,0)) Then Sum(ISNULL(rjoin,0))
Else (Sum(ISNULL(rjoin,0))-1)
End pair
FROM Pairs_Details where userid=#userid and date between #start_date and #end_date
Group By userid,DATEPART(wk, Date)
end
GO
if i execute my stored procedure as follows it return following result
exec pair_Scounting 'LDS','2014-02-17','2014-02-28'
userid ljoin rjoin WeekNumber DateRange pair
LDS 10 2 8 Feb 17 2014 12:00AM TO Feb 23 2014 12:00AM 2
LDS 1 3 9 Feb 24 2014 12:00AM TO Mar 2 2014 12:00AM 1
but i want if i execute my stored procedure it return my desired result as follows
please read carefully above my stored procedure which is used to count pair weekly
my desired output following
userid ljoin rjoin WeekNumber DateRange pair
LDS 10 2 8 Feb 17 2014 12:00AM TO Feb 23 2014 12:00AM 2
LDS 9 3 9 Feb 24 2014 12:00AM TO Mar 2 2014 12:00AM 3
if (ljoin>rjoin) then ljoin-rjoin(10-2=8) value must be added to ljoin of next week and
so ljoin of next week from Feb 24 2014 12:00AM TO Mar 2 2014 12:00AM is 8+1=9 according my data
if (rjoin>ljoin) then rjoin-ljoin value must be added to rjoin of next week
so how it can be done please any can suggest us.
thanks

Related

Replacement of values by logical condition by groups in SQL Server

Here's my sample data
shop_code product_id doc_date ship_count mark_1 outputer y
-----------------------------------------------------------------------------
1 00664НСК 11628 2015-01-03 00:00:00.000 12 1 8 1
2 00664НСК 11628 2015-01-05 00:00:00.000 7 1 8 1
3 00664НСК 11628 2015-01-06 00:00:00.000 24 0 8 1
4 00664НСК 11628 2015-01-07 00:00:00.000 18 1 8 1
5 00664НСК 11628 2015-01-08 00:00:00.000 12 1 8 1
6 00664НСК 11628 2015-01-09 00:00:00.000 18 0 8 1
7 00664НСК 11628 2015-01-10 00:00:00.000 6 0 6 1
8 00664НСК 11628 2015-01-11 00:00:00.000 6 1 6 1
9 00664НСК 11628 2015-01-12 00:00:00.000 6 1 6 1
10 00664НСК 11628 2015-01-13 00:00:00.000 18 1 12 0
11 00664НСК 11628 2015-01-14 00:00:00.000 6 1 6 0
12 00664НСК 11628 2015-01-15 00:00:00.000 18 1 12 0
13 00664НСК 11628 2015-01-16 00:00:00.000 12 1 12 1
14 00664НСК 11628 2015-01-17 00:00:00.000 18 1 12 1
15 00664НСК 11628 2015-01-18 00:00:00.000 12 1 12 1
16 00664НСК 11628 2015-01-19 00:00:00.000 10 1 10 0
17 00664НСК 11628 2015-01-20 00:00:00.000 24 1 12 0
18 00664НСК 11628 2015-01-21 00:00:00.000 6 1 6 0
19 00664НСК 11628 2015-01-24 00:00:00.000 6 1 6 0
20 00664НСК 11628 2015-01-25 00:00:00.000 6 0 6 0
21 00664НСК 11628 2015-01-26 00:00:00.000 10 0 10 1
22 00664НСК 11628 2015-01-27 00:00:00.000 6 1 6 0
23 00664НСК 11628 2015-01-28 00:00:00.000 10 1 10 0
24 00664НСК 11628 2015-01-29 00:00:00.000 70 0 12 1
25 00664НСК 11628 2015-01-30 00:00:00.000 100 1 12 1
Similar question I have asked for R and got working solution, but now I want do it using T-SQL.
I need observe such a condition: if y = 1 and mark1 = 1, then the output by mark1=1 must be replaced by the first value that goes for y = 0 and mark1 = 1 in the output variable.
If the first value that goes for Y = 0 and mark1 = 1 in the output is more than the ship_count, then in output left the actual value of ship_count
Zero category of mark1 for output, we don't touch.
This operation must be done by group ship_code+product_id
So the desired output should look like this:
shop_code product_id doc_date ship_count mark_1 outputer y
----------------------------------------------------------------------------
1 00664НСК 11628 2015-01-03 00:00:00.000 12 1 *12 1
2 00664НСК 11628 2015-01-05 00:00:00.000 7 1 *7 1
3 00664НСК 11628 2015-01-06 00:00:00.000 24 0 24 1
4 00664НСК 11628 2015-01-07 00:00:00.000 18 1 *12 1
5 00664НСК 11628 2015-01-08 00:00:00.000 12 1 *12 1
6 00664НСК 11628 2015-01-09 00:00:00.000 18 0 18 1
7 00664НСК 11628 2015-01-10 00:00:00.000 6 0 6 1
8 00664НСК 11628 2015-01-11 00:00:00.000 6 1 6 1
9 00664НСК 11628 2015-01-12 00:00:00.000 6 1 6 1
10 00664НСК 11628 2015-01-13 00:00:00.000 18 1 *12 0
11 00664НСК 11628 2015-01-14 00:00:00.000 6 1 6 0
12 00664НСК 11628 2015-01-15 00:00:00.000 18 1 12 0
13 00664НСК 11628 2015-01-16 00:00:00.000 12 1 *10 1
14 00664НСК 11628 2015-01-17 00:00:00.000 18 1 *10 1
15 00664НСК 11628 2015-01-18 00:00:00.000 12 1 *10 1
16 00664НСК 11628 2015-01-19 00:00:00.000 10 1 10 0
17 00664НСК 11628 2015-01-20 00:00:00.000 24 1 12 0
18 00664НСК 11628 2015-01-21 00:00:00.000 6 1 6 0
19 00664НСК 11628 2015-01-24 00:00:00.000 6 1 6 0
20 00664НСК 11628 2015-01-25 00:00:00.000 6 0 6 0
21 00664НСК 11628 2015-01-26 00:00:00.000 10 0 10 1
22 00664НСК 11628 2015-01-27 00:00:00.000 6 1 6 1
23 00664НСК 11628 2015-01-28 00:00:00.000 20 1 *12 0
24 00664НСК 11628 2015-01-29 00:00:00.000 70 1 12 0
25 00664НСК 11628 2015-01-30 00:00:00.000 100 1 12 1
Good evening,
You should use a case statement to do your job.
For finding the first value for the describing clauses , use a subquery in which you keep the order that you wish(order by) and select the top 1 value.
Give a try and if you face some issues ask again.

Dates between two dates from a table

I can't find the specific answer to this question but apologies if it has been asked previously.
I have the following example table which I have kept simple but it contains more rows and Types. It gets updated frequently.
Type From To Qty
1 2016-01-01 00:00:00.0000000 2016-01-03 00:00:00.0000000 30
1 2016-01-04 00:00:00.0000000 2016-01-05 00:00:00.0000000 31
1 2016-01-06 00:00:00.0000000 NULL 31
2 2016-04-24 00:00:00.0000000 NULL 15
I want to be able to update a table every day (as shown below) so it shows all of the dates between (and including) the From and To dates. The Qty for the relevant date must be displayed up to todays date where the TO is NULL.
Type Date Qty
1 2016-01-01 00:00:00.0000000 30
1 2016-01-02 00:00:00.0000000 30
1 2016-01-03 00:00:00.0000000 30
1 2016-04-04 00:00:00.0000000 31
1 2016-04-05 00:00:00.0000000 31
1 2016-04-06 00:00:00.0000000 31
1 2016-04-07 00:00:00.0000000 31
1 .... up to today where TO is NULL
1 2016-07-25 00:00:00.0000000 31
2 2016-04-24 00:00:00.0000000 15
2 .... up to today where TO is NULL
2 2016-07-25 00:00:00.0000000 15
Thank you in advance for your help.
Using Numbers table..
Demo Here
select b.*,qty from #test
cross apply
(
select dateadd(day,n,fromdate) from
numbers
where n<=
case when todate is null
then datediff(day,fromdate,getdate()) else datediff(day,fromdate,todate) end
) b(upd)
You can do this using a recursive CTE to generate all of the dates and JOIN to that for the result:
Test Data
Create Table Test
(
[Type] Int,
[From] Date,
[To] Date,
Qty Int
)
Insert Test
Values
(1, '2016-01-01', '2016-01-03', 30 ),
(1, '2016-01-04', '2016-01-05', 31 ),
(1, '2016-01-06', NULL, 31 ),
(2, '2016-04-24', NULL, 15 )
Query
;With MinMax As
(
Select Min([From]) MinFrom,
Max([To]) MaxTo,
Convert(Date, GetDate()) Today
From Test
), Date (Date) As
(
Select MinFrom
From MinMax
Union All
Select DateAdd(Day, 1, Date)
From Date
Where Date < (Select MaxTo From MinMax)
Or Date < (Select Today From MinMax)
)
Select T.[Type],
D.[Date],
T.Qty
From Test T
Join Date D On D.Date Between T.[From] And Coalesce(T.[To], Convert(Date, GetDate()))
Order By T.[Type], D.[Date]
Option (MaxRecursion 0)
Results
Type Date Qty
1 2016-01-01 30
1 2016-01-02 30
1 2016-01-03 30
1 2016-01-04 31
1 2016-01-05 31
1 2016-01-06 31
1 2016-01-07 31
1 2016-01-08 31
1 2016-01-09 31
1 2016-01-10 31
1 2016-01-11 31
1 2016-01-12 31
1 2016-01-13 31
1 2016-01-14 31
1 2016-01-15 31
1 2016-01-16 31
1 2016-01-17 31
1 2016-01-18 31
1 2016-01-19 31
1 2016-01-20 31
1 2016-01-21 31
1 2016-01-22 31
1 2016-01-23 31
1 2016-01-24 31
1 2016-01-25 31
1 2016-01-26 31
1 2016-01-27 31
1 2016-01-28 31
1 2016-01-29 31
1 2016-01-30 31
1 2016-01-31 31
1 2016-02-01 31
1 2016-02-02 31
1 2016-02-03 31
1 2016-02-04 31
1 2016-02-05 31
1 2016-02-06 31
1 2016-02-07 31
1 2016-02-08 31
1 2016-02-09 31
1 2016-02-10 31
1 2016-02-11 31
1 2016-02-12 31
1 2016-02-13 31
1 2016-02-14 31
1 2016-02-15 31
1 2016-02-16 31
1 2016-02-17 31
1 2016-02-18 31
1 2016-02-19 31
1 2016-02-20 31
1 2016-02-21 31
1 2016-02-22 31
1 2016-02-23 31
1 2016-02-24 31
1 2016-02-25 31
1 2016-02-26 31
1 2016-02-27 31
1 2016-02-28 31
1 2016-02-29 31
1 2016-03-01 31
1 2016-03-02 31
1 2016-03-03 31
1 2016-03-04 31
1 2016-03-05 31
1 2016-03-06 31
1 2016-03-07 31
1 2016-03-08 31
1 2016-03-09 31
1 2016-03-10 31
1 2016-03-11 31
1 2016-03-12 31
1 2016-03-13 31
1 2016-03-14 31
1 2016-03-15 31
1 2016-03-16 31
1 2016-03-17 31
1 2016-03-18 31
1 2016-03-19 31
1 2016-03-20 31
1 2016-03-21 31
1 2016-03-22 31
1 2016-03-23 31
1 2016-03-24 31
1 2016-03-25 31
1 2016-03-26 31
1 2016-03-27 31
1 2016-03-28 31
1 2016-03-29 31
1 2016-03-30 31
1 2016-03-31 31
1 2016-04-01 31
1 2016-04-02 31
1 2016-04-03 31
1 2016-04-04 31
1 2016-04-05 31
1 2016-04-06 31
1 2016-04-07 31
1 2016-04-08 31
1 2016-04-09 31
1 2016-04-10 31
1 2016-04-11 31
1 2016-04-12 31
1 2016-04-13 31
1 2016-04-14 31
1 2016-04-15 31
1 2016-04-16 31
1 2016-04-17 31
1 2016-04-18 31
1 2016-04-19 31
1 2016-04-20 31
1 2016-04-21 31
1 2016-04-22 31
1 2016-04-23 31
1 2016-04-24 31
1 2016-04-25 31
1 2016-04-26 31
1 2016-04-27 31
1 2016-04-28 31
1 2016-04-29 31
1 2016-04-30 31
1 2016-05-01 31
1 2016-05-02 31
1 2016-05-03 31
1 2016-05-04 31
1 2016-05-05 31
1 2016-05-06 31
1 2016-05-07 31
1 2016-05-08 31
1 2016-05-09 31
1 2016-05-10 31
1 2016-05-11 31
1 2016-05-12 31
1 2016-05-13 31
1 2016-05-14 31
1 2016-05-15 31
1 2016-05-16 31
1 2016-05-17 31
1 2016-05-18 31
1 2016-05-19 31
1 2016-05-20 31
1 2016-05-21 31
1 2016-05-22 31
1 2016-05-23 31
1 2016-05-24 31
1 2016-05-25 31
1 2016-05-26 31
1 2016-05-27 31
1 2016-05-28 31
1 2016-05-29 31
1 2016-05-30 31
1 2016-05-31 31
1 2016-06-01 31
1 2016-06-02 31
1 2016-06-03 31
1 2016-06-04 31
1 2016-06-05 31
1 2016-06-06 31
1 2016-06-07 31
1 2016-06-08 31
1 2016-06-09 31
1 2016-06-10 31
1 2016-06-11 31
1 2016-06-12 31
1 2016-06-13 31
1 2016-06-14 31
1 2016-06-15 31
1 2016-06-16 31
1 2016-06-17 31
1 2016-06-18 31
1 2016-06-19 31
1 2016-06-20 31
1 2016-06-21 31
1 2016-06-22 31
1 2016-06-23 31
1 2016-06-24 31
1 2016-06-25 31
1 2016-06-26 31
1 2016-06-27 31
1 2016-06-28 31
1 2016-06-29 31
1 2016-06-30 31
1 2016-07-01 31
1 2016-07-02 31
1 2016-07-03 31
1 2016-07-04 31
1 2016-07-05 31
1 2016-07-06 31
1 2016-07-07 31
1 2016-07-08 31
1 2016-07-09 31
1 2016-07-10 31
1 2016-07-11 31
1 2016-07-12 31
1 2016-07-13 31
1 2016-07-14 31
1 2016-07-15 31
1 2016-07-16 31
1 2016-07-17 31
1 2016-07-18 31
1 2016-07-19 31
1 2016-07-20 31
1 2016-07-21 31
1 2016-07-22 31
1 2016-07-23 31
1 2016-07-24 31
1 2016-07-25 31
1 2016-07-26 31
2 2016-04-24 15
2 2016-04-25 15
2 2016-04-26 15
2 2016-04-27 15
2 2016-04-28 15
2 2016-04-29 15
2 2016-04-30 15
2 2016-05-01 15
2 2016-05-02 15
2 2016-05-03 15
2 2016-05-04 15
2 2016-05-05 15
2 2016-05-06 15
2 2016-05-07 15
2 2016-05-08 15
2 2016-05-09 15
2 2016-05-10 15
2 2016-05-11 15
2 2016-05-12 15
2 2016-05-13 15
2 2016-05-14 15
2 2016-05-15 15
2 2016-05-16 15
2 2016-05-17 15
2 2016-05-18 15
2 2016-05-19 15
2 2016-05-20 15
2 2016-05-21 15
2 2016-05-22 15
2 2016-05-23 15
2 2016-05-24 15
2 2016-05-25 15
2 2016-05-26 15
2 2016-05-27 15
2 2016-05-28 15
2 2016-05-29 15
2 2016-05-30 15
2 2016-05-31 15
2 2016-06-01 15
2 2016-06-02 15
2 2016-06-03 15
2 2016-06-04 15
2 2016-06-05 15
2 2016-06-06 15
2 2016-06-07 15
2 2016-06-08 15
2 2016-06-09 15
2 2016-06-10 15
2 2016-06-11 15
2 2016-06-12 15
2 2016-06-13 15
2 2016-06-14 15
2 2016-06-15 15
2 2016-06-16 15
2 2016-06-17 15
2 2016-06-18 15
2 2016-06-19 15
2 2016-06-20 15
2 2016-06-21 15
2 2016-06-22 15
2 2016-06-23 15
2 2016-06-24 15
2 2016-06-25 15
2 2016-06-26 15
2 2016-06-27 15
2 2016-06-28 15
2 2016-06-29 15
2 2016-06-30 15
2 2016-07-01 15
2 2016-07-02 15
2 2016-07-03 15
2 2016-07-04 15
2 2016-07-05 15
2 2016-07-06 15
2 2016-07-07 15
2 2016-07-08 15
2 2016-07-09 15
2 2016-07-10 15
2 2016-07-11 15
2 2016-07-12 15
2 2016-07-13 15
2 2016-07-14 15
2 2016-07-15 15
2 2016-07-16 15
2 2016-07-17 15
2 2016-07-18 15
2 2016-07-19 15
2 2016-07-20 15
2 2016-07-21 15
2 2016-07-22 15
2 2016-07-23 15
2 2016-07-24 15
2 2016-07-25 15
2 2016-07-26 15

SQL - Compare rows by id, date and amount

I need to SELECT a row in which issue_date = maturity_date of another row with same id, and same amount_usd.
I tried with self join, but I do not get right result.
Here is a simplified version of my table:
ID ISSUE_DATE MATURITY_DATE AMOUNT_USD
1 2010-01-01 00:00:00.000 2015-12-01 00:00:00.000 5000
1 2010-01-01 00:00:00.000 2001-09-19 00:00:00.000 700
2 2014-04-09 00:00:00.000 2019-04-09 00:00:00.000 400
1 2015-12-01 00:00:00.000 2016-12-31 00:00:00.000 5000
5 2015-02-24 00:00:00.000 2015-02-24 00:00:00.000 8000
4 2012-11-29 00:00:00.000 2015-11-29 00:00:00.000 10000
3 2015-01-21 00:00:00.000 2018-01-21 00:00:00.000 17500
2 2015-02-02 00:00:00.000 2015-12-05 00:00:00.000 12000
1 2015-01-12 00:00:00.000 2018-01-12 00:00:00.000 18000
2 2015-12-05 00:00:00.000 2016-01-10 00:00:00.000 12000
Result should be:
ID ISSUE_DATE MATURITY_DATE AMOUNT_USD
1 2015-12-01 00:00:00.000 2016-12-31 00:00:00.000 5000
2 2015-12-05 00:00:00.000 2016-01-10 00:00:00.000 12000
Thanks in advance!
Do following: http://sqlfiddle.com/#!6/c0a02/1
select a.id, a.issue_date, a.maturity_date, a.amount_usd
from tbl a
inner join tbl b
on a.id = b.id
and a.maturity_date = b.issue_date
-- added to prevent same maturity date and issue date
where a.maturity_date <> a.issue_date
Output:
| id | issue_date | maturity_date | amount_usd |
|----|----------------------------|----------------------------|------------|
| 1 | January, 01 2010 00:00:00 | December, 01 2015 00:00:00 | 5000 |
| 2 | February, 02 2015 00:00:00 | December, 05 2015 00:00:00 | 12000 |

inserting result of a function into a table

I get consequent 24 months from the following table valued function. I need to insert these months in another table where the result should be like this:
Week Period
1 2010-02-01
2 2010-02-01
3 2010-02-01
4 2010-02-01
5 2010-03-01
6 2010-03-01
7 2010-03-01
8 2010-03-01
9 2010-03-01
10 2010-04-01
11 2010-04-01
12 2010-04-01
13 2010-04-01
14 2010-05-01
15 2010-05-01
16 2010-05-01
17 2010-05-01
18 2010-06-01
19 2010-06-01
20 2010-06-01
21 2010-06-01
22 2010-06-01
23 2010-07-01
24 2010-07-01
25 2010-07-01
26 2010-07-01
27 2010-08-01
28 2010-08-01
29 2010-08-01
30 2010-08-01
31 2010-09-01
32 2010-09-01
33 2010-09-01
34 2010-09-01
35 2010-09-01
36 2010-10-01
37 2010-10-01
38 2010-10-01
39 2010-10-01
40 2010-11-01
41 2010-11-01
42 2010-11-01
43 2010-11-01
44 2010-12-01
45 2010-12-01
46 2010-12-01
47 2010-12-01
48 2010-12-01
49 2011-01-01
50 2011-01-01
51 2011-01-01
52 2011-01-01
53 2011-02-01
54 2011-02-01
55 2011-02-01
56 2011-02-01
57 2011-03-01
58 2011-03-01
59 2011-03-01
60 2011-03-01
61 2011-03-01
62 2011-04-01
63 2011-04-01
64 2011-04-01
65 2011-04-01
66 2011-05-01
67 2011-05-01
68 2011-05-01
69 2011-05-01
70 2011-06-01
71 2011-06-01
72 2011-06-01
73 2011-06-01
74 2011-06-01
75 2011-07-01
76 2011-07-01
77 2011-07-01
78 2011-07-01
79 2011-08-01
80 2011-08-01
81 2011-08-01
82 2011-08-01
83 2011-09-01
84 2011-09-01
85 2011-09-01
86 2011-09-01
87 2011-09-01
88 2011-10-01
89 2011-10-01
90 2011-10-01
91 2011-10-01
92 2011-11-01
93 2011-11-01
94 2011-11-01
95 2011-11-01
96 2011-12-01
97 2011-12-01
98 2011-12-01
99 2011-12-01
100 2011-12-01
101 2012-01-01
102 2012-01-01
103 2012-01-01
104 2012-01-01
My function is as follows:
ALTER FUNCTION [dbo].[sun_care1](#start_period date, #end_period date)
RETURNS #date TABLE (
Period date NOT NULL
)
AS
BEGIN
--& DATEADD (MONTH,1,#start_period)
WHILE (#end_period <= #start_period) BEGIN
SET #end_period= DATEADD(MONTH,1,#end_period)
--set #start_period =CAST(#start_period as varchar(max))
INSERT INTO #date VALUES (#end_period)
END;
RETURN;
END;
In SQLServer2005+ use CTE and master..spt_values system table. Also you can use Sequence tables instead of master..spt_values system table.
Modified function
ALTER FUNCTION [dbo].[sun_care1](#start_period date, #end_period date)
RETURNS #date TABLE (Period date NOT NULL)
AS
BEGIN
--& DATEADD (MONTH,1,#start_period)
WHILE (#start_period <= #end_period)
BEGIN
SET #start_period= DATEADD(MONTH, 1, #start_period)
--set #start_period =CAST(#start_period as varchar(max))
INSERT INTO #date
VALUES(#start_period)
END;
RETURN;
END;
INSERT statement with using CTE and master..spt_values system table
DECLARE #start_period date = '20100101',
#end_period date = '20111201'
;WITH cte AS
(
SELECT -1 AS rn
UNION ALL
SELECT rn + 3
FROM cte
WHERE rn < DATEDIFF(MONTH, #start_period, #end_period) * 3
), cte2 AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY Period) AS rn
FROM [dbo].[sun_care1](#start_period, #end_period)
)
INSERT AnotherTableName(columnName)
SELECT Period
FROM cte2 s LEFT JOIN cte c ON s.rn = c.rn
CROSS APPLY (
SELECT number
FROM master..spt_values v
WHERE v.type = 'P'
AND v.number < CASE WHEN c.rn IS NULL
THEN 4 ELSE 5 END
) o

How can I join two queries?

How can i join these two queries (Query1-Query2)
Query1:
declare #date1 datetime,#date2 datetime , #COUNT INT , #countgap int
seLECT #date1='2009-05-11' , #date2 = '2009-05-12'
seLECT #countgap = 30 , #COUNT = 0
select #date1 TARIH , #COUNT SIRA
INTO #TMP
WHILE #date1 < #date2
BEGIN
SELECT #date1 = DATEadd(minute,#countgap,#date1) , #COUNT = #COUNT +1
INSERT INTO #TMP
select #date1 TARIH , #COUNT SIRA
END
SELECT TARIH , SIRA , ( 0) VISITINGCOUNT
FROM #TMP
ORDER BY SIRA
Query2 :
select count(page) as TARIH,
(datepart(hour,Date)*60+datepart(minute,Date))/#countgap as SIRA
from scr_SecuristLog
where Date between #date1 and #date2
GROUP BY (datepart(hour,Date)*60+datepart(minute,Date))/#countgap
Look please 'SIRA'. This Column equals in two datatable.
Run Query1 look result please. i need inner join two table
Query1 Result
2009-05-11 00:00:00.000 0 0
.
.
.
.
.
2009-05-11 07:00:00.000 14 0
2009-05-11 07:30:00.000 15 0
2009-05-11 08:00:00.000 16 0
2009-05-11 08:30:00.000 17 0
2009-05-11 09:00:00.000 18 0
2009-05-11 09:30:00.000 19 0
2009-05-11 10:00:00.000 20 0
2009-05-11 10:30:00.000 21 0
2009-05-11 11:00:00.000 22 0
2009-05-11 11:30:00.000 23 0
2009-05-11 12:00:00.000 24 0
2009-05-11 12:30:00.000 25 0
2009-05-11 13:00:00.000 26 0
2009-05-11 13:30:00.000 27 0
2009-05-11 14:00:00.000 28 0
2009-05-11 14:30:00.000 29 0
2009-05-11 15:00:00.000 30 0
2009-05-11 15:30:00.000 31 0
2009-05-11 16:00:00.000 32 0
2009-05-11 16:30:00.000 33 0
2009-05-11 17:00:00.000 34 0
2009-05-11 17:30:00.000 35 0
2009-05-11 18:00:00.000 36 0
.
.
.
Query2 Result
3 23
9 29
10 32
3 21
18 33
i need Result
2009-05-11 00:00:00.000 0 0
....
2009-05-11 07:00:00.000 14 0
2009-05-11 07:30:00.000 15 0
2009-05-11 08:00:00.000 16 0
2009-05-11 08:30:00.000 17 0
2009-05-11 09:00:00.000 18 33
2009-05-11 09:30:00.000 19 0
2009-05-11 10:00:00.000 20 0
2009-05-11 10:30:00.000 21 0
2009-05-11 11:00:00.000 22 0
2009-05-11 11:30:00.000 23 3
2009-05-11 12:00:00.000 24 0
2009-05-11 12:30:00.000 25 0
2009-05-11 13:00:00.000 26 0
2009-05-11 13:30:00.000 27 0
2009-05-11 14:00:00.000 28 0
2009-05-11 14:30:00.000 29 9
2009-05-11 15:00:00.000 30 0
2009-05-11 15:30:00.000 31 0
2009-05-11 16:00:00.000 32 10
2009-05-11 16:30:00.000 33 0
2009-05-11 17:00:00.000 34 0
2009-05-11 17:30:00.000 35 0
2009-05-11 18:00:00.000 36 0
...
A bit tricky, as the field names doesn't match between the tables...
select t1.TARIH, t1.SIRA, VISITINGCOUNT = isnull(t2.SIRA, 0)
from #TMP t1
left join ( --paste query 2 here-- ) t2 on t2.TARIH = t1.SIRA
order by t1.SIRA