Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5981219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:54:48+00:00 2026-05-22T21:54:48+00:00

two query returning different no of rows. Query1: ———- SELECT t1.a1, t1.a2, t2.b1,t2.b2 from

  • 0

two query returning different no of rows.

Query1:
----------
SELECT t1.a1, t1.a2, t2.b1,t2.b2 
from (SELECT a1,a2 from xyz WHERE (date BETWEEN '2011-01-01' AND '2011-01-30')
AND id = 70 GROUP BY a1 a2)t1,
(SELECT a1,a2 from xyz WHERE (date BETWEEN '2011-01-01' AND '2011-01-30')
AND id = 70 GROUP BY a1 a2)t2, t3

where t1.a1=t3.a1
and t2.a1=t3.a1


output 1:
---------------
a1 a2  b1  b2
---------------
1   2  7   4
1   3  4   2
1   6  5   1

is there anything wrong in the above query?

I have same structured query and it bring me some row.

when i do same query by using union all it bring me more rows than previous query

query2:
---------------
SELECT t1.a1, t1.a2, '' as b1,'' as b2 
FROM(SELECT a1,a2 from xyz WHERE (date BETWEEN '2011-01-01' AND '2011-01-31')
AND id = 70 GROUP BY a1 a2)t1, t3
where t1.a1=t3.c1

UNION ALL

SELECT '' AS a1, '' AS a2, t2.b1,t2.b2 
FROM(SELECT a1,a2 from xyz WHERE (date BETWEEN '2011-02-01' AND '2011-02-30')
AND id = 70 GROUP BY a1 a2)t1, t3
where t2.b1=t3.c1

output 2:
---------------
a1 a2  b1  b2
---------------
1   2      4
1   3      2
1   6  5
4   8  3

If i have to think of any other techniques then pls advise.

note: I need tow different date range data in a report like sales quantity for the month1 and month2 for a sales man for comparing to months sales. “Ouptup1” will resolve my problem. Please suggest.
note: I am not very much expart in querying data.

Thanks.
Shahidul- Dhaka, Bangladesh.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:54:48+00:00Added an answer on May 22, 2026 at 9:54 pm

    The first query can ( and should ) be wrrite like so (correcting what I’m assuming should be two different date ranges):

    Select t1.a1
        , t1.a2
        , t2.b1
        ,t2.b2 
    From    (
            Select a1,a2 
            From xyz 
            Where date Between '2011-01-01' And '2011-01-30'
                And id = 70 
            Group By a1,a2
            ) As t1
        Join t3
            On t3.a1 = t1.a1
        Join    (   
                Select a1,a2 
                From xyz 
                Where date Between '2011-02-01' And '2011-02-28'
                    And id = 70 
                Group By a1, a2
                ) As t2
            On t2.a1 = t3.a1
    

    You should avoid writing queries where you separate tables in the From clause using commas. This syntax is deprecated and IMO makes for harder to follow queries. The better syntax is the ANSI/ISO Join syntax I have demonstrated.

    For a row to appear in your first query, there must exist an a1 value in table t3 and there must exist a row for that a1 value with a date range in Jan and there must exist a row for Feb. I would assume you want data that appears in either Jan or Feb.

    Given what I think are your requirements, the following might provide you with what you want:

    Select T1.a1, T1.a2
        , T2.a1 As b1, T2.a2 As b2
    From    (
            Select a1, a2
            From xyz
                Join t3
                    On t3.a1 = xyz.a1
            Where xyz.id = 70
                And xyz.date >= '2011-01-01' And xyz.date < '2011-02-01' 
            Group By a1, a2
            ) As T1
        Full Join   (
                    Select a1, a2
                    From xyz
                        Join t3
                            On t3.a1 = xyz.a1
                    Where xyz.id = 70
                        And xyz.date >= '2011-02-01' And xyz.date < '2011-03-01' 
                    Group By a1, a2
                    ) As T2
            On T2.a1 = T1.a1
    

    While the above query is close to what you asked, it would be simpler to stack each months data rather than attempt to show it side by side. To do that, you would use the Union All similar to the way you attempted:

    Select 'Jan' As Month, a1, a2
    From xyz
        Join t3
            On t3.a1 = xyz.a1
    Where xyz.id = 70
        And xyz.date >= '2011-01-01' And xyz.date < '2011-02-01' 
    Group By a1, a2
    Union All
    Select 'Feb', a1, a2
    From xyz
        Join t3
            On t3.a1 = xyz.a1
    Where xyz.id = 70
        And xyz.date >= '2011-02-01' And xyz.date < '2011-03-01' 
    Group By a1, a2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to execute a SQL Query between two databases. Example: SELECT * from
Why is this query returning 0 rows? select t.f1, t.f2 from (select null f1,
I am returning two lists from the database using LINQ to SQL compiled query.
In a SELECT call we are returning two different timestamps, but because each column
What i have is a union query that is returning two rows because of
I'm running a query that is timing out for first two times and returning
I'm using a linq group by query (with two grouping parameters) and would like
I'm trying to get the following query to work in Hibernate: SELECT m FROM
Here is two SQL Query when executing this SQL its work fine. SELECT *
i have a MySQL SELECT query which fetches data from 6 tables using Mysql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.