Joining Calendar tableWhat is the difference between “INNER JOIN” and “OUTER JOIN”?Add a column with a default value to an existing table in SQL ServerTable Naming Dilemma: Singular vs. Plural NamesLEFT JOIN vs. LEFT OUTER JOIN in SQL ServerDifference between JOIN and INNER JOINInsert results of a stored procedure into a temporary tableImprove INSERT-per-second performance of SQLite?Finding duplicate values in a SQL tableWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?To get the join columns from a SQL (query) using PYparsing, python script

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

Is it improper etiquette to ask your opponent what his/her rating is before the game?

How to set Output path correctly for a Single Image render?

Did arcade monitors have same pixel aspect ratio as TV sets?

A social experiment. What is the worst that can happen?

Is Asuka Langley-Soryu disgusted by Shinji?

Interest Rate Futures Question from Hull, 8e

Why did the EU agree to delay the Brexit deadline?

Has Darkwing Duck ever met Scrooge McDuck?

Is it possible to have a strip of cold climate in the middle of a planet?

How do I repair my stair bannister?

Proving a function is onto where f(x)=|x|.

Can someone explain how this makes sense electrically?

Translation of Scottish 16th century church stained glass

Schmidt decomposition - example

Should I install hardwood flooring or cabinets first?

Why does Async/Await work properly when the loop is inside the async function and not the other way around?

How much character growth crosses the line into breaking the character

Does a 'pending' US visa application constitute a denial?

Greco-Roman egalitarianism

Varistor? Purpose and principle

Fuse symbol on toroidal transformer

Will adding a BY-SA image to a blog post make the entire post BY-SA?

Query about absorption line spectra



Joining Calendar table


What is the difference between “INNER JOIN” and “OUTER JOIN”?Add a column with a default value to an existing table in SQL ServerTable Naming Dilemma: Singular vs. Plural NamesLEFT JOIN vs. LEFT OUTER JOIN in SQL ServerDifference between JOIN and INNER JOINInsert results of a stored procedure into a temporary tableImprove INSERT-per-second performance of SQLite?Finding duplicate values in a SQL tableWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?To get the join columns from a SQL (query) using PYparsing, python script













0















I've created a calendar table (with indexes) that has dates, day of week, fiscal year, day name, etc. The range on my calendar table is between 2005 - 2071. I don't have any issues when I query the calendar table.



I want to use the date column on the calendar table to filter 2 different date columns on 2 different tables.



The issue I'm running into, is when searching a large date range using the date column on the calendar table(i.e. 2010-2019), it's taking a VERY long time to return the results. Whereas, if I query the same date range on each table, the results are returned in seconds. Here's the query i'm working with:



Select 
a.Col1,
a.Col2,
a.Col3,
a.Date1,
b.Date1
From
TableA as A
FULL JOIN TableB as B
on A.Col1 = B.Col1
INNER JOIN TableCal as C
on A.Date1 = C.Date1
or B.Date1 = C.Date1
WHERE
C.date1 between '2010' and '2019'


If there's a better way to write this or if I'm going about it the wrong way, I'm open to all advice / suggestions! Thanks!










share|improve this question



















  • 1





    Start here on how to get help with a slow query. Also, although you have already made your own, you may find Aaron Bertrand's calendar table more useful / more dynamic. Also from Aaron, be sure you really need NOLOCK and understand why it can be a bad idea. It is not a "go faster" button.

    – scsimon
    Mar 21 at 13:42












  • First of all, remove those NOLOCK hints. They don't make anything run faster. Second, does your Calendar table have indexes on its columns? If not, it's going to be slow. Finally, post the actual query. What you posted here doesn't join dates and filters the Date column by a year string. A calendar table is supposed to have separate fields for each date element and the date field should be a date. If WHERE c.date between '2010' and '2019' doesn't raise an error it's probably because the date values were implicitly converted to strings, forcing a full table scan

    – Panagiotis Kanavos
    Mar 21 at 13:44












  • Sample data and desired results would really help. FULL OUTER JOIN is rarely needed.

    – Gordon Linoff
    Mar 21 at 14:00











  • C.date1 is really a varchar of the year?

    – scsimon
    Mar 21 at 16:34















0















I've created a calendar table (with indexes) that has dates, day of week, fiscal year, day name, etc. The range on my calendar table is between 2005 - 2071. I don't have any issues when I query the calendar table.



I want to use the date column on the calendar table to filter 2 different date columns on 2 different tables.



The issue I'm running into, is when searching a large date range using the date column on the calendar table(i.e. 2010-2019), it's taking a VERY long time to return the results. Whereas, if I query the same date range on each table, the results are returned in seconds. Here's the query i'm working with:



Select 
a.Col1,
a.Col2,
a.Col3,
a.Date1,
b.Date1
From
TableA as A
FULL JOIN TableB as B
on A.Col1 = B.Col1
INNER JOIN TableCal as C
on A.Date1 = C.Date1
or B.Date1 = C.Date1
WHERE
C.date1 between '2010' and '2019'


If there's a better way to write this or if I'm going about it the wrong way, I'm open to all advice / suggestions! Thanks!










share|improve this question



















  • 1





    Start here on how to get help with a slow query. Also, although you have already made your own, you may find Aaron Bertrand's calendar table more useful / more dynamic. Also from Aaron, be sure you really need NOLOCK and understand why it can be a bad idea. It is not a "go faster" button.

    – scsimon
    Mar 21 at 13:42












  • First of all, remove those NOLOCK hints. They don't make anything run faster. Second, does your Calendar table have indexes on its columns? If not, it's going to be slow. Finally, post the actual query. What you posted here doesn't join dates and filters the Date column by a year string. A calendar table is supposed to have separate fields for each date element and the date field should be a date. If WHERE c.date between '2010' and '2019' doesn't raise an error it's probably because the date values were implicitly converted to strings, forcing a full table scan

    – Panagiotis Kanavos
    Mar 21 at 13:44












  • Sample data and desired results would really help. FULL OUTER JOIN is rarely needed.

    – Gordon Linoff
    Mar 21 at 14:00











  • C.date1 is really a varchar of the year?

    – scsimon
    Mar 21 at 16:34













0












0








0








I've created a calendar table (with indexes) that has dates, day of week, fiscal year, day name, etc. The range on my calendar table is between 2005 - 2071. I don't have any issues when I query the calendar table.



I want to use the date column on the calendar table to filter 2 different date columns on 2 different tables.



The issue I'm running into, is when searching a large date range using the date column on the calendar table(i.e. 2010-2019), it's taking a VERY long time to return the results. Whereas, if I query the same date range on each table, the results are returned in seconds. Here's the query i'm working with:



Select 
a.Col1,
a.Col2,
a.Col3,
a.Date1,
b.Date1
From
TableA as A
FULL JOIN TableB as B
on A.Col1 = B.Col1
INNER JOIN TableCal as C
on A.Date1 = C.Date1
or B.Date1 = C.Date1
WHERE
C.date1 between '2010' and '2019'


If there's a better way to write this or if I'm going about it the wrong way, I'm open to all advice / suggestions! Thanks!










share|improve this question
















I've created a calendar table (with indexes) that has dates, day of week, fiscal year, day name, etc. The range on my calendar table is between 2005 - 2071. I don't have any issues when I query the calendar table.



I want to use the date column on the calendar table to filter 2 different date columns on 2 different tables.



The issue I'm running into, is when searching a large date range using the date column on the calendar table(i.e. 2010-2019), it's taking a VERY long time to return the results. Whereas, if I query the same date range on each table, the results are returned in seconds. Here's the query i'm working with:



Select 
a.Col1,
a.Col2,
a.Col3,
a.Date1,
b.Date1
From
TableA as A
FULL JOIN TableB as B
on A.Col1 = B.Col1
INNER JOIN TableCal as C
on A.Date1 = C.Date1
or B.Date1 = C.Date1
WHERE
C.date1 between '2010' and '2019'


If there's a better way to write this or if I'm going about it the wrong way, I'm open to all advice / suggestions! Thanks!







sql sql-server database performance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 14:25









DRapp

38k1056115




38k1056115










asked Mar 21 at 13:41









SD617SD617

11




11







  • 1





    Start here on how to get help with a slow query. Also, although you have already made your own, you may find Aaron Bertrand's calendar table more useful / more dynamic. Also from Aaron, be sure you really need NOLOCK and understand why it can be a bad idea. It is not a "go faster" button.

    – scsimon
    Mar 21 at 13:42












  • First of all, remove those NOLOCK hints. They don't make anything run faster. Second, does your Calendar table have indexes on its columns? If not, it's going to be slow. Finally, post the actual query. What you posted here doesn't join dates and filters the Date column by a year string. A calendar table is supposed to have separate fields for each date element and the date field should be a date. If WHERE c.date between '2010' and '2019' doesn't raise an error it's probably because the date values were implicitly converted to strings, forcing a full table scan

    – Panagiotis Kanavos
    Mar 21 at 13:44












  • Sample data and desired results would really help. FULL OUTER JOIN is rarely needed.

    – Gordon Linoff
    Mar 21 at 14:00











  • C.date1 is really a varchar of the year?

    – scsimon
    Mar 21 at 16:34












  • 1





    Start here on how to get help with a slow query. Also, although you have already made your own, you may find Aaron Bertrand's calendar table more useful / more dynamic. Also from Aaron, be sure you really need NOLOCK and understand why it can be a bad idea. It is not a "go faster" button.

    – scsimon
    Mar 21 at 13:42












  • First of all, remove those NOLOCK hints. They don't make anything run faster. Second, does your Calendar table have indexes on its columns? If not, it's going to be slow. Finally, post the actual query. What you posted here doesn't join dates and filters the Date column by a year string. A calendar table is supposed to have separate fields for each date element and the date field should be a date. If WHERE c.date between '2010' and '2019' doesn't raise an error it's probably because the date values were implicitly converted to strings, forcing a full table scan

    – Panagiotis Kanavos
    Mar 21 at 13:44












  • Sample data and desired results would really help. FULL OUTER JOIN is rarely needed.

    – Gordon Linoff
    Mar 21 at 14:00











  • C.date1 is really a varchar of the year?

    – scsimon
    Mar 21 at 16:34







1




1





Start here on how to get help with a slow query. Also, although you have already made your own, you may find Aaron Bertrand's calendar table more useful / more dynamic. Also from Aaron, be sure you really need NOLOCK and understand why it can be a bad idea. It is not a "go faster" button.

– scsimon
Mar 21 at 13:42






Start here on how to get help with a slow query. Also, although you have already made your own, you may find Aaron Bertrand's calendar table more useful / more dynamic. Also from Aaron, be sure you really need NOLOCK and understand why it can be a bad idea. It is not a "go faster" button.

– scsimon
Mar 21 at 13:42














First of all, remove those NOLOCK hints. They don't make anything run faster. Second, does your Calendar table have indexes on its columns? If not, it's going to be slow. Finally, post the actual query. What you posted here doesn't join dates and filters the Date column by a year string. A calendar table is supposed to have separate fields for each date element and the date field should be a date. If WHERE c.date between '2010' and '2019' doesn't raise an error it's probably because the date values were implicitly converted to strings, forcing a full table scan

– Panagiotis Kanavos
Mar 21 at 13:44






First of all, remove those NOLOCK hints. They don't make anything run faster. Second, does your Calendar table have indexes on its columns? If not, it's going to be slow. Finally, post the actual query. What you posted here doesn't join dates and filters the Date column by a year string. A calendar table is supposed to have separate fields for each date element and the date field should be a date. If WHERE c.date between '2010' and '2019' doesn't raise an error it's probably because the date values were implicitly converted to strings, forcing a full table scan

– Panagiotis Kanavos
Mar 21 at 13:44














Sample data and desired results would really help. FULL OUTER JOIN is rarely needed.

– Gordon Linoff
Mar 21 at 14:00





Sample data and desired results would really help. FULL OUTER JOIN is rarely needed.

– Gordon Linoff
Mar 21 at 14:00













C.date1 is really a varchar of the year?

– scsimon
Mar 21 at 16:34





C.date1 is really a varchar of the year?

– scsimon
Mar 21 at 16:34












1 Answer
1






active

oldest

votes


















0














Formally, the answer is to use equijoins, like this:



Select a.Col1, a.Col2, a.Col3
From (a join
cal ca
on a.col2 = ca.col2
) full join
(b join
cal cb
on b.col2 = cb.col2
)
on a.col1 = b.col1
where ca.date between '2010' and '2019' or
cb.date between '2010' and '2019' ;





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281775%2fjoining-calendar-table%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Formally, the answer is to use equijoins, like this:



    Select a.Col1, a.Col2, a.Col3
    From (a join
    cal ca
    on a.col2 = ca.col2
    ) full join
    (b join
    cal cb
    on b.col2 = cb.col2
    )
    on a.col1 = b.col1
    where ca.date between '2010' and '2019' or
    cb.date between '2010' and '2019' ;





    share|improve this answer



























      0














      Formally, the answer is to use equijoins, like this:



      Select a.Col1, a.Col2, a.Col3
      From (a join
      cal ca
      on a.col2 = ca.col2
      ) full join
      (b join
      cal cb
      on b.col2 = cb.col2
      )
      on a.col1 = b.col1
      where ca.date between '2010' and '2019' or
      cb.date between '2010' and '2019' ;





      share|improve this answer

























        0












        0








        0







        Formally, the answer is to use equijoins, like this:



        Select a.Col1, a.Col2, a.Col3
        From (a join
        cal ca
        on a.col2 = ca.col2
        ) full join
        (b join
        cal cb
        on b.col2 = cb.col2
        )
        on a.col1 = b.col1
        where ca.date between '2010' and '2019' or
        cb.date between '2010' and '2019' ;





        share|improve this answer













        Formally, the answer is to use equijoins, like this:



        Select a.Col1, a.Col2, a.Col3
        From (a join
        cal ca
        on a.col2 = ca.col2
        ) full join
        (b join
        cal cb
        on b.col2 = cb.col2
        )
        on a.col1 = b.col1
        where ca.date between '2010' and '2019' or
        cb.date between '2010' and '2019' ;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 21 at 14:02









        Gordon LinoffGordon Linoff

        790k35314418




        790k35314418





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281775%2fjoining-calendar-table%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해