pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSQL Query
45+ messages / 28 participants
[nested] [flat]
* SQL Query
@ 2002-11-29 19:23 Scott Taylor <scott.taylor@4i-dotcom.com>
0 siblings, 3 replies; 45+ messages in thread
From: Scott Taylor @ 2002-11-29 19:23 UTC (permalink / raw)
To: pgsql-general
I have submitted this query to the list before, but have since upgraded
to a later version so I lost the command history.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2002-11-29 19:32 Tariq Muhammad <tmuhamma@libertyrms.com>
parent: Scott Taylor <scott.taylor@4i-dotcom.com>
2 siblings, 0 replies; 45+ messages in thread
From: Tariq Muhammad @ 2002-11-29 19:32 UTC (permalink / raw)
To: Scott Taylor <scott.taylor@4i-dotcom.com>; +Cc: pgsql-general
On 29 Nov 2002, Scott Taylor wrote:
> I have submitted this query to the list before, but have since upgraded
> to a later version so I lost the command history.
>
> >From the below output, could someone tell me how to return rows only
> where:
>
> 1. If chart_id=10074, return all rows with same trans_id (i.e. trans_id
> 10088 and 10101)
> 2. Where amount >=0
> 3. With transdate between 2002-07-01 and 2002-09-30
SELECT trans_id, chart_id, amount, transdate
FROM acc_trans
WHERE chart_id = 10074 AND
trans_id in (10088,10101) AND
amount >=0 AND
transdate BETWEEN '2002-07-01' AND '2002-09-30';
Tariq Muhammad
Liberty RMS
tariq@libertyrms.info
v:416-646-3304 x 111
c:416-993-1859
p:416-381-1457
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2002-11-29 20:07 Scott Lamb <slamb@slamb.org>
parent: Scott Taylor <scott.taylor@4i-dotcom.com>
2 siblings, 0 replies; 45+ messages in thread
From: Scott Lamb @ 2002-11-29 20:07 UTC (permalink / raw)
To: pgsql-general
Scott Taylor wrote:
> I have submitted this query to the list before, but have since upgraded
> to a later version so I lost the command history.
Ergh. Command history? You really should stick your queries somewhere
more permanent than that, even if it is one you only use in the query
monitor.
> From the below output, could someone tell me how to return rows only
> where:
where all of these are true? where any of these are true?
> 1. If chart_id=10074, return all rows with same trans_id (i.e. trans_id
> 10088 and 10101)
Return all rows where there exists a row with the same trans_id and
chart_id = 10074. (That's easy to express with an "exists" where clause.)
Does the chart_id = 10074 row have to satisfy the two conditions below
for rows with the same trans_id to be returned? Or just the rows being
actually returned?
> 2. Where amount >=0
> 3. With transdate between 2002-07-01 and 2002-09-30
My best guess is that you are looking for this:
select trans_id,
chart_id,
amount,
transdate
from acc_trans
where exists (
select 'x'
from acc_trans sub
where acc_trans.trans_id = sub.trans_id
and sub.chart_id = 10074)
and amount >= 0
and transdate between '2002-07-01' and '2002-09-30'
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2002-11-30 07:45 Joel Burton <joel@joelburton.com>
parent: Scott Taylor <scott.taylor@4i-dotcom.com>
2 siblings, 1 reply; 45+ messages in thread
From: Joel Burton @ 2002-11-30 07:45 UTC (permalink / raw)
To: Scott Taylor <scott.taylor@4i-dotcom.com>; +Cc: pgsql-general
On Fri, Nov 29, 2002 at 07:23:56PM +0000, Scott Taylor wrote:
> I have submitted this query to the list before, but have since upgraded
> to a later version so I lost the command history.
>
> >From the below output, could someone tell me how to return rows only
> where:
>
> 1. If chart_id=10074, return all rows with same trans_id (i.e. trans_id
> 10088 and 10101)
> 2. Where amount >=0
> 3. With transdate between 2002-07-01 and 2002-09-30
>
> accs=# select trans_id, chart_id, amount, transdate from acc_trans;
> trans_id | chart_id | amount | transdate
> ----------+----------+----------+------------
> 10088 | 10004 | -2062.12 | 2002-01-03
> 10088 | 10037 | 1755 | 2002-01-03
> 10088 | 10074 | 307.12 | 2002-01-03
> 10088 | 10004 | 2062.12 | 2002-07-03
> 10088 | 10002 | -2062.12 | 2002-07-03
> 10096 | 10016 | 1169.75 | 2002-12-03
> 10096 | 10047 | -1169.75 | 2002-12-03
> 10096 | 10002 | 1169.75 | 2002-11-03
> 10096 | 10016 | -1169.75 | 2002-11-03
> 10098 | 10016 | 283.91 | 2002-12-03
> 10098 | 10044 | -283.91 | 2002-12-03
> 10099 | 10016 | 137.6 | 2002-12-03
> 10099 | 10045 | -137.6 | 2002-12-03
> 10100 | 10016 | 163.74 | 2002-12-03
> 10100 | 10046 | -163.74 | 2002-12-03
> 10101 | 10004 | -528.75 | 2002-03-20
> 10101 | 10037 | 450 | 2002-03-20
> 10101 | 10074 | 78.75 | 2002-03-20
It'd be helpful if you gave us the solution you expect for this sample
data, BTW.
Interpreting your question, I get:
0) Find all trans_id #s where chart_id=10074
1) Find all rows w/those trans_id where
a) the amount >=0
b) the date is between 7/1 and 9/30
so only the fourth record would be returned.
So something like:
SELECT *
FROM trans
WHERE
trans_id IN (SELECT trans_id
FROM trans
WHERE chart_id = 10074 )
AND amount >= 0
AND transdate BETWEEN '2002-07-01' AND '2002-09-30'
would be the easiest-to-understand solution, but it won't perform
terribly well (because of the IN statement). You can rewrite this
w/EXISTS or with a multi-table join, and it should perform better:
SELECT t2.*
FROM trans AS t1,
trans AS t2
WHERE
t1.chart_id = 10074
AND t1.trans_id = t2.trans_id
AND t2.amount >= 0
AND t2.transdate BETWEEN '2002-07-01' AND '2002-09-30'
but you should test w/your data and indexes to check performance.
--
Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Independent Knowledge Management Consultant
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2002-11-30 08:05 Scott Lamb <slamb@slamb.org>
parent: Joel Burton <joel@joelburton.com>
0 siblings, 1 reply; 45+ messages in thread
From: Scott Lamb @ 2002-11-30 08:05 UTC (permalink / raw)
To: pgsql-general
On Sat, Nov 30, 2002 at 02:45:44AM -0500, Joel Burton wrote:
> You can rewrite this w/EXISTS or with a multi-table join, and it
> should perform better:
>
> SELECT t2.*
> FROM trans AS t1,
> trans AS t2
> WHERE
> t1.chart_id = 10074
> AND t1.trans_id = t2.trans_id
> AND t2.amount >= 0
> AND t2.transdate BETWEEN '2002-07-01' AND '2002-09-30'
Doesn't that need a "distinct" to be equivalent to the exists query? If
there are two 10074 rows with the same trans_id, I think all rows with
that trans_id would be returned twice otherwise.
--
Scott Lamb
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2002-11-30 08:21 Joel Burton <joel@joelburton.com>
parent: Scott Lamb <slamb@slamb.org>
0 siblings, 0 replies; 45+ messages in thread
From: Joel Burton @ 2002-11-30 08:21 UTC (permalink / raw)
To: Scott Lamb <slamb@slamb.org>; +Cc: pgsql-general
On Sat, Nov 30, 2002 at 02:05:20AM -0600, Scott Lamb wrote:
> On Sat, Nov 30, 2002 at 02:45:44AM -0500, Joel Burton wrote:
> > You can rewrite this w/EXISTS or with a multi-table join, and it
> > should perform better:
> >
> > SELECT t2.*
> > FROM trans AS t1,
> > trans AS t2
> > WHERE
> > t1.chart_id = 10074
> > AND t1.trans_id = t2.trans_id
> > AND t2.amount >= 0
> > AND t2.transdate BETWEEN '2002-07-01' AND '2002-09-30'
>
> Doesn't that need a "distinct" to be equivalent to the exists query? If
> there are two 10074 rows with the same trans_id, I think all rows with
> that trans_id would be returned twice otherwise.
Good catch, Scott. Yes, if you have another row with trans_id=10088 and
chart_id=10074, this row and the original-correct row will both show up
twice.
Adding DISTINCT will prevent that, but it's not perfect -- this would
suppress the case where two matching rows were in the table, while this
would appear in the IN or EXISTS cases. Which may or may not be a
problem, depending on the application. Of course, the best solution to
this would be to ensure that the table has a primary key, even if its
just a SERIAL column. Then we could DISTINCT w/o fear.
Ok, did I miss anything else? ;)
- J.
--
Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Independent Knowledge Management Consultant
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL Query
@ 2002-12-01 15:53 Scott Taylor <scott.taylor@4i-dotcom.com>
0 siblings, 0 replies; 45+ messages in thread
From: Scott Taylor @ 2002-12-01 15:53 UTC (permalink / raw)
To: pgsql-general
That did not seem to work. In answer to a previous question, the
expected output should be all rows with trans_id's of 10088 (3 rows) and
10101 (2 rows), because these are the only trans_id's where chart_id
=10074 and also meet the other criteria.
Below is the query and output, which as you can see omits rows it should
have returned.
accs=# SELECT t2.*
accs-# FROM acc_trans AS t1,
accs-# acc_trans AS t2
accs-# WHERE
accs-# t1.chart_id = 10074
accs-# AND t1.trans_id = t2.trans_id
accs-# AND t2.amount >=0
accs-# AND t2.transdate BETWEEN '2002-07-01' AND '2002-09-30';
trans_id | chart_id | amount | transdate | source | cleared |
----------+----------+---------+------------+--------+---------+----------------
10088 | 10004 | 2062.12 | 2002-07-03 | | f | f
10103 | 10004 | 528.75 | 2002-08-04 | | f | f
10128 | 10037 | 450 | 2002-07-05 | | f | f
10128 | 10074 | 78.75 | 2002-07-05 | | f | f
10126 | 10004 | 528.75 | 2002-09-05 | | f | f
10164 | 10037 | 525 | 2002-07-17 | | f | f
10164 | 10074 | 91.88 | 2002-07-17 | | f | f
10168 | 10037 | 525 | 2002-07-18 | | f | f
10168 | 10074 | 91.88 | 2002-07-18 | | f | f
10171 | 10029 | 23.5 | 2002-07-22 | J00012 | f | f
10172 | 10029 | 91.65 | 2002-07-22 | J00013 | f | f
10192 | 10074 | 3.5 | 2002-07-22 | J00032 | f | f
10192 | 10074 | 3.5 | 2002-07-22 | J00032 | f | f
10192 | 10057 | 20 | 2002-07-22 | J00032 | f | f
10192 | 10057 | 20 | 2002-07-22 | J00032 | f | f
10192 | 10074 | 13.65 | 2002-07-22 | J00032 | f | f
10192 | 10074 | 13.65 | 2002-07-22 | J00032 | f | f
10192 | 10058 | 78 | 2002-07-22 | J00032 | f | f
10192 | 10058 | 78 | 2002-07-22 | J00032 | f | f
10194 | 10037 | 525 | 2002-07-22 | | f | f
10194 | 10074 | 91.88 | 2002-07-22 | | f | f
10162 | 10004 | 555.19 | 2002-07-25 | | f | f
10168 | 10004 | 616.88 | 2002-07-25 | | f | f
10201 | 10037 | 525 | 2002-07-26 | | f | f
10201 | 10074 | 91.88 | 2002-07-26 | | f | f
10205 | 10037 | 420 | 2002-09-08 | | f | f
10205 | 10074 | 73.5 | 2002-09-08 | | f | f
10208 | 10037 | 2520 | 2002-09-08 | | f | f
10208 | 10074 | 441 | 2002-09-08 | | f | f
10164 | 10004 | 616.88 | 2002-07-31 | | f | f
10194 | 10004 | 616.88 | 2002-07-31 | | f | f
10201 | 10004 | 616.88 | 2002-07-31 | | f | f
10203 | 10004 | 616.88 | 2002-08-15 | | f | f
10205 | 10004 | 493.5 | 2002-08-15 | | f | f
10208 | 10004 | 2961 | 2002-08-22 | | f | f
Regards
Scott
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query...
@ 2003-02-19 13:38 Mario Weilguni <mweilguni@sime.com>
parent: jerome <jerome@gmanmi.tv>
0 siblings, 2 replies; 45+ messages in thread
From: Mario Weilguni @ 2003-02-19 13:38 UTC (permalink / raw)
To: jerome@gmanmi.tv; pgsql-general
>if i have 5 tables and all of it has column X and i wanted to get unique
>values in column X for all tables..
>can i do it in one query?
sure. this is one way to do this:
select distinct foo.X from (
select X from table1
union
select X from table2
....
) as foo;
if the values are unique within every table, then you might want to omit the
surrounding select.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query...
@ 2003-02-19 14:31 greg@turnstep.com
parent: Mario Weilguni <mweilguni@sime.com>
1 sibling, 0 replies; 45+ messages in thread
From: greg@turnstep.com @ 2003-02-19 14:31 UTC (permalink / raw)
To: pgsql-general
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
> if i have 5 tables and all of it has column X and i wanted to get unique
> values in column X for all tables..
> can i do it in one query?
Yes, you want to use UNION:
SELECT foo FROM table1
UNION
SELECT foo FROM table2
UNION
SELECT foo FROM table3
UNION
SELECT foo FROM table4
UNION
SELECT foo FROM table5;
This will by default remove all duplicate entries. Use UNION ALL to
see all entries, even duplicated ones. Note that UNION (and its
companions INTERSECT and EXPECT) all require that the tables have
the same number of columns, and that each column is of a compatible
type.
- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200302190926
-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html
iD8DBQE+U5SSvJuQZxSWSsgRApdKAJ4oV4G1nq9RVoNbNX4qJ8hy/fI4eQCfTqcE
r3xW8sPVsIdwotPxoZHUHGk=
=P8cL
-----END PGP SIGNATURE-----
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query...
@ 2003-02-19 15:28 Bruno Wolff III <bruno@wolff.to>
parent: Mario Weilguni <mweilguni@sime.com>
1 sibling, 0 replies; 45+ messages in thread
From: Bruno Wolff III @ 2003-02-19 15:28 UTC (permalink / raw)
To: Mario Weilguni <mweilguni@sime.com>; +Cc: jerome@gmanmi.tv; pgsql-general
On Wed, Feb 19, 2003 at 14:38:53 +0100,
Mario Weilguni <mweilguni@sime.com> wrote:
> >if i have 5 tables and all of it has column X and i wanted to get unique
> >values in column X for all tables..
>
> >can i do it in one query?
>
> sure. this is one way to do this:
> select distinct foo.X from (
> select X from table1
> union
> select X from table2
> ....
> ) as foo;
>
> if the values are unique within every table, then you might want to omit the
> surrounding select.
Union's will already only return unique values. So you don't need the outer
select distinct. You might get better performance by using union alls
with select distinct, since only one sort will be needed. For the case
where you know they are all unique, you can use union alls without the
select distinct.
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL query...
@ 2003-02-19 16:35 jerome <jerome@gmanmi.tv>
0 siblings, 1 reply; 45+ messages in thread
From: jerome @ 2003-02-19 16:35 UTC (permalink / raw)
To: pgsql-general
if i have 5 tables and all of it has column X and i wanted to get unique
values in column X for all tables..
can i do it in one query?
TIA
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL query
@ 2005-02-11 11:07 David Goodenough <david.goodenough@btconnect.com>
0 siblings, 4 replies; 45+ messages in thread
From: David Goodenough @ 2005-02-11 11:07 UTC (permalink / raw)
To: pgsql-general
I realise this is not strictly a Postgreslql question, but if the best way to
solve it involves using PG extensions, such as the PG procedural languages
I am only going to do this on PG and so I am happy to use them.
I have an address table, with all the normal fields and a customer name
field and an address type. There is a constraint that means that the
combination of customer and type have to be unique. Normally the
only record per customer will be of type 'default', but if for instance
the customer wants a different billing address I would add in a second
type='billing' address record.
I then want to join this table to another table, say an invoice table,
and I want to use the billing address if present, otherwise the default
address. I do not want to create either two addresses or to put both
addresses on the invoice.
I could do this by doing a select * from addresses where customer = ?
and type = 'billing', looking to see if there is a result row and if not
repeating the query with type = 'default', but that seems inelegant to
me.
I thought of using an inner select for the join, and using limit 1 to
get just the one, and forcing the order by to give me the billing
address by preference, but I am then dependant on the sort order
of the particular type values I am selecting from.
Is there a better way? I am sure this kind of problem must have
been solved before.
Thanks in advance for any help you can give
David
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2005-02-11 11:31 Matt K <matt@kynx.org>
parent: David Goodenough <david.goodenough@btconnect.com>
3 siblings, 1 reply; 45+ messages in thread
From: Matt K @ 2005-02-11 11:31 UTC (permalink / raw)
To: David Goodenough <david.goodenough@btconnect.com>; pgsql-general
David Goodenough wrote:
>I could do this by doing a select * from addresses where customer = ?
>and type = 'billing', looking to see if there is a result row and if not
>repeating the query with type = 'default', but that seems inelegant to
>me.
>
>
Use NULL to indicate that the customer type is default. Then you can
query with:
select * from addresses where customer = ?
and coalesce(type, 'billing') = 'billing'
If type is NULL, the comparison will be 'billing' = 'billing' - always true. If there's a bunch of non-null type addresses, you'll get the 'billing' one.
http://www.postgresql.org/docs/8.0/interactive/functions-conditional.html#AEN12003
Matt
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2005-02-11 11:41 Janning Vygen <vygen@gmx.de>
parent: David Goodenough <david.goodenough@btconnect.com>
3 siblings, 1 reply; 45+ messages in thread
From: Janning Vygen @ 2005-02-11 11:41 UTC (permalink / raw)
To: David Goodenough <david.goodenough@btconnect.com>; +Cc: pgsql-general
Am Freitag, 11. Februar 2005 12:07 schrieb David Goodenough:
>
> I have an address table, with all the normal fields and a customer name
> field and an address type. There is a constraint that means that the
> combination of customer and type have to be unique. Normally the
> only record per customer will be of type 'default', but if for instance
> the customer wants a different billing address I would add in a second
> type='billing' address record.
>
> I then want to join this table to another table, say an invoice table,
> and I want to use the billing address if present, otherwise the default
> address. I do not want to create either two addresses or to put both
> addresses on the invoice.
>
> I could do this by doing a select * from addresses where customer = ?
> and type = 'billing', looking to see if there is a result row and if not
> repeating the query with type = 'default', but that seems inelegant to
> me.
>
> I thought of using an inner select for the join, and using limit 1 to
> get just the one, and forcing the order by to give me the billing
> address by preference, but I am then dependant on the sort order
> of the particular type values I am selecting from.
don't think "vertical" (adresses in rows), think "horizontal" (adresses in
columns), like this:
SELECT
c.*,
COALESCE(a1.street, a2.street) AS street,
COALESCE(a1.zip, a2.zip) AS zip,
COALESCE(a1.town, a2.town) AS town
FROM
customer AS c
LEFT JOIN adresses AS a1 USING (customer_id)
LEFT JOIN adresses AS a2 USING (customer_id)
WHERE
a1.type = default
AND a2.type = 'billing'
i just type the and did not tested it. the trick is to join adresses multiple
times and get the right data with COALESCE function which returns the first
value which is NOT NULL.
If you still have difficulties, please send your schema.
kind regards,
janning
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2005-02-11 11:46 Richard Huxton <dev@archonet.com>
parent: David Goodenough <david.goodenough@btconnect.com>
3 siblings, 1 reply; 45+ messages in thread
From: Richard Huxton @ 2005-02-11 11:46 UTC (permalink / raw)
To: David Goodenough <david.goodenough@btconnect.com>; +Cc: pgsql-general
David Goodenough wrote:
> I realise this is not strictly a Postgreslql question, but if the best way to
> solve it involves using PG extensions, such as the PG procedural languages
> I am only going to do this on PG and so I am happy to use them.
>
> I have an address table, with all the normal fields and a customer name
> field and an address type. There is a constraint that means that the
> combination of customer and type have to be unique. Normally the
> only record per customer will be of type 'default', but if for instance
> the customer wants a different billing address I would add in a second
> type='billing' address record.
>
> I then want to join this table to another table, say an invoice table,
> and I want to use the billing address if present, otherwise the default
> address. I do not want to create either two addresses or to put both
> addresses on the invoice.
Not sure whether a schema change is possible for you, but you might want
to have two tables -
addresses (customer_id*, addr_id*, ...)
addr_usage (customer_id*, addr_type*, addr_id)
Add a custom trigger that ensures for every customer_id there is a valid
row in addr_usage for each addr_type (sales, billing, shipping etc).
That way you can have any mix of addresses you like, and it's explicit
which address is for which purpose.
--
Richard Huxton
Archonet Ltd
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2005-02-11 13:39 Bruno Wolff III <bruno@wolff.to>
parent: David Goodenough <david.goodenough@btconnect.com>
3 siblings, 1 reply; 45+ messages in thread
From: Bruno Wolff III @ 2005-02-11 13:39 UTC (permalink / raw)
To: David Goodenough <david.goodenough@btconnect.com>; +Cc: pgsql-general
On Fri, Feb 11, 2005 at 11:07:24 +0000,
David Goodenough <david.goodenough@btconnect.com> wrote:
>
> I thought of using an inner select for the join, and using limit 1 to
> get just the one, and forcing the order by to give me the billing
> address by preference, but I am then dependant on the sort order
> of the particular type values I am selecting from.
You can order by boolean expressions such as type = 'billing'.
You can use that with LIMIT or DISTINCT ON to get just the address you want.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2005-02-11 16:39 David Goodenough <david.goodenough@btconnect.com>
parent: Matt K <matt@kynx.org>
0 siblings, 0 replies; 45+ messages in thread
From: David Goodenough @ 2005-02-11 16:39 UTC (permalink / raw)
To: pgsql-general
On Friday 11 February 2005 11:31, Matt K wrote:
> David Goodenough wrote:
> >I could do this by doing a select * from addresses where customer = ?
> >and type = 'billing', looking to see if there is a result row and if not
> >repeating the query with type = 'default', but that seems inelegant to
> >me.
>
> Use NULL to indicate that the customer type is default. Then you can
> query with:
>
> select * from addresses where customer = ?
> and coalesce(type, 'billing') = 'billing'
>
> If type is NULL, the comparison will be 'billing' = 'billing' - always
> true. If there's a bunch of non-null type addresses, you'll get the
> 'billing' one.
>
> http://www.postgresql.org/docs/8.0/interactive/functions-conditional.html#A
>EN12003
>
> Matt
>
Well coalesce is not something I had come across, learn something every day.
But I can not use this as the type (with the customer) are the primary key and
therefore not null. I could do something like:-
coalesce( nullif( 'default', type), 'billing')
but I think that might be over egging it a bit. I will hope this one reserve
and remember coalesce for the future.
Thanks,
David
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2005-02-11 16:39 David Goodenough <david.goodenough@btconnect.com>
parent: Richard Huxton <dev@archonet.com>
0 siblings, 0 replies; 45+ messages in thread
From: David Goodenough @ 2005-02-11 16:39 UTC (permalink / raw)
To: pgsql-general
On Friday 11 February 2005 11:46, Richard Huxton wrote:
> David Goodenough wrote:
> > I realise this is not strictly a Postgreslql question, but if the best
> > way to solve it involves using PG extensions, such as the PG procedural
> > languages I am only going to do this on PG and so I am happy to use them.
> >
> > I have an address table, with all the normal fields and a customer name
> > field and an address type. There is a constraint that means that the
> > combination of customer and type have to be unique. Normally the
> > only record per customer will be of type 'default', but if for instance
> > the customer wants a different billing address I would add in a second
> > type='billing' address record.
> >
> > I then want to join this table to another table, say an invoice table,
> > and I want to use the billing address if present, otherwise the default
> > address. I do not want to create either two addresses or to put both
> > addresses on the invoice.
>
> Not sure whether a schema change is possible for you, but you might want
> to have two tables -
> addresses (customer_id*, addr_id*, ...)
> addr_usage (customer_id*, addr_type*, addr_id)
> Add a custom trigger that ensures for every customer_id there is a valid
> row in addr_usage for each addr_type (sales, billing, shipping etc).
>
> That way you can have any mix of addresses you like, and it's explicit
> which address is for which purpose.
Interesting idea, I will consider this.
Thanks
David
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2005-02-11 16:41 David Goodenough <david.goodenough@btconnect.com>
parent: Janning Vygen <vygen@gmx.de>
0 siblings, 0 replies; 45+ messages in thread
From: David Goodenough @ 2005-02-11 16:41 UTC (permalink / raw)
To: pgsql-general
On Friday 11 February 2005 11:41, Janning Vygen wrote:
> Am Freitag, 11. Februar 2005 12:07 schrieb David Goodenough:
> > I have an address table, with all the normal fields and a customer name
> > field and an address type. There is a constraint that means that the
> > combination of customer and type have to be unique. Normally the
> > only record per customer will be of type 'default', but if for instance
> > the customer wants a different billing address I would add in a second
> > type='billing' address record.
> >
> > I then want to join this table to another table, say an invoice table,
> > and I want to use the billing address if present, otherwise the default
> > address. I do not want to create either two addresses or to put both
> > addresses on the invoice.
> >
> > I could do this by doing a select * from addresses where customer = ?
> > and type = 'billing', looking to see if there is a result row and if not
> > repeating the query with type = 'default', but that seems inelegant to
> > me.
> >
> > I thought of using an inner select for the join, and using limit 1 to
> > get just the one, and forcing the order by to give me the billing
> > address by preference, but I am then dependant on the sort order
> > of the particular type values I am selecting from.
>
> don't think "vertical" (adresses in rows), think "horizontal" (adresses in
> columns), like this:
>
> SELECT
> c.*,
> COALESCE(a1.street, a2.street) AS street,
> COALESCE(a1.zip, a2.zip) AS zip,
> COALESCE(a1.town, a2.town) AS town
> FROM
> customer AS c
> LEFT JOIN adresses AS a1 USING (customer_id)
> LEFT JOIN adresses AS a2 USING (customer_id)
> WHERE
> a1.type = default
> AND a2.type = 'billing'
>
> i just type the and did not tested it. the trick is to join adresses
> multiple times and get the right data with COALESCE function which returns
> the first value which is NOT NULL.
>
> If you still have difficulties, please send your schema.
>
> kind regards,
> janning
Lateral thinking always did appeal to me. I will look into this further.
Thanks
David
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2005-02-11 16:46 David Goodenough <david.goodenough@btconnect.com>
parent: Bruno Wolff III <bruno@wolff.to>
0 siblings, 0 replies; 45+ messages in thread
From: David Goodenough @ 2005-02-11 16:46 UTC (permalink / raw)
To: pgsql-general
On Friday 11 February 2005 13:39, Bruno Wolff III wrote:
> On Fri, Feb 11, 2005 at 11:07:24 +0000,
>
> David Goodenough <david.goodenough@btconnect.com> wrote:
> > I thought of using an inner select for the join, and using limit 1 to
> > get just the one, and forcing the order by to give me the billing
> > address by preference, but I am then dependant on the sort order
> > of the particular type values I am selecting from.
>
> You can order by boolean expressions such as type = 'billing'.
> You can use that with LIMIT or DISTINCT ON to get just the address you
> want.
Tried this, and got a rather un-intuative answer. If you have two relevant
entries (one billing, the other default) and you:-
order by type = 'billing' limit 1
you get the default one, if you:-
order by type != 'billing' limit 1
you get the billing one.
However:-
order by type = 'billing' DESC limit 1
does get you the billing one.
It makes sense in that false == 0 and true == 1 in many languages
and 0 sorts before 1, but it still feels wrong.
I had not realised I could use a comparison like this in order by.
Thanks
David
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL Query
@ 2007-12-05 10:24 Ashish Karalkar <ashish_postgre@yahoo.co.in>
0 siblings, 2 replies; 45+ messages in thread
From: Ashish Karalkar @ 2007-12-05 10:24 UTC (permalink / raw)
To: pgsql-general; ashish.karalkar@netcore.co.in
Hello List member,
Iha a table containing two columns x and y . for single value of x there are multiple values in y e.g
X Y
------------
1 ABC
2 PQR
3 XYZ
4 LMN
1 LMN
2 XYZ
I want a query that will give me following output
1 ABC:LMN
2 PQR:XYZ
3 XYZ
4 LMN
Any help will be really helpful
Thanks in advance
With Regards
Ashish
---------------------------------
Why delete messages? Unlimited storage is just a click away.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-05 10:35 A. Kretschmer <andreas.kretschmer@schollglas.com>
parent: Ashish Karalkar <ashish_postgre@yahoo.co.in>
1 sibling, 1 reply; 45+ messages in thread
From: A. Kretschmer @ 2007-12-05 10:35 UTC (permalink / raw)
To: pgsql-general
am Wed, dem 05.12.2007, um 10:24:04 +0000 mailte Ashish Karalkar folgendes:
> Hello List member,
>
> Iha a table containing two columns x and y . for single value of x there are
> multiple values in y e.g
>
> X Y
> ------------
> 1 ABC
> 2 PQR
> 3 XYZ
> 4 LMN
> 1 LMN
> 2 XYZ
>
> I want a query that will give me following output
>
> 1 ABC:LMN
> 2 PQR:XYZ
> 3 XYZ
> 4 LMN
>
> Any help will be really helpful
You need a new aggregate-function. A solution for a similar problem (but
with comma instead :) can you find here:
http://www.zigo.dhs.org/postgresql/#comma_aggregate
Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-05 10:47 Ashish Karalkar <ashish_postgre@yahoo.co.in>
parent: A. Kretschmer <andreas.kretschmer@schollglas.com>
0 siblings, 2 replies; 45+ messages in thread
From: Ashish Karalkar @ 2007-12-05 10:47 UTC (permalink / raw)
To: A. Kretschmer <andreas.kretschmer@schollglas.com>; pgsql-general
"A. Kretschmer" <andreas.kretschmer@schollglas.com> wrote: am Wed, dem 05.12.2007, um 10:24:04 +0000 mailte Ashish Karalkar folgendes:
> Hello List member,
>
> Iha a table containing two columns x and y . for single value of x there are
> multiple values in y e.g
>
> X Y
> ------------
> 1 ABC
> 2 PQR
> 3 XYZ
> 4 LMN
> 1 LMN
> 2 XYZ
>
> I want a query that will give me following output
>
> 1 ABC:LMN
> 2 PQR:XYZ
> 3 XYZ
> 4 LMN
>
> Any help will be really helpful
You need a new aggregate-function. A solution for a similar problem (but
with comma instead :) can you find here:
http://www.zigo.dhs.org/postgresql/#comma_aggregate
Thanks Andreas for your replay.
But i don't have an option two send argument to the store proc nither do i know how many multiple records are there for a single X. I want result for all rows of table.
I dont thnink that function will give desired output.
any suggestions?
With Regards
Ashish
Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?
http://archives.postgresql.org/
---------------------------------
Save all your chat conversations. Find them online.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-05 11:15 A. Kretschmer <andreas.kretschmer@schollglas.com>
parent: Ashish Karalkar <ashish_postgre@yahoo.co.in>
1 sibling, 0 replies; 45+ messages in thread
From: A. Kretschmer @ 2007-12-05 11:15 UTC (permalink / raw)
To: Ashish Karalkar <ashish_postgre@yahoo.co.in>; pgsql-general
am Wed, dem 05.12.2007, um 10:47:44 +0000 mailte Ashish Karalkar folgendes:
> > X Y
> > ------------
> > 1 ABC
> > 2 PQR
> > 3 XYZ
> > 4 LMN
> > 1 LMN
> > 2 XYZ
> >
> > I want a query that will give me following output
> >
> > 1 ABC:LMN
> > 2 PQR:XYZ
> > 3 XYZ
> > 4 LMN
> >
> > Any help will be really helpful
>
> You need a new aggregate-function. A solution for a similar problem (but
> with comma instead :) can you find here:
> http://www.zigo.dhs.org/postgresql/#comma_aggregate
>
> Thanks Andreas for your replay.
> But i don't have an option two send argument to the store proc nither do i
> know how many multiple records are there for a single X. I want result for
> all rows of table.
>
> I dont thnink that function will give desired output.
test=# create table Ashish ( x int, y text);
CREATE TABLE
test=*# copy ashish from stdin;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1 abc
>> 2 pqr
>> 3 yxz
>> 4 lmn
>> 1 lmn
>> 2 xyz
>> \.
test=*# CREATE FUNCTION my_aggregate(text,text) RETURNS text AS ' SELECT CASE WHEN $1 <> '''' THEN $1 || '':'' || $2 ELSE $2 END; ' LANGUAGE sql IMMUTABLE STRICT;
CREATE AGGREGATE my_comma (basetype=text, sfunc=my_aggregate , stype=text, initcond='' );
CREATE FUNCTION
CREATE AGGREGATE
test=*# select x, my_comma(y) from ashish group by x;
x | my_comma
---+----------
4 | lmn
3 | yxz
2 | pqr:xyz
1 | abc:lmn
(4 rows)
Okay, i forgot to sort and the chars are in lower case...
Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-05 11:43 Steve Grey <stevegrey78@gmail.com>
parent: Ashish Karalkar <ashish_postgre@yahoo.co.in>
1 sibling, 1 reply; 45+ messages in thread
From: Steve Grey @ 2007-12-05 11:43 UTC (permalink / raw)
To: pgsql-general; +Cc: Ashish Karalkar <ashish_postgre@yahoo.co.in>; A. Kretschmer <andreas.kretschmer@schollglas.com>
Hi,
Its not elegant, and certainly not dynamic or the perfect solution or for
anything but a static dataset but I've approached this in SQL before as...
First work out the maximum number of times each value of X will occur in the
table - something like "select max(subfoo.ycount) from (select foo.X,count(
foo.Y) as ycount from foo group by 1) as subfoo;" might do the job, I
haven't tested it though!
Once you have the count (lets say four, for example), you know how many
subselects you have to make...
select
superfoo.X,
coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X =
superfoo.Xlimit 1)),'') ||
coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X =
superfoo.Xlimit 1 offset 1)),'') ||
coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X =
superfoo.Xlimit 1 offset 2)),'') ||
coalesce((':' || (select subfoo.Y from subfoowhere subfoo.X =
superfoo.Xlimit 1 offset 3)),'')
from superfoo;
Indexes would help alot also.
If anyone has any better ideas on how to do this dynamically for an unknown
count of Y values (this heads towards a pivot table) I'd love to know!
N.B. if you do coalesce((':' || subfoo.Y),'') and subfoo.Y happens to be
null, (':' || subfoo.Y) will also be null as the colon will have been wiped
out so you won't have multiple colons sitting around. Which no-one wants.
Regards,
Steve
On Dec 5, 2007 10:47 AM, Ashish Karalkar <ashish_postgre@yahoo.co.in> wrote:
>
>
> *"A. Kretschmer" <andreas.kretschmer@schollglas.com>* wrote:
>
> am Wed, dem 05.12.2007, um 10:24:04 +0000 mailte Ashish Karalkar
> folgendes:
> > Hello List member,
> >
> > Iha a table containing two columns x and y . for single value of x there
> are
> > multiple values in y e.g
> >
> > X Y
> > ------------
> > 1 ABC
> > 2 PQR
> > 3 XYZ
> > 4 LMN
> > 1 LMN
> > 2 XYZ
> >
> > I want a query that will give me following output
> >
> > 1 ABC:LMN
> > 2 PQR:XYZ
> > 3 XYZ
> > 4 LMN
> >
> > Any help will be really helpful
>
> You need a new aggregate-function. A solution for a similar problem (but
> with comma instead :) can you find here:
> http://www.zigo.dhs.org/postgresql/#comma_aggregate
>
> Thanks Andreas for your replay.
> But i don't have an option two send argument to the store proc nither do i
> know how many multiple records are there for a single X. I want result for
> all rows of table.
>
> I dont thnink that function will give desired output.
>
> any suggestions?
>
> With Regards
> Ashish
>
>
>
>
>
>
> Andreas
> --
> Andreas Kretschmer
> Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
> GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org/
>
>
> ------------------------------
> Save all your chat conversations. Find them online.<http://in.rd.yahoo.com/tagline_webmessenger_3/*http://in.messenger.yahoo.com/webmessengerpromo.php;
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-05 11:46 David Fetter <david@fetter.org>
parent: Ashish Karalkar <ashish_postgre@yahoo.co.in>
1 sibling, 1 reply; 45+ messages in thread
From: David Fetter @ 2007-12-05 11:46 UTC (permalink / raw)
To: Ashish Karalkar <ashish_postgre@yahoo.co.in>; +Cc: pgsql-general; ashish.karalkar@netcore.co.in
On Wed, Dec 05, 2007 at 10:24:04AM +0000, Ashish Karalkar wrote:
> Hello List member,
>
> Iha a table containing two columns x and y . for single value of x there are multiple values in y e.g
>
> X Y
> ------------
> 1 ABC
> 2 PQR
> 3 XYZ
> 4 LMN
> 1 LMN
> 2 XYZ
>
> I want a query that will give me following output
>
> 1 ABC:LMN
> 2 PQR:XYZ
> 3 XYZ
> 4 LMN
>
> Any help will be really helpful
Use the array_accum aggregate from the docs as follows:
SELECT x, array_to_string(array_accum(y),':')
FROM your_table
GROUP BY x;
Cheers,
David.
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com
Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-05 12:04 A. Kretschmer <andreas.kretschmer@schollglas.com>
parent: David Fetter <david@fetter.org>
0 siblings, 0 replies; 45+ messages in thread
From: A. Kretschmer @ 2007-12-05 12:04 UTC (permalink / raw)
To: pgsql-general
am Wed, dem 05.12.2007, um 3:46:26 -0800 mailte David Fetter folgendes:
> Use the array_accum aggregate from the docs as follows:
>
> SELECT x, array_to_string(array_accum(y),':')
> FROM your_table
> GROUP BY x;
Yes, no noubt a better solution as my new aggregat...
Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-05 14:36 Stephane Bortzmeyer <bortzmeyer@nic.fr>
parent: Steve Grey <stevegrey78@gmail.com>
0 siblings, 0 replies; 45+ messages in thread
From: Stephane Bortzmeyer @ 2007-12-05 14:36 UTC (permalink / raw)
To: Steve Grey <stevegrey78@gmail.com>; +Cc: pgsql-general; Ashish Karalkar <ashish_postgre@yahoo.co.in>; A. Kretschmer <andreas.kretschmer@schollglas.com>
On Wed, Dec 05, 2007 at 11:43:08AM +0000,
Steve Grey <stevegrey78@gmail.com> wrote
a message of 153 lines which said:
> First work out the maximum number of times each value of X will occur in the
> table
A better solution, when you do not know this maximum number, is CREATE
AGGREGATE (http://www.postgresql.org/docs/8.1/interactive/xaggr.html)
See details :
http://www.bortzmeyer.org/agregats-postgresql.html
(Yes, it is in french but the SQL examples are in english, variable
names included, so they still can be useful for the OP).
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL Query
@ 2007-12-13 09:47 Ashish Karalkar <ashish_postgre@yahoo.co.in>
0 siblings, 1 reply; 45+ messages in thread
From: Ashish Karalkar @ 2007-12-13 09:47 UTC (permalink / raw)
To: pgsql-novice@postgresql.org; ashish.karalkar@netcore.co.in
Hello List member,
I have a table which ha sfollowing structure
my_table:
(
output_id serial priimary key,
parent_id int,
output_date timesatmp
)
parent_table:
(parent_id int,
parent desc
)
the my_table contains data for previous as well as future output dates.
for single parent there may be multiple rows.
I want to retrieve all the output_id which are having latest output_date but less than now() (no future output_dates) group by parent_id
Thanks in advance
With Regards
Ashish...
---------------------------------
Chat on a cool, new interface. No download required. Click here.
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL Query
@ 2007-12-13 10:00 Ashish Karalkar <ashish_postgre@yahoo.co.in>
0 siblings, 1 reply; 45+ messages in thread
From: Ashish Karalkar @ 2007-12-13 10:00 UTC (permalink / raw)
To: pgsql-general
Hello List member,
I have a table which ha sfollowing structure
my_table:
(
output_id serial priimary key,
parent_id int,
output_date timesatmp
)
parent_table:
(parent_id int,
parent desc
)
the my_table contains data for previous as well as future output dates.
for single parent there may be multiple rows.
I want to retrieve all the output_id which are having latest output_date but less than now() (no future output_dates) group by parent_id
Thanks in advance
With Regards
Ashish...
---------------------------------
Forgot the famous last words? Access your message archive online. Click here.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-13 13:02 Trinath Somanchi <trinaths@intoto.com>
parent: Ashish Karalkar <ashish_postgre@yahoo.co.in>
0 siblings, 1 reply; 45+ messages in thread
From: Trinath Somanchi @ 2007-12-13 13:02 UTC (permalink / raw)
To: Ashish Karalkar <ashish_postgre@yahoo.co.in>; +Cc: pgsql-general
Hi ,
Try this out
SELECT count(o.id)
FROM output_table o , parent_table p
WHERE o.pid=p.pid AND o_date < now()
GROUP BY p.pid ;
On Thu, 13 Dec 2007 10:00:56 +0000 (GMT)
Ashish Karalkar <ashish_postgre@yahoo.co.in> wrote:
> Hello List member,
> I have a table which ha sfollowing structure
>
> my_table:
> (
> output_id serial priimary key,
> parent_id int,
> output_date timesatmp
> )
>
> parent_table:
>
> (parent_id int,
> parent desc
> )
>
> the my_table contains data for previous as well as future output
> dates. for single parent there may be multiple rows.
>
> I want to retrieve all the output_id which are having latest
> output_date but less than now() (no future output_dates) group by
> parent_id
>
> Thanks in advance
>
> With Regards
> Ashish...
>
>
> ---------------------------------
> Forgot the famous last words? Access your message archive online.
> Click here.
--
Trinath Somanchi ,
(trinaths@intoto.com),
********************************************************************************
This email message (including any attachments) is for the sole use of the intended recipient(s)
and may contain confidential, proprietary and privileged information. Any unauthorized review,
use, disclosure or distribution is prohibited. If you are not the intended recipient,
please immediately notify the sender by reply email and destroy all copies of the original message.
Thank you.
Intoto Inc.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-13 14:19 Ranbeer Makin <ranbeer@gmail.com>
parent: Trinath Somanchi <trinaths@intoto.com>
0 siblings, 0 replies; 45+ messages in thread
From: Ranbeer Makin @ 2007-12-13 14:19 UTC (permalink / raw)
To: trinaths@intoto.com; +Cc: Ashish Karalkar <ashish_postgre@yahoo.co.in>; pgsql-general
Hello All:
Ashish wants latest output_date but less than now()
>want to retrieve all the output_id which are having latest
>output_date but less than now() (no future output_dates)
The query written by Tirnath will return count of all output ids given
parent id where output_date is less than now, it won't return rows for
latest output_date.
I propose the following solution:
SELECT o.output_id
FROM my_table o, parent_table p
WHERE o.parent_id = p.parent_id
AND o.output_date < now()
AND NOT EXISTS
(
SELECT out.output_date
FROM my_table out
WHERE out.parent_id = o.parent_id
AND out.output_date < now ()
AND o.output_date < out.output_date
);
The idea is get first maximum date which is less than now(). Assuming now()
as the maximum date, the problem reduces to finding second maximum date.
The nested query means: the date in context (outer query date) shouldn't be
less than any date which is less than now().
This will give you the latest output date grouped by parent_id albeit
there's no group clause used. :-)
Let me know if there's any mistake.
Cheers,
Ranbeer Makin
On Dec 13, 2007 6:32 PM, Trinath Somanchi <trinaths@intoto.com> wrote:
> Hi ,
>
> Try this out
>
> SELECT count(o.id)
> FROM output_table o , parent_table p
> WHERE o.pid=p.pid AND o_date < now()
> GROUP BY p.pid ;
>
>
> On Thu, 13 Dec 2007 10:00:56 +0000 (GMT)
> Ashish Karalkar <ashish_postgre@yahoo.co.in> wrote:
>
> > Hello List member,
> > I have a table which ha sfollowing structure
> >
> > my_table:
> > (
> > output_id serial priimary key,
> > parent_id int,
> > output_date timesatmp
> > )
> >
> > parent_table:
> >
> > (parent_id int,
> > parent desc
> > )
> >
> > the my_table contains data for previous as well as future output
> > dates. for single parent there may be multiple rows.
> >
> > I want to retrieve all the output_id which are having latest
> > output_date but less than now() (no future output_dates) group by
> > parent_id
> >
> > Thanks in advance
> >
> > With Regards
> > Ashish...
> >
> >
> > ---------------------------------
> > Forgot the famous last words? Access your message archive online.
> > Click here.
>
> --
> Trinath Somanchi ,
> (trinaths@intoto.com),
>
>
> ********************************************************************************
> This email message (including any attachments) is for the sole use of the
> intended recipient(s)
> and may contain confidential, proprietary and privileged information. Any
> unauthorized review,
> use, disclosure or distribution is prohibited. If you are not the intended
> recipient,
> please immediately notify the sender by reply email and destroy all copies
> of the original message.
> Thank you.
>
> Intoto Inc.
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL Query
@ 2007-12-13 17:23 Mark Wimer <mwimer@usgs.gov>
parent: Ashish Karalkar <ashish_postgre@yahoo.co.in>
0 siblings, 0 replies; 45+ messages in thread
From: Mark Wimer @ 2007-12-13 17:23 UTC (permalink / raw)
To: Ashish Karalkar <ashish_postgre@yahoo.co.in>; +Cc: ashish.karalkar@netcore.co.in; pgsql-novice@postgresql.org
See if this works; the max aggregate in the subquery could be just about
any function, since it's only going to join to one row.
select max(m1.output_date), m1.parent_id, max(m2.output_id) as
latest_output
from my_table M1 inner join (select output_date, output_id, parent_ID from
my_table) as M2
on m1.output_date = m2.output_date and m1.parent_ID = m2.parent_ID
Where M1.output_date < now()
Group by m1.parent_id
-- Mark
Ashish Karalkar <ashish_postgre@yahoo.co.in>
Sent by: pgsql-novice-owner@postgresql.org
12/13/2007 04:47 AM
To
pgsql-novice@postgresql.org, ashish.karalkar@netcore.co.in
cc
Subject
[NOVICE] SQL Query
Hello List member,
I have a table which ha sfollowing structure
my_table:
(
output_id serial priimary key,
parent_id int,
output_date timesatmp
)
parent_table:
(parent_id int,
parent desc
)
the my_table contains data for previous as well as future output dates.
for single parent there may be multiple rows.
I want to retrieve all the output_id which are having latest output_date
but less than now() (no future output_dates) group by parent_id
Thanks in advance
With Regards
Ashish...
Chat on a cool, new interface. No download required. Click here.
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL query
@ 2008-11-22 04:33 Michael Thorsen <mthorsen1980@gmail.com>
0 siblings, 2 replies; 45+ messages in thread
From: Michael Thorsen @ 2008-11-22 04:33 UTC (permalink / raw)
To: pgsql-general
I am running the Postgres(8.2.11) on Windows.
I have 2 tables, one with users and one with locations.
user_table
---------------
user_id user_code price value
1 22222 45.23 -97.82
2 33333 42.67 -98.32
3 44444 35.56 -76.32
locations
--------------
id code price value
1 22222 45.23 -97.82
2 33333 42.67 -98.32
3 44444 43.26 -98.65
I have a query that takes every user and looks into locations to see if the
code, price and value match up. If they do then count it.
select count(*)
from user_table u, locations l
where u.user_code = l.code
and u.price = l.price
and u.value = l.value;
The answer to this should be 2, but when I run my query I get 4 (in fact
more entries than user_table) which is incorrect. What am I doing
incorrectly? I have been breaking my head over this for a while. Is there
some other query to get the required results? Any help would be highly
appreciated. I gave a simple example above, but the query runs over 2 tables
with about a million entries in each. So I am unable to verify what is
wrong, but I know the count is incorrect as I should not have more than what
is in the user_table.
Thanks,
Michael
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2008-11-22 15:47 Raymond O'Donnell <rod@iol.ie>
parent: Michael Thorsen <mthorsen1980@gmail.com>
1 sibling, 1 reply; 45+ messages in thread
From: Raymond O'Donnell @ 2008-11-22 15:47 UTC (permalink / raw)
To: Michael Thorsen <mthorsen1980@gmail.com>; +Cc: pgsql-general
On 22/11/2008 04:33, Michael Thorsen wrote:
> select count(*)
> from user_table u, locations l
> where u.user_code = l.code
> and u.price = l.price
> and u.value = l.value;
>
> The answer to this should be 2, but when I run my query I get 4 (in fact
Are you sure that's the query that's being run? I just tried it here,
and got 2 - this was using your data above.
What do your table definitions look like? - here's what I did:
CREATE TABLE user_table
(
user_id integer NOT NULL,
user_code integer NOT NULL,
price numeric(6,2) NOT NULL,
"value" numeric(6,2) NOT NULL,
CONSTRAINT user_pk PRIMARY KEY (user_id)
)
WITH (OIDS=FALSE);
CREATE TABLE locations
(
id integer NOT NULL,
code integer NOT NULL,
price numeric(6,2) NOT NULL,
"value" numeric(6,2) NOT NULL,
CONSTRAINT location_pk PRIMARY KEY (id)
)
WITH (OIDS=FALSE);
Does this correspond to what you have?
Ray.
------------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
Galway Cathedral Recitals: http://www.galwaycathedral.org/recitals
------------------------------------------------------------------
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2008-11-22 16:23 Raymond O'Donnell <rod@iol.ie>
parent: Raymond O'Donnell <rod@iol.ie>
0 siblings, 0 replies; 45+ messages in thread
From: Raymond O'Donnell @ 2008-11-22 16:23 UTC (permalink / raw)
To: Michael Thorsen <mthorsen1980@gmail.com>; pgsql-general
On 22/11/2008 16:07, Michael Thorsen wrote:
> For the most part yes. The price and value were "real" columns,
> otherwise the rest of it is the same. On a small data set I seem to get
That's almost certainly the problem, so - rounding errors are causing
the equality test in the join to fail. You should use NUMERIC for those
floating-point values.
Have a look at what the docs say on REAL and family:
http://www.postgresql.org/docs/8.3/static/datatype-numeric.html#DATATYPE-FLOAT
Ray.
PS - please don't top-post, as it makes the thread difficult to follow.
------------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
Galway Cathedral Recitals: http://www.galwaycathedral.org/recitals
------------------------------------------------------------------
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2008-11-22 18:17 Tom Lane <tgl@sss.pgh.pa.us>
parent: Michael Thorsen <mthorsen1980@gmail.com>
1 sibling, 0 replies; 45+ messages in thread
From: Tom Lane @ 2008-11-22 18:17 UTC (permalink / raw)
To: Michael Thorsen <mthorsen1980@gmail.com>; +Cc: pgsql-general
"Michael Thorsen" <mthorsen1980@gmail.com> writes:
> ... I gave a simple example above, but the query runs over 2 tables
> with about a million entries in each. So I am unable to verify what is
> wrong, but I know the count is incorrect as I should not have more than what
> is in the user_table.
You could easily get a count larger than the number of rows in
user_table, if there are rows in user_table that join to multiple rows
in the locations table. So look for duplicated data in locations ...
regards, tom lane
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL query
@ 2019-04-18 16:43 Vikas Sharma <shavikas@gmail.com>
0 siblings, 4 replies; 45+ messages in thread
From: Vikas Sharma @ 2019-04-18 16:43 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hi,
I have come across a query that a developer wrote to update a few rows in
table, the query did update the two desired rows but also updated the
rest of the table with the column value as 'false'.
Update tableA set col1 = null and col2 in (1,2);
The query updated col1 to null for the rows where col2 was either 1 or 2,
rest of rows were also updated for col1 to 'false'.
The above was run without where clause.
Could the experts throw some light on this?
Regards
Vikas Sharma
^ permalink raw reply [nested|flat] 45+ messages in thread
* SQL query
@ 2019-04-18 17:00 David G. Johnston <david.g.johnston@gmail.com>
parent: Vikas Sharma <shavikas@gmail.com>
3 siblings, 0 replies; 45+ messages in thread
From: David G. Johnston @ 2019-04-18 17:00 UTC (permalink / raw)
To: Vikas Sharma <shavikas@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Thursday, April 18, 2019, Vikas Sharma <shavikas@gmail.com> wrote:
>
> The above was run without where clause.
>
There was no where clause thus every row has to be updated by
definition...null and bool evaluates to either false or null since if bool
is false the null doesn’t matter and if bool is true the result is unknown
from the null.
David J.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2019-04-18 17:01 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Vikas Sharma <shavikas@gmail.com>
3 siblings, 0 replies; 45+ messages in thread
From: Adrian Klaver @ 2019-04-18 17:01 UTC (permalink / raw)
To: Vikas Sharma <shavikas@gmail.com>; pgsql-general@lists.postgresql.org
On 4/18/19 9:43 AM, Vikas Sharma wrote:
> Hi,
>
> I have come across a query that a developer wrote to update a few rows
> in table, the query did update the two desired rows but also updated the
> rest of the table with the column value as 'false'.
>
> Update tableA set col1 = null and col2 in (1,2);
>
> The query updated col1 to null for the rows where col2 was either 1 or
> 2, rest of rows were also updated for col1 to 'false'.
> The above was run without where clause.
> Could the experts throw some light on this?
Hmm.
What Postgres version?
Assuming col1 is boolean, correct?
My experimentation:
create table up_test(id integer, col1 boolean, col2 integer);
insert into up_test values (1, 't', 1), (2, null, 2), (3, 't', 4);
update up_test set col1=null and col2 in(1,2);
UPDATE 3
select * from up_test;
id | col1 | col2
----+------+------
1 | | 1
2 | | 2
3 | f | 4
(3 rows)
truncate up_test ;
TRUNCATE TABLE
insert into up_test values (1, 't', 1), (2, null, 2), (3, 't', 4);
INSERT 0 3
update up_test set col1=(null and col2 in(1,2));
UPDATE 3
test_(postgres)# select * from up_test;
id | col1 | col2
----+------+------
1 | | 1
2 | | 2
3 | f | 4
Looks to me it is seeing the and as part of an expression.
>
> Regards
> Vikas Sharma
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2019-04-18 17:58 Ron <ronljohnsonjr@gmail.com>
parent: Vikas Sharma <shavikas@gmail.com>
3 siblings, 0 replies; 45+ messages in thread
From: Ron @ 2019-04-18 17:58 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 4/18/19 11:43 AM, Vikas Sharma wrote:
> Hi,
>
> I have come across a query that a developer wrote to update a few rows in
> table, the query did update the two desired rows but also updated the
> rest of the table with the column value as 'false'.
>
> Update tableA set col1 = null and col2 in (1,2);
>
> The query updated col1 to null for the rows where col2 was either 1 or 2,
> rest of rows were also updated for col1 to 'false'.
> The above was run without where clause.
On the QA server, right?
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2019-04-18 18:36 Ravi Krishna <srkrishna@myself.com>
parent: Vikas Sharma <shavikas@gmail.com>
3 siblings, 2 replies; 45+ messages in thread
From: Ravi Krishna @ 2019-04-18 18:36 UTC (permalink / raw)
To: Vikas Sharma <shavikas@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
Not able to produce this with PG 11.1
If col1 is any type other than boolean, the update statement fails in syntax.
If col1 is boolean, then it updated it correctly. In other words
update col1 = NULL
and col2 in (1,2)
is treated same as
update col1 = NULL
where col2 in (1,2)
Also I checked it on DB2 which rejected the UPDATE sql as bad syntax, as expected.
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2019-04-18 18:40 Ravi Krishna <srkrishna@myself.com>
parent: Ravi Krishna <srkrishna@myself.com>
1 sibling, 0 replies; 45+ messages in thread
From: Ravi Krishna @ 2019-04-18 18:40 UTC (permalink / raw)
To: ; +Cc: Vikas Sharma <shavikas@gmail.com>; pgsql-general@lists.postgresql.org
Oh wait. I see that it in both cases it did update correct target rows, but the value of col1 for non matching rows
is different. In the first case (and col2), the non matching rows also got updated.
So yes, same behavior like yours.
> Sent: Thursday, April 18, 2019 at 2:36 PM
> From: "Ravi Krishna" <srkrishna@myself.com>
> To: "Vikas Sharma" <shavikas@gmail.com>
> Cc: pgsql-general@lists.postgresql.org
> Subject: Re: SQL query
>
> Not able to produce this with PG 11.1
>
> If col1 is any type other than boolean, the update statement fails in syntax.
> If col1 is boolean, then it updated it correctly. In other words
> update col1 = NULL
> and col2 in (1,2)
> is treated same as
> update col1 = NULL
> where col2 in (1,2)
>
> Also I checked it on DB2 which rejected the UPDATE sql as bad syntax, as expected.
>
>
>
>
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2019-04-18 18:41 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Ravi Krishna <srkrishna@myself.com>
1 sibling, 1 reply; 45+ messages in thread
From: Adrian Klaver @ 2019-04-18 18:41 UTC (permalink / raw)
To: Ravi Krishna <srkrishna@myself.com>; Vikas Sharma <shavikas@gmail.com>; +Cc: pgsql-general@lists.postgresql.org
On 4/18/19 11:36 AM, Ravi Krishna wrote:
> Not able to produce this with PG 11.1
>
> If col1 is any type other than boolean, the update statement fails in syntax.
> If col1 is boolean, then it updated it correctly. In other words
> update col1 = NULL
> and col2 in (1,2)
> is treated same as
> update col1 = NULL
> where col2 in (1,2)
The above is not the same format as OP's query:
Update tableA set col1 = null and col2 in (1,2);
>
> Also I checked it on DB2 which rejected the UPDATE sql as bad syntax, as expected.
>
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 45+ messages in thread
* Re: SQL query
@ 2019-04-18 19:12 Ravi Krishna <srkrishna@myself.com>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 0 replies; 45+ messages in thread
From: Ravi Krishna @ 2019-04-18 19:12 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Vikas Sharma <shavikas@gmail.com>; pgsql-general@lists.postgresql.org
> The above is not the same format as OP's query:
>
> Update tableA set col1 = null and col2 in (1,2);
I did include set in the sql. I typed it wrong here.
^ permalink raw reply [nested|flat] 45+ messages in thread
end of thread, other threads:[~2019-04-18 19:12 UTC | newest]
Thread overview: 45+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-11-29 19:23 SQL Query Scott Taylor <scott.taylor@4i-dotcom.com>
2002-11-29 19:32 ` Tariq Muhammad <tmuhamma@libertyrms.com>
2002-11-29 20:07 ` Scott Lamb <slamb@slamb.org>
2002-11-30 07:45 ` Joel Burton <joel@joelburton.com>
2002-11-30 08:05 ` Scott Lamb <slamb@slamb.org>
2002-11-30 08:21 ` Joel Burton <joel@joelburton.com>
2002-12-01 15:53 SQL Query Scott Taylor <scott.taylor@4i-dotcom.com>
2003-02-19 16:35 SQL query... jerome <jerome@gmanmi.tv>
2003-02-19 13:38 ` Re: SQL query... Mario Weilguni <mweilguni@sime.com>
2003-02-19 14:31 ` Re: SQL query... greg@turnstep.com
2003-02-19 15:28 ` Re: SQL query... Bruno Wolff III <bruno@wolff.to>
2005-02-11 11:07 SQL query David Goodenough <david.goodenough@btconnect.com>
2005-02-11 11:31 ` Re: SQL query Matt K <matt@kynx.org>
2005-02-11 16:39 ` Re: SQL query David Goodenough <david.goodenough@btconnect.com>
2005-02-11 11:41 ` Re: SQL query Janning Vygen <vygen@gmx.de>
2005-02-11 16:41 ` Re: SQL query David Goodenough <david.goodenough@btconnect.com>
2005-02-11 11:46 ` Re: SQL query Richard Huxton <dev@archonet.com>
2005-02-11 16:39 ` Re: SQL query David Goodenough <david.goodenough@btconnect.com>
2005-02-11 13:39 ` Re: SQL query Bruno Wolff III <bruno@wolff.to>
2005-02-11 16:46 ` Re: SQL query David Goodenough <david.goodenough@btconnect.com>
2007-12-05 10:24 SQL Query Ashish Karalkar <ashish_postgre@yahoo.co.in>
2007-12-05 10:35 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2007-12-05 10:47 ` Ashish Karalkar <ashish_postgre@yahoo.co.in>
2007-12-05 11:15 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2007-12-05 11:43 ` Steve Grey <stevegrey78@gmail.com>
2007-12-05 14:36 ` Stephane Bortzmeyer <bortzmeyer@nic.fr>
2007-12-05 11:46 ` David Fetter <david@fetter.org>
2007-12-05 12:04 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2007-12-13 09:47 SQL Query Ashish Karalkar <ashish_postgre@yahoo.co.in>
2007-12-13 17:23 ` Mark Wimer <mwimer@usgs.gov>
2007-12-13 10:00 SQL Query Ashish Karalkar <ashish_postgre@yahoo.co.in>
2007-12-13 13:02 ` Trinath Somanchi <trinaths@intoto.com>
2007-12-13 14:19 ` Ranbeer Makin <ranbeer@gmail.com>
2008-11-22 04:33 SQL query Michael Thorsen <mthorsen1980@gmail.com>
2008-11-22 15:47 ` Re: SQL query Raymond O'Donnell <rod@iol.ie>
2008-11-22 16:23 ` Re: SQL query Raymond O'Donnell <rod@iol.ie>
2008-11-22 18:17 ` Re: SQL query Tom Lane <tgl@sss.pgh.pa.us>
2019-04-18 16:43 SQL query Vikas Sharma <shavikas@gmail.com>
2019-04-18 17:00 ` SQL query David G. Johnston <david.g.johnston@gmail.com>
2019-04-18 17:01 ` Re: SQL query Adrian Klaver <adrian.klaver@aklaver.com>
2019-04-18 17:58 ` Re: SQL query Ron <ronljohnsonjr@gmail.com>
2019-04-18 18:36 ` Re: SQL query Ravi Krishna <srkrishna@myself.com>
2019-04-18 18:40 ` Re: SQL query Ravi Krishna <srkrishna@myself.com>
2019-04-18 18:41 ` Re: SQL query Adrian Klaver <adrian.klaver@aklaver.com>
2019-04-18 19:12 ` Re: SQL query Ravi Krishna <srkrishna@myself.com>
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