Is this possible?

  • I'm a beginner in sql and wonder if this is possible:

    Joining these 3 queries together to form a table of results:

    declare @sd datetime, @ed datetime, @ac varchar(100), @Theme varchar(100), @Theme2 varchar(100), @IssueType varchar(100)

    select distinct Theme from tbl where (nullif(Theme,'') is not NULL) order by Theme asc

    select distinct Theme2 from tbl where (nullif(Theme2,'') is not NULL) order by Theme2 asc

    select count(1) as NoOfResults from tbl where (ConsentFormReceivedThroughDoor between @sd and @ed)

    and(AreaCode = @ac) and(Theme = @Theme) and(IssueType = @IssueType)

  • Yes, it's possible, but it depends on what you want it to look like. You first query will return a result set like:

    ThemeA

    ThemeB

    ThemeC

    The second will be like:

    Theme2A

    Theme2B

    Theme2C

    Theme2D

    The third will be like:

    7

    How do you wish to combine those three result sets? Some sample data and table DDL would be very helpful here.

    John

  • Also want to note here that the ORDER BY clauses are pretty useless if you want to join results together.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • If you have a relation/common columns in a different tables, you can use joins in your query to get the result.

  • Theme | Theme2 | NoOfResults

    Counting (NoOfResults) how many Theme2s are paired with Theme

  • Retracted due to lack of coffee.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • duncan.turner.2dg (10/25/2013)


    Theme | Theme2 | NoOfResults

    Counting (NoOfResults) how many Theme2s are paired with Theme

    How to relate Theme with Theme2? What is the relationship?

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

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