473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Join 2 aggregate queries?

Hi all
I have 2 aggregate queries, which are:

select f.id as fixtureid, t.name as home_team, count(t.name)
from teams t, results r, fixtures f, playerstoteams p
where f.type=1 and f.league=4 and r.fixture=f.id and t.division=4
and p.teamid=t.id and r.home=p.playerid and r.winner=p.playerid
group by fixtureid, t.name;
fixtureid | home_team | count
-----------+-----------+-------
2872 | Kat Fish | 12
2944 | The Fowls | 11

and

select f.id as fixtureid, t.name as away_team, count(t.name)
from teams t, results r, fixtures f, playerstoteams p
where f.type=1 and f.league=4 and r.fixture=f.id and t.division=4
and p.teamid=t.id and r.away=p.playerid and r.winner=p.playerid
group by fixtureid, t.name;

fixtureid | away_team | count
-----------+-------------+-------
2872 | A Cut Above | 13
2944 | Kat Fish | 14

I'd like to join them somehow to get the following:

fixtureid | home_team | count1 | away_team | count2
-----------+-----------+--------+-------------+-------
2872 | Kat Fish | 12 | A Cut Above | 13
2944 | The Fowls | 11 | Kat Fish | 14

Can anyone spot a reasonable way to do that please?

I have tried this, with predictably poor results:

select f.id as fixtureid, t.name as home_team, count(t.name)
from teams t, results r, fixtures f, playerstoteams p
left outer join (select f2.id as fixtureid, t2.name as away_team, count(t2.name)
from teams t2, results r2, fixtures f2, playerstoteams p2
where f2.type=1 and f2.league=4 and r2.fixture=f2.id and t2.division=4
and p2.teamid=t2.id and r2.away=p2.playerid and r2.winner=p2.playerid
group by fixtureid, t2.name) as foo on (id=id)
where f.type=1 and f.league=4 and r.fixture=f.id and t.division=4
and p.teamid=t.id and r.home=p.playerid and r.winner=p.playerid
group by f.id, t.name;

I'd also like to not have to create a view or any temporary tables that would
need deleting afterwards.

Any help much appreciated, thanks!

Cheers

Zak
--
================================================== ======================
http://www.carfolio.com/ Searchable database of 10 000+ car specs
================================================== ======================

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 22 '05 #1
3 2101
On Fri, Feb 13, 2004 at 02:13:11 +0200,
Zak McGregor <za*@mighty.co.za> wrote:

I'd like to join them somehow to get the following:

fixtureid | home_team | count1 | away_team | count2
-----------+-----------+--------+-------------+-------
2872 | Kat Fish | 12 | A Cut Above | 13
2944 | The Fowls | 11 | Kat Fish | 14

Can anyone spot a reasonable way to do that please?


The simplest way given what you've got already is to make each of the
two selects a from item and join them on fixtureid.
That probably isn't the most efficient way to do it, but should get you
going for now.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 22 '05 #2
On Fri, 13 Feb 2004, Zak McGregor wrote:
Hi all
I have 2 aggregate queries, which are:

select f.id as fixtureid, t.name as home_team, count(t.name)
from teams t, results r, fixtures f, playerstoteams p
where f.type=1 and f.league=4 and r.fixture=f.id and t.division=4
and p.teamid=t.id and r.home=p.playerid and r.winner=p.playerid
group by fixtureid, t.name;
fixtureid | home_team | count
-----------+-----------+-------
2872 | Kat Fish | 12
2944 | The Fowls | 11

and

select f.id as fixtureid, t.name as away_team, count(t.name)
from teams t, results r, fixtures f, playerstoteams p
where f.type=1 and f.league=4 and r.fixture=f.id and t.division=4
and p.teamid=t.id and r.away=p.playerid and r.winner=p.playerid
group by fixtureid, t.name;

fixtureid | away_team | count
-----------+-------------+-------
2872 | A Cut Above | 13
2944 | Kat Fish | 14

I'd like to join them somehow to get the following:

fixtureid | home_team | count1 | away_team | count2
-----------+-----------+--------+-------------+-------
2872 | Kat Fish | 12 | A Cut Above | 13
2944 | The Fowls | 11 | Kat Fish | 14


Sure, make each a subselect and join those:
select * from

(select f.id as fixtureid, t.name as home_team, count(t.name)
from teams t, results r, fixtures f, playerstoteams p
where f.type=1 and f.league=4 and r.fixture=f.id and t.division=4
and p.teamid=t.id and r.home=p.playerid and r.winner=p.playerid
group by fixtureid, t.name) as a

join

(select f.id as fixtureid, t.name as away_team, count(t.name)
from teams t, results r, fixtures f, playerstoteams p
where f.type=1 and f.league=4 and r.fixture=f.id and t.division=4
and p.teamid=t.id and r.away=p.playerid and r.winner=p.playerid
group by fixtureid, t.name) as b

on (a.id=b.id);


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #3
On Fri, 13 Feb 2004 09:00:29 -0700 (MST)
"scott.marlowe" <sc***********@ihs.com> wrote:
Sure, make each a subselect and join those:

[snip]

Thanks Scoot, and Bruno too for his earlier suggestion of the same approach. I
have it working exactly as I needed it to, thanks!

Ciao

Zak
--
================================================== ======================
http://www.carfolio.com/ Searchable database of 10 000+ car specs
================================================== ======================

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nils Benson | last post by:
Here's a strange problem: I have an arbitrary query that has calculated columns in its output. It could be a "SELECT TOP 1 * FROM ...", or a "SELECT STDEV(somefield) FROM ...". It doesn't matter...
2
by: Frank.Sebesta | last post by:
I would like to run two queries together with the results together in one report. I would also want to extract only the duplicate records or have only the duplicate record show up in the results....
1
by: epigram | last post by:
I'd like to know if there is a best approach, in terms of using the Data Web Controls (i.e. DataGrid, DataList & Repeater), for displaying data that is the result of a query that joins two tables...
7
by: Shanimal | last post by:
I would like to know how to join 2 queries so that the results of these 2 queries show up in the same query: SELECT b.bios_serial_number FROM bios b: SELECT s.system_name FROM system s; ...
11
by: dtinsley | last post by:
Ok, I have having a problem joing these two queries. Can anyone please help me out here. These are the results of my two queries: These are the values in the A1 field of QueryA COCKBURN 2 GT...
1
by: nsymiakakis | last post by:
Hi everyone, I have two seperate queries that work great individually, but I need to join them together to get a combined output. Can someone look at these and help me with the code? Here are the 2...
3
topher23
by: topher23 | last post by:
I was working in the development copy of one of my databases, and as I was saving it, Access encountered one of it's much-beloved errors. No choice - I restarted and "recovered" the database....
2
by: brian123 | last post by:
I want to group and then join two tables in a stored procedure and return results to a datareader. The non-grouped data in each table has a many to many relationship. select entity, count(*) as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.