"Merging" results from two queries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elvis123
    New Member
    • Jan 2008
    • 4

    #1

    "Merging" results from two queries

    I want to "merge" the results of two queries:

    select DATE,CUSTOMER_I D from DB1
    select DATE,CUSTOMER_I D from DB2

    Then I want to be able to count the number of customers for each date in the "merged" result.

    What is the best way to solve this?
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    Originally posted by Elvis123
    I want to "merge" the results of two queries:

    select DATE,CUSTOMER_I D from DB1
    select DATE,CUSTOMER_I D from DB2

    Then I want to be able to count the number of customers for each date in the "merged" result.

    What is the best way to solve this?


    select DATE,CUSTOMER_I D from DB1
    union all
    select DATE,CUSTOMER_I D from DB2

    Comment

    • Elvis123
      New Member
      • Jan 2008
      • 4

      #3
      Thanks, I really appreciate it!

      Comment

      • Elvis123
        New Member
        • Jan 2008
        • 4

        #4
        But how do I do the counting once I have merged the query results? I tried to do it like this:


        select DATE,count(dist inct CUSTOMER_ID) from
        (select DATE,CUSTOMER_I D from DB1
        union all
        select DATE,CUSTOMER_I D from DB2)


        but it does not work...

        Comment

        • deepuv04
          Recognized Expert New Member
          • Nov 2007
          • 227

          #5
          Originally posted by Elvis123
          But how do I do the counting once I have merged the query results? I tried to do it like this:


          select DATE,count(dist inct CUSTOMER_ID) from
          (select DATE,CUSTOMER_I D from DB1
          union all
          select DATE,CUSTOMER_I D from DB2)


          but it does not work...
          you need to group the records based on date

          try the following

          select T.DATE,count(di stinct T.CUSTOMER_ID) from
          (select DATE,CUSTOMER_I D from DB1
          union all
          select DATE,CUSTOMER_I D from DB2) as T
          Group by t.date

          if the above not wroks try this


          WITH CTE
          (
          select DATE,CUSTOMER_I D from DB1
          union all
          select DATE,CUSTOMER_I D from DB2
          )SELECT DATE , Count(CUSTOMER_ ID) FROM CTE
          GROUP BY DATE

          Comment

          • ck9663
            Recognized Expert Specialist
            • Jun 2007
            • 2878

            #6
            Originally posted by Elvis123
            But how do I do the counting once I have merged the query results? I tried to do it like this:


            select DATE,count(dist inct CUSTOMER_ID) from
            (select DATE,CUSTOMER_I D from DB1
            union all
            select DATE,CUSTOMER_I D from DB2)


            but it does not work...
            in your sample, you are retrieving record from a subquery. your subquery becomes some sort of a view. so you need to put an alias on the subquery

            try:

            Code:
            select DATE,count(distinct CUSTOMER_ID) from
            (select DATE,CUSTOMER_ID from DB1
            union all
            select DATE,CUSTOMER_ID from DB2) myTableAlias
            group by DATE
            -- CK

            Comment

            • Elvis123
              New Member
              • Jan 2008
              • 4

              #7
              Thanks! Now it works!

              Comment

              Working...