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

            How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

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

            Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?