Thread: Insert based in a select
Hi list, Could someone help me with this problem I have a table like this Master table id number 10 9898398398 20 9938378390 30 9873636736 and I want to make an insert into a table with this structure: Secondary table id(sequence), master_id 1 10 2 20 3 30 It is possible to make this insert based in all records of my master table ? Any help would be very welcomed. Best Regards Ezequias
INSERT INTO second_table (master_id) SELECT id FROM master_table ORDER BY id;<br /><br /> Was it important to you that theid and master id in the second table match up? ie, 1 and 10, 2 and 20, 3 and 30 - not 1 and 30, 2 and 10, 3 and 20 etc...<br/><br /> ~p<br /><br /> On Sat, 2007-02-24 at 21:06 -0300, Ezequias Rodrigues da Rocha wrote: <blockquote type="CITE"><pre> <font color="#000000">Hi list,</font> <font color="#000000">Could someone help me with this problem</font> <font color="#000000">I have a table like this</font> <font color="#000000">Master table</font> <font color="#000000">id number</font> <font color="#000000">10 9898398398</font> <font color="#000000">20 9938378390</font> <font color="#000000">30 9873636736</font> <font color="#000000">and I want to make an insert into a table with this structure:</font> <font color="#000000">Secondary table</font> <font color="#000000">id(sequence), master_id</font> <font color="#000000">1 10</font> <font color="#000000">2 20</font> <font color="#000000">3 30</font> <font color="#000000">It is possible to make this insert based in all records of my master table ?</font> <font color="#000000">Any help would be very welcomed.</font> <font color="#000000">Best Regards</font> <font color="#000000">Ezequias</font> <font color="#000000">---------------------------(end of broadcast)---------------------------</font> <font color="#000000">TIP 7: You can help support the PostgreSQL project by donating at</font> <font color="#000000"> <a href="http://www.postgresql.org/about/donate">http://www.postgresql.org/about/donate</a></font> </pre></blockquote><br /><p><b>*******************Confidentiality and Privilege Notice*******************</b><p> The materialcontained in this message is privileged and confidential to the addressee. If you are not the addressee indicatedin this message or responsible for delivery of the message to such person, you may not copy or deliver this messageto anyone, and you should destroy it and kindly notify the sender by reply email. <p> Information in this messagethat does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta.Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequentialloss arising from transmission of this message or any attachments <br />
<div class="Section1"><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy">(Please reply to the list when replying)</span></font><p class="MsoNormal"><font color="navy"face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy">The error is correct – you are telling PG to insert one row (the literal values you’vepassed to INSERT), but the sub-query is returning multiple rows which won’t fit in a single row.</span></font><p class="MsoNormal"><fontcolor="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy">I see 2 options, but someone else I’m sure will have a more elegant way to do it:</span></font><pclass="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy">1) insert into base.ingresso (select id from base.cartao order by id)</span></font><pclass="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy">2) insert into base.ingresso values (nextval(' base.ingresso_id'), 4, now(), 12.34,12.34, 1, 1678, (select id from base.cartao where id not in (SELECT id from base.ingresso) order by id limit 1), 2,25, 99)</span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy">Number 1 will create one row in base.ingresso for each row in base.cartao. You willthen need to use UPDATE to adjust the values in each of those rows to the other values you want.</span></font><p class="MsoNormal"><fontcolor="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy">Number 2 would need to be run over and over again – not a practical option I imagine…</span></font><pclass="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><span style="font-size: 10.0pt;font-family:Arial;color:navy">~p</span></font><p class="MsoNormal"><font color="navy" face="Arial" size="2"><spanstyle="font-size: 10.0pt;font-family:Arial;color:navy"> </span></font><p class="MsoNormal" style="margin-left:36.0pt"><font face="Tahoma" size="2"><spanlang="EN-US" style="font-size:10.0pt;font-family:Tahoma">-----Original Message-----<br /><b><span style="font-weight:bold">From:</span></b>Ezequias Rodrigues da Rocha [mailto:ezequias.rocha@gmail.com] <br /><b><span style="font-weight:bold">Sent:</span></b></span></font><font face="Tahoma" size="2"><span lang="EN-US" style="font-size:10.0pt;font-family:Tahoma">Monday,26 February 2007</span></font><font face="Tahoma" size="2"><span lang="EN-US"style="font-size:10.0pt;font-family:Tahoma"> </span></font><font face="Tahoma" size="2"><span lang="EN-US" style="font-size:10.0pt;font-family:Tahoma">00:43</span></font><fontface="Tahoma" size="2"><span lang="EN-US" style="font-size:10.0pt;font-family:Tahoma"><br/><b><span style="font-weight:bold">To:</span></b> phillip.smith@weatherbeeta.com.au<br/><b><span style="font-weight:bold">Subject:</span></b> Re: [SQL] Insert based in a select</span></font><pclass="MsoNormal" style="margin-left:36.0pt"><font face="Times New Roman" size="3"><span style="font-size:12.0pt"> </span></font><pclass="MsoNormal" style="margin-left:36.0pt"><font face="Times New Roman" size="3"><spanstyle="font-size:12.0pt">Phillip,<br /><br /> Thank you for the information but the master (id) is only a fieldof my secondary table.<br /><br /> My sql statement is like this:<br /><br /></span></font><b><font size="1"><span style="font-size:7.5pt;font-weight:bold">insertinto base.ingresso values (nextval(' base.ingresso_id'), 4, now(), 12.34,12.34, 1, 1678, (select id from base.cartao order by id), 2, 25, 99)</span></font></b><br /><br /> And I am gettingthe following error return message:<br /><br /><b><span style="font-weight:bold">ERROR: more than one row returnedby a subquery used as an expression <br /> SQL state: 21000<br /></span></b><br /> Could you give me another helpwith this ?<br /><br /> Ezequias</div><br /><p><b>*******************Confidentiality and Privilege Notice*******************</b><p>The material contained in this message is privileged and confidential to the addressee. Ifyou are not the addressee indicated in this message or responsible for delivery of the message to such person, you maynot copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email. <p>Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither givennor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct,indirect or consequential loss arising from transmission of this message or any attachments <br />
The first statement i didn't understand. You are adding only the first field on my base.ingresso table ?
Regards
Ezequias
(Please reply to the list when replying)
The error is correct – you are telling PG to insert one row (the literal values you've passed to INSERT), but the sub-query is returning multiple rows which won't fit in a single row.
I see 2 options, but someone else I'm sure will have a more elegant way to do it:
1) insert into base.ingresso (select id from base.cartao order by id)
2) insert into base.ingresso values (nextval(' base.ingresso_id'), 4, now(), 12.34, 12.34, 1, 1678, (select id from base.cartao where id not in (SELECT id from base.ingresso) order by id limit 1), 2, 25, 99)
Number 1 will create one row in base.ingresso for each row in base.cartao. You will then need to use UPDATE to adjust the values in each of those rows to the other values you want.
Number 2 would need to be run over and over again – not a practical option I imagine…
~p
-----Original Message-----
From: Ezequias Rodrigues da Rocha [mailto:ezequias.rocha@gmail.com]
Sent: Monday, 26 February 2007 00:43
To: phillip.smith@weatherbeeta.com.au
Subject: Re: [SQL] Insert based in a select
Phillip,
Thank you for the information but the master (id) is only a field of my secondary table.
My sql statement is like this:
insert into base.ingresso values (nextval(' base.ingresso_id'), 4, now(), 12.34, 12.34, 1, 1678, (select id from base.cartao order by id), 2, 25, 99)
And I am getting the following error return message:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
Could you give me another help with this ?
Ezequias*******************Confidentiality and Privilege Notice*******************
The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.
Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Atenciosamente (Sincerely)
Ezequias Rodrigues da Rocha
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
A pior das democracias ainda é melhor do que a melhor das ditaduras
The worst of democracies is still better than the better of dictatorships
http://ezequiasrocha.blogspot.com/
Apologies – the first statement should have included the column name to insert to:
INSERT INTO base.ingresso (id_column) (SELECT id FROM base.cartao ORDER BY id)
That will insert one row in ingresso for each row in cartao – only changing the id column. All the other columns in each row will be populated with the default values. That is why you will need to do an UPDATE afterwards. Something like:
UPDATE base.ingresso
SET col1 = ‘value1’, sol2 = ‘value2’
WHERE id IN (SELECT id FROM base.cartao);
Hope this makes sense,
~p
-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Ezequias Rodrigues da Rocha
Sent: Monday, 26 February 2007 10:58
To: Phillip Smith
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Insert based in a select
The second statement works but only 1 row was affected. It do not apply to me.
The first statement i didn't understand. You are adding only the first field on my base.ingresso table ?
Regards
Ezequias
2007/2/25, Phillip Smith <phillip.smith@weatherbeeta.com.au>:
(Please reply to the list when replying)
The error is correct – you are telling PG to insert one row (the literal values you've passed to INSERT), but the sub-query is returning multiple rows which won't fit in a single row.
I see 2 options, but someone else I'm sure will have a more elegant way to do it:
1) insert into base.ingresso (select id from base.cartao order by id)
2) insert into base.ingresso values (nextval(' base.ingresso_id'), 4, now(), 12.34, 12.34, 1, 1678, (select id from base.cartao where id not in (SELECT id from base.ingresso) order by id limit 1), 2, 25, 99)
Number 1 will create one row in base.ingresso for each row in base.cartao. You will then need to use UPDATE to adjust the values in each of those rows to the other values you want.
Number 2 would need to be run over and over again – not a practical option I imagine…
~p
-----Original Message-----
From: Ezequias Rodrigues da Rocha [mailto:ezequias.rocha@gmail.com]
Sent: Monday, 26 February 2007 00:43
To: phillip.smith@weatherbeeta.com.au
Subject: Re: [SQL] Insert based in a select
Phillip,
Thank you for the information but the master (id) is only a field of my secondary table.
My sql statement is like this:
insert into base.ingresso values (nextval(' base.ingresso_id'), 4, now(), 12.34, 12.34, 1, 1678, (select id from base.cartao order by id), 2, 25, 99)
And I am getting the following error return message:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
Could you give me another help with this ?
Ezequias
*******************Confidentiality and Privilege Notice*******************
The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.
Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Atenciosamente (Sincerely)
Ezequias Rodrigues da Rocha
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
A pior das democracias ainda é melhor do que a melhor das ditaduras
The worst of democracies is still better than the better of dictatorships
http://ezequiasrocha.blogspot.com/
*******************Confidentiality and Privilege Notice*******************
The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.
Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments
Anyway, it don't apply to my point. I must do an insert in each row of my ingresso table with his Id, some fields and the id of my cartao table.
You help is very welcomed but my solution doesn't came this time.
My best regards
Ezequias
Apologies – the first statement should have included the column name to insert to:
INSERT INTO base.ingresso (id_column) (SELECT id FROM base.cartao ORDER BY id)
That will insert one row in ingresso for each row in cartao – only changing the id column. All the other columns in each row will be populated with the default values. That is why you will need to do an UPDATE afterwards. Something like:
UPDATE base.ingresso
SET col1 = 'value1', sol2 = 'value2'
WHERE id IN (SELECT id FROM base.cartao);
Hope this makes sense,
~p
-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto: pgsql-sql-owner@postgresql.org] On Behalf Of Ezequias Rodrigues da Rocha
Sent: Monday, 26 February 2007 10:58
To: Phillip Smith
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Insert based in a select
The second statement works but only 1 row was affected. It do not apply to me.
The first statement i didn't understand. You are adding only the first field on my base.ingresso table ?
Regards
Ezequias2007/2/25, Phillip Smith <phillip.smith@weatherbeeta.com.au>:
(Please reply to the list when replying)
The error is correct – you are telling PG to insert one row (the literal values you've passed to INSERT), but the sub-query is returning multiple rows which won't fit in a single row.
I see 2 options, but someone else I'm sure will have a more elegant way to do it:
1) insert into base.ingresso (select id from base.cartao order by id)
2) insert into base.ingresso values (nextval(' base.ingresso_id'), 4, now(), 12.34, 12.34, 1, 1678, (select id from base.cartao where id not in (SELECT id from base.ingresso) order by id limit 1), 2, 25, 99)
Number 1 will create one row in base.ingresso for each row in base.cartao. You will then need to use UPDATE to adjust the values in each of those rows to the other values you want.
Number 2 would need to be run over and over again – not a practical option I imagine…
~p
-----Original Message-----
From: Ezequias Rodrigues da Rocha [mailto:ezequias.rocha@gmail.com]
Sent: Monday, 26 February 2007 00:43
To: phillip.smith@weatherbeeta.com.au
Subject: Re: [SQL] Insert based in a select
Phillip,
Thank you for the information but the master (id) is only a field of my secondary table.
My sql statement is like this:
insert into base.ingresso values (nextval(' base.ingresso_id'), 4, now(), 12.34, 12.34, 1, 1678, (select id from base.cartao order by id), 2, 25, 99)
And I am getting the following error return message:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
Could you give me another help with this ?
Ezequias
*******************Confidentiality and Privilege Notice*******************
The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.
Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Atenciosamente (Sincerely)
Ezequias Rodrigues da Rocha
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
A pior das democracias ainda � melhor do que a melhor das ditaduras
The worst of democracies is still better than the better of dictatorships
http://ezequiasrocha.blogspot.com/*******************Confidentiality and Privilege Notice*******************
The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.
Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Atenciosamente (Sincerely)
Ezequias Rodrigues da Rocha
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
A pior das democracias ainda é melhor do que a melhor das ditaduras
The worst of democracies is still better than the better of dictatorships
http://ezequiasrocha.blogspot.com/