pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedGROUPing by expressions, and subSELECTs
3+ messages / 2 participants
[nested] [flat]
* GROUPing by expressions, and subSELECTs
@ 2002-02-28 18:29 Shane Wright <me@shanewright.co.uk>
0 siblings, 1 reply; 3+ messages in thread
From: Shane Wright @ 2002-02-28 18:29 UTC (permalink / raw)
To: pgsql-sql
Hi
I have somewhat of a problem with what has got to be my biggest query ever.
So y'all dont get put off by the query itself, here's the problem:
It needs to GROUP BY the result of an expression, thats fine, I put the
expression in the fields bit, and GROUP BY the expression at the end:
SELECT
[fields],
[expression]
FROM
table
GROUP BY
[expression]
Now, the problem is that I want to use the result of that expression in a
subquery. So, I tried nam the result of the expression (...[expression] AS
name...), but no luck. So, I recreated the expression in the subquery and
specified the fields used in the expression hoping that might work:
SELECT
[fields],
[expression],
(SELECT * FROM table2 WHERE [expression]) AS mynewfield
FROM
table
GROUP BY
[expression]
But, it gives this error:
ERROR: Sub-SELECT uses un-GROUPed attribute [table].[field] from outer
query
I'm stumped, and the query is screwing with my mind already. Precalculating
the result of the expression into a different isn't an option because of the
structure and context of its use. Anyone got any ideas?
For the brave, here's the query. I know its messy, but it's dynamically
generated - optimisation comes later...
SELECT iid1 AS iid, ((field1 - 1009670400) / 86400) AS period,
(to_char(ROUND((COUNT(iid1))::float / ( SELECT COUNT(rid) FROM table2 AS
myresponses WHERE (sid=922120971) AND (field1>=(table2.field1
- 1009670400) / 86400) AND (field1<=((table2.field1 -
1009670400) / 86400)+86400) )::float * 100.0, 2), '999.99') || '%') AS count
FROM table1, table2 WHERE (nid=870428218) AND (table2.rid=table1.rid) AND
(field1 >= 1009670400) AND (field1 <= 1014940800) GROUP BY
((field1 - 1009670400) / 86400), iid1
...the names have been changed to protect the innocent...
Any help appreciated,
thanks
Shane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: GROUPing by expressions, and subSELECTs
@ 2002-02-28 21:40 Tom Lane <tgl@sss.pgh.pa.us>
parent: Shane Wright <me@shanewright.co.uk>
0 siblings, 1 reply; 3+ messages in thread
From: Tom Lane @ 2002-02-28 21:40 UTC (permalink / raw)
To: me@shanewright.co.uk; +Cc: pgsql-sql
Shane Wright <me@shanewright.co.uk> writes:
> SELECT
> [fields],
> [expression],
> (SELECT * FROM table2 WHERE [expression]) AS mynewfield
> FROM
> table
> GROUP BY
> [expression]
> But, it gives this error:
> ERROR: Sub-SELECT uses un-GROUPed attribute [table].[field] from outer
> query
Yeah, the code is not bright enough to figure out that this could be
considered legitimate. But try this:
SELECT
[fields],
myexpr,
(SELECT * FROM table2 WHERE ss.myexpr) AS mynewfield
FROM
(SELECT [fields],
[expression] as myexpr
FROM table
GROUP BY myexpr) AS ss
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: GROUPing by expressions, and subSELECTs
@ 2002-03-01 12:18 Shane Wright <me@shanewright.co.uk>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 3+ messages in thread
From: Shane Wright @ 2002-03-01 12:18 UTC (permalink / raw)
To: pgsql-sql; +Cc: Tom Lane <tgl@sss.pgh.pa.us>
Tom, you're a star! It took me a minute to work out what you meant, but hte
whole thing is working a treat now!
Thanks again,
Shane
On Thursday 28 Feb 2002 9:40 pm, Tom Lane wrote:
> Shane Wright <me@shanewright.co.uk> writes:
> > SELECT
> > [fields],
> > [expression],
> > (SELECT * FROM table2 WHERE [expression]) AS mynewfield
> > FROM
> > table
> > GROUP BY
> > [expression]
> >
> > But, it gives this error:
> >
> > ERROR: Sub-SELECT uses un-GROUPed attribute [table].[field] from outer
> > query
>
> Yeah, the code is not bright enough to figure out that this could be
> considered legitimate. But try this:
>
> SELECT
> [fields],
> myexpr,
> (SELECT * FROM table2 WHERE ss.myexpr) AS mynewfield
> FROM
> (SELECT [fields],
> [expression] as myexpr
> FROM table
> GROUP BY myexpr) AS ss
>
> regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2002-03-01 12:18 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-02-28 18:29 GROUPing by expressions, and subSELECTs Shane Wright <me@shanewright.co.uk>
2002-02-28 21:40 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-03-01 12:18 ` Shane Wright <me@shanewright.co.uk>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox