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.
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
精彩评论