How to write tsql for report

  • Hi guys,

    Hope all are doing good.

    I just wanted a idea on report buiding query. Like , i actually tired to bring count for a particular logic , that came well, but when i tried to bring count for another complete different logic in same query it is messed up.

    So, can any one tell me idea to merge 2 different logic query in a single result? i mean as seperate column in a single row

    for example

    result :

    col1 col2 col3

    logic1 logic2 logic3

    I tried Union but some times it works but most of time it gives some problem.

    Just give me some sample idea to mingle two different logic in a single row

    Thanks

  • Can you be more specific about the term "completely different logic"?

    Are the tables at least the same for those different logics?

    _____________
    Code for TallyGenerator

  • Sergiy (7/28/2016)


    Can you be more specific about the term "completely different logic"?

    Are the tables at least the same for those different logics?

    Thanks for your reply Sergiy, i meant 'completly different logic' because count and grouping them for another set which is in different tables ( no ways to join )

    we can use #temp but still i wanted to do it in a query without #temp table. Is there any option that we can combine both in a single row result ?

  • JoNTSQLSrv (7/28/2016)


    Sergiy (7/28/2016)


    Can you be more specific about the term "completely different logic"?

    Are the tables at least the same for those different logics?

    Thanks for your reply Sergiy, i meant 'completly different logic' because count and grouping them for another set which is in different tables ( no ways to join )

    we can use #temp but still i wanted to do it in a query without #temp table. Is there any option that we can combine both in a single row result ?

    If you are attempting to merge two quite different queries, then why not reduce the number of guesses and post the queries? This article [/url]will help you to understand that forum communication works differently to face-to-face.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Probably the easiest way is to use derived tables within a main query, like below, assuming that each derive table returns only a single row:

    SELECT q1.*, q2.*, q3.* --, ...

    FROM (

    SELECT ..., ...

    FROM ...

    ) AS q1

    CROSS JOIN (

    SELECT ..., ..., ..., ...

    FROM ...

    ) AS q2

    CROSS JOIN (

    SELECT ...

    FROM ...

    ) AS q3

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • ScottPletcher (7/28/2016)


    Probably the easiest way is to use derived tables within a main query, like below, assuming that each derive table returns only a single row:

    SELECT q1.*, q2.*, q3.* --, ...

    FROM (

    SELECT ..., ...

    FROM ...

    ) AS q1

    CROSS JOIN (

    SELECT ..., ..., ..., ...

    FROM ...

    ) AS q2

    CROSS JOIN (

    SELECT ...

    FROM ...

    ) AS q3

    Thanks a lot ScottPletcher .. it worked

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply