-2

I tried using Cross Join but it takes 5 minutes to run, there is another way to do that? Note: I'm comparing dates and Int Fields, Table 1 has records from a system and table 2 store a work calendar dates. SQL Server.

B FILE

C FILE

Trying to achieve

SampleCode

JC_BI
  • 419
  • 1
  • 5
  • 16
  • 1
    please create a very simple sample dataset and add it to the post to illustrate your problem and what you are hoping to achieve. – Tanner Feb 21 '18 at 14:28
  • what is being compared? just because the relationship doens't exist doesn't mean you can't do a `full outer join` on the columns you think should match... it will just be slower... than if a relationship existed... you could also use [set based operations](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql) such as except or intersect... As it stand the question is too vague to provide any real help. – xQbert Feb 21 '18 at 14:30
  • jcaceres, please setup some example data and explain the over all issue. Yesterday you posted some of a query but without more scope and understanding of the over all goal and what the data looks like we're only guessing at what you're trying to accomplish, give us some data and table def - What is the relationship between int and date columns? – Random_User Feb 21 '18 at 14:38
  • Ok, give some moment... – JC_BI Feb 21 '18 at 14:39
  • Depends what you mean by "compare". – Tab Alleman Feb 21 '18 at 14:49
  • jcaceres, you're asking for tsql help, we need tsql examples. file links with no description of the columns will get a bunch of downvotes and the thread will get put on hold. I don't understand your result set, how does `resultset4` relate to the other two data sets? – Random_User Feb 21 '18 at 15:23
  • I've also noticed the dates you are trying to parse start prior to your calendar table. – Random_User Feb 21 '18 at 15:24
  • I just added the sampleCode... – JC_BI Feb 21 '18 at 15:28
  • yesterday you indicated one was larger, which one is larger b or c? – Random_User Feb 21 '18 at 15:38
  • B is the larger, has 40k records. – JC_BI Feb 21 '18 at 15:40

1 Answers1

1

You don't make it easy, but here is a working solution. In the future try to use this type of code in your question so we can focus on the query and solution. I have edited some of your dates to make the example work.

EDIT: New Code

declare @cal table (
    calID int not null
,   date_ date not null
,   isWeekday bit not null
,   isHoliday bit not null
,   year_  int not null
);

insert into @cal (calID, date_, isWeekday, isHoliday, year_)
select 1,    '1-Jan-2010',  1,  1,  2010 union all
select 2,    '2-Jan-2010',  0,  0,  2010 union all
select 3,    '3-Jan-2010',  0,  0,  2010 union all
select 4,    '4-Jan-2010',  1,  0,  2010 union all
select 5,    '5-Jan-2010',  1,  0,  2010 union all
select 6,    '6-Jan-2010',  1,  0,  2010 union all
select 7,    '7-Jan-2010',  1,  0,  2010 union all
select 8,    '8-Jan-2010',  1,  0,  2010 union all
select 9,    '9-Jan-2010',  0,  0,  2010 union all
select 10,  '10-Jan-2010',  0,  0,  2010 union all
select 11,  '11-Jan-2010',  1,  0,  2010 union all
select 12,  '12-Jan-2010',  1,  0,  2010 union all
select 13,  '13-Jan-2010',  1,  0,  2010 union all
select 14,  '14-Jan-2010',  1,  0,  2010 union all
select 15,  '15-Jan-2010',  1,  0,  2010 union all
select 16,  '16-Jan-2010',  0,  0,  2010 union all
select 17,  '17-Jan-2010',  0,  0,  2010 union all
select 18,  '18-Jan-2010',  1,  1,  2010 union all
select 19,  '19-Jan-2010',  1,  0,  2010 union all
select 20,  '20-Jan-2010',  1,  0,  2010 union all
select 21,  '21-Jan-2010',  1,  0,  2010 union all
select 22,  '22-Jan-2010',  1,  0,  2010 union all
select 23,  '23-Jan-2010',  0,  0,  2010 union all
select 24,  '24-Jan-2010',  0,  0,  2010 union all
select 25,  '25-Jan-2010',  1,  0,  2010 union all
select 26,  '26-Jan-2010',  1,  0,  2010;

declare @date table(
    dateID int identity(1,1) not null
,   date2 date null
,   date3 date null
,   date4 date null
,   date5 date null
);

insert into @date (date2, date3, date4, date5)
select '6/20/2009', NULL,   NULL,   '7/19/2009'   union all
select '1/2/2010',  NULL,   NULL,   '1/19/2010'   union all
select '1/4/2010',  NULL,   NULL,   '1/15/2010'  union all
select '1/2/2010',  NULL,   NULL,   '1/22/2010'  union all
select '9/17/2009', NULL,   NULL,   '10/26/2009'  union all
select '6/4/2009',  NULL,   NULL,   '6/24/2009';

;with cte as (
        select dateid
             , b.date_
          from @date
         cross apply ( 
                        Select Top (DateDiff(DAY,date2,IsNull(date5,date2))+1) DateAdd(DAY, -1+Row_Number() Over (Order By 1/0),date2) date_
                          from master..spt_values n1
                        ) b
            )

select distinct b.dateID
     , c.date2
     , c.date5
     , count(*) over(order by b.dateid) cnt
  from @cal a
  join cte b
    on a.date_ = b.date_
  join @date c
    on b.dateid = c.dateid
 where isWeekday = 1
   and isHoliday = 0

you could change out the from master..spt_values n1

for something like this:

 ;with E00(n) as (select 1 union all select 1)
     , E02(n) as (select 1 from E00 a, E00 b)
     , E04(n) as (select 1 from E02 a, E02 b)
     , E08(n) as (select 1 from E04 a, E04 b)
     , E16(n) as (select 1 from E08 a, E08 b)
     , E32(n) as (select 1 from E16 a, E16 b)
, cteTally(d) as (select row_number() over (order by n) from E32)

        , cte as (
       select dateid
            , b.date_
         from @date
  cross apply   ( 
                        select top (datediff(day,date2,isnull(date5,date2))+1) dateadd(day, -1+row_number() over(order by 1/0),date2) date_
                          from cteTally
                        ) b
                )

       select distinct b.dateID
            , c.date2
            , c.date5
            , count(*) over(order by b.dateid) cnt
         from @cal a
         join cte b
           on a.date_ = b.date_
         join @date c
           on b.dateid = c.dateid
        where isWeekday = 1
          and isHoliday = 0
Random_User
  • 363
  • 1
  • 7
  • I'm trying to calculate the dates between the calendar which is C and some dates pull out from a system which is B, the result should be numbers as I said in trying to achieve file. The code I'm using is in sampleCode link. – JC_BI Feb 21 '18 at 16:05
  • okay so you want to count the days between two dates? Did I see where you only want to count the days where they are weekdays from another post? – Random_User Feb 21 '18 at 16:21
  • Exactly, that's why I'm using a sum. – JC_BI Feb 21 '18 at 16:24
  • Based on the results my code is running well, but is taking long time to execute, that's why I'm looking for a solution to fix that. – JC_BI Feb 21 '18 at 16:33
  • I've edited the sample code, this isn't the whole solution but you should be able to figure it out from there, I've focused on the difference between date2 and date5. Also your calendar data needs to be updated, you have dates from 2009 and the sample you gave starts in 2010 – Random_User Feb 21 '18 at 16:38
  • @jcaceres did that get the answer for you? this is what you were trying to achieve yes? – Random_User Feb 21 '18 at 16:54
  • I don't know exactly what is master..spt_values? – JC_BI Feb 21 '18 at 16:59
  • it is a table in the database that is often used as a make shift tally table, in this instance it is more of a patsy table. Meaning the table is not used but the form requires a table to be used. – Random_User Feb 21 '18 at 17:03
  • I'm doing the test with my tables and I'm getting this error:Msg 206, Level 16, State 2, Line 1 Operand type clash: date is incompatible with int – JC_BI Feb 21 '18 at 17:13
  • I'm trying to do the insert from my tables and I get that : (7670 row(s) affected) (39415 row(s) affected) Msg 1014, Level 15, State 1, Line 26 A TOP or FETCH clause contains an invalid value. – JC_BI Feb 21 '18 at 17:47
  • I don't understand, why are you doing an insert? You will want to apply the sample code to your current tables. – Random_User Feb 21 '18 at 19:25