[SQL] Insert a description while creating a table
Hi, I want to insert descriptions at the columns of my tables but without using the command COMMENT ON. I want to do it together with the table creation. Is that possible? I wanna do something like this: create table test ( id serial 'Descripitions about ID', name varchar(50) 'Descriptions about NAME' ); Thanks in advance Luiz. -- * * Luiz Fernando Pinto * * -*- * * Klais Desenvolvimento * * [EMAIL PROTECTED] * * ---(end of broadcast)--- TIP 7: don't forget to increase your free space map settings
Re: [SQL] Insert a description while creating a table
Hi Christoph, I'm thinking that the best solution is create a script in perl or python that executes de COMMENT command to me. My initial idea would be comment my columns in a standard way and then run the script. My comment that will turn into description will start with "/*$" instead of the simple "/*", for instance. Regards, Luiz. On Wed, 13 Aug 2003, Christoph Haller wrote: > > > I want to insert descriptions at the columns of my tables but > without > > > using the command COMMENT ON. I want to do it together with the > table > > > creation. Is that possible? > > > > > > I wanna do something like this: > > > > > > create table test ( > > > id serial 'Descripitions about ID', > > > name varchar(50) 'Descriptions about NAME' > > > ); > > > > Probably not going to happen in the backend. > > > > However, you should be able to accomplish that with a little bit of > Perl > > to pre-process the SQL. > > > That perl script comes to my mind too. > The reason why Luiz doesn't like it, may be because you can't > see these descriptions within psql using \dd test > > I did > the create table, then > COMMENT ON COLUMN test.id is 'Descripitions about ID'; > COMMENT ON COLUMN test.name is 'Descriptions about NAME'; > \dd test shows > > Object descriptions > Schema | Name | Object | Description > +--++- > (0 rows) > > This is odd. OK, I know the doc says > \dd [ pattern ] > > Shows the descriptions of objects matching the pattern, or of all > visible objects if no argument is given. But in either case, > only objects that have a description are listed. ("Object" covers > aggregates, functions, operators, types, relations (tables, views, > indexes, sequences, large objects), rules, and triggers.) For > example: > > Nothing about columns. > But what is the purpose of comments on columns if you can only get them > via > select * from pg_description where > objoid = (select typrelid from pg_type where typname='test') > order by objsubid ; > objoid | classoid | objsubid | description > +--+--+- > 17326 | 1259 |1 | Descripitions about ID > 17326 | 1259 |2 | Descriptions about NAME > (2 rows) > > which you'll have to find out on your own. > > Regards, Christoph > > > > ---(end of broadcast)--- > TIP 4: Don't 'kill -9' the postmaster > > -- * * Luiz Fernando Pinto * * -*- * * Klais Desenvolvimento * * [EMAIL PROTECTED] * * ---(end of broadcast)--- TIP 7: don't forget to increase your free space map settings
[SQL] Break a Report in Run Time
I have a apllication with a lot of reports, where I need to break a process report in run time. How Can I do this? Thanks. Jander. ---(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
[SQL] UPDATE in a specific order
Hi, I have a follow scenario: CREATE TABLE table1 ( id integer , vlpr numeric(10,2) , vlab numeric(10,2) , vlbx numeric(15,5) , pct numeric(12,8) ); CREATE TABLE table2 ( id integer , fk_table1 integer , tpop char(2) , valor numeric(15,5) ); insert into table1 VALUES ( 1, 200 , 0 , 0 , 1 ); insert into table2 VALUES ( 1, 1 , 'CR' , 100 ) , ( 2, 1 , 'BX' , 15 ) , ( 3, 1 , 'AC' , 40 ); I need to make update of table1 with data on table2 in the order of id of table2 I´m trying to do an update like this: UPDATE table1 SET vlab = vlab + CASE WHEN tHist.tpop IN ('BX' , 'DC') THEN - tHist.valor ELSE tHist.valor END , vlbx = vlbx + CASE WHEN tHist.tpop IN ('BX', 'DC') THEN tHist.valor ELSE 0 END , pct = CASE WHEN tHist.tpop in ('AC', 'DC' ) THEN (vlpr - vlbx) / vlab ELSE pct END FROM ( SELECT * FROM table2 ORDER BY id ) tHist WHERE table1.id = tHist.fk_table1 The "FROM ( SELECT * FROM table2 ORDER BY id ) tHist" is a try to force a specific order on table2 to update table1 but this isn´t working. There are some way to do this with a UPDATE statement ? Thanks in advance, -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda. * *
Re: [SQL] UPDATE in a specific order
Thanks for reply, Em 16/12/2010 17:58, Jasen Betts escreveu: I need to make update of table1 with data on table2 in the order of id of table2 that looks like EAV. is it? Err, I don´t know so much about EAV, so I think that isn´t. I´m just trying to reproduce a calc in a spreeadsheet. There are some way to do this with a UPDATE statement ? to do it with an update statement you need no more than one rows in the from for each row in the target. easiest non update statement approach is probably to use a plpgsql function with a loop. basically you need to find another way to do it. Ok! I make a function in plpgsql to do it. Thank´s again ! * *
Re: [SQL] cartesian product
Hi, Salman Maybe this isn't so much elegant, but works: SELECT p1.sequence as sequence1, p2.sequence as sequence2 FROM potential_pairs p1, potential_pairs p2 WHERE p1.sequence <= p2.sequence Hope this helps Salman Tahir wrote: Hi, I have a query regarding an SQL statement I'm trying to execute. I have the following table: sequence -+ AK AKCMK CMKA I execute the following statement (Cartesian product): SELECT p1.sequence as sequence1, p2.sequence as sequence2 FROM potential_pairs p1, potential_pairs p2 which gives me: sequence1 | sequence2 +-- AK | AK AK | AKCMK AK | CMKA AKCMK| AK AKCMK| AKCMK AKCMK| CMKA CMKA | AK CMKA | AKCMK CMKA | CMKA (9 rows) I want to eliminate duplicates and by duplicate I mean a tuple such as {AK, CMKA} should be regarded as the same as {CMKA, AK}. So I would like the following result: sequence1 | sequence2 +-- AK | AK AK | AKCMK AK | CMKA AKCMK| AKCMK AKCMK| CMKA CMKA | CMKA Any help would be appreciated. - Salman ---(end of broadcast)--- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda. ---(end of broadcast)--- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
Re: [SQL] Alternative to INTERSECT
I don't know if this is more efficient but an alternative can be something like this SELECT t.id FROM test t JOIN test t2 ON t2.id = t.id AND t2.field = 'firstname' AND t2.value LIKE 'jose%' JOIN test t3 ON t3.id = t2.id AND t3.field = 'lastname' AND t3.value LIKE 'kro%' WHERE t.field = 'firstname' AND t.value LIKE 'andrea%' Hope this helps Andreas Joseph Krogh wrote: On Tuesday 31 July 2007 18:52:22 Josh Trutwin wrote: On Tue, 31 Jul 2007 17:30:51 + Andreas Joseph Krogh <[EMAIL PROTECTED]> wrote: Hi all. I have the following schema: CREATE TABLE test ( id integer NOT NULL, field character varying NOT NULL, value character varying NOT NULL ); ALTER TABLE ONLY test ADD CONSTRAINT test_id_key UNIQUE (id, field, value); CREATE INDEX test_like_idx ON test USING btree (id, field, value varchar_pattern_ops); Using INTERSECT I want to retrieve the rows matching (pseudo-code) "firstname LIKE ('andrea%' OR 'jose%') AND lastname LIKE 'kro%'" Why not: WHERE (t.field = lastname AND t.value LIKE 'kro%') OR (t.field = firsname AND ( t.value LIKE 'jose%' OR t.value LIKE 'andrea%') ) Not tested. If you're having performance problems is probably less like that the INTERSECT is the problem with all those LIKE's in there? Is t.value indexed? Yes, as I wrote: CREATE INDEX test_like_idx ON test USING btree (id, field, value varchar_pattern_ops); And I'm observing that it uses that index. Your query doesn't cut it, let me try to explain what I'm trying to achieve: Suppose I have the following data: INSERT INTO test VALUES (1, 'firstname', 'andreas'); INSERT INTO test VALUES (1, 'firstname', 'joseph'); INSERT INTO test VALUES (1, 'lastname', 'krogh'); INSERT INTO test VALUES (2, 'firstname', 'andreas'); INSERT INTO test VALUES (2, 'lastname', 'noname'); The reason for why I use INTERSECT is that I want: SELECT t.id from test t WHERE t.field = 'firstname' AND t.value LIKE 'andrea%' INTERSECT SELECT t.id FROM test t WHERE t.field = 'firstname' AND t.value LIKE 'jose%' INTERSECT SELECT t.id FROM test t WHERE t.field = 'lastname' AND t.value LIKE 'kro%'; To return only id 1, and the query: SELECT t.id from test t WHERE t.field = 'firstname' AND t.value LIKE 'andrea%' INTERSECT SELECT t.id FROM test t WHERE t.field = 'firstname' AND t.value LIKE 'jose%' INTERSECT SELECT t.id FROM test t WHERE t.field = 'lastname' AND t.value LIKE 'non%'; To return no rows at all (cause nobydy's name is "andreas joseph noname"). Your suggestion doesn't cover this case. -- AJK ---(end of broadcast)--- TIP 2: Don't 'kill -9' the postmaster -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda. ---(end of broadcast)--- TIP 6: explain analyze is your friend
[SQL] Cast on character columns in views
Hello, I have a scenario like this: CREATE TABLE table1 ( id serial NOT NULL, col1 character varying(30), CONSTRAINT pk_table1 PRIMARY KEY (id) ); CREATE TABLE table2 ( fk_table1 integer, type1 character(3), id serial NOT NULL, CONSTRAINT pk_table2 PRIMARY KEY (id) ); CREATE TABLE table3 ( id serial NOT NULL, type2 integer, fk_table1 integer, CONSTRAINT pk_table3 PRIMARY KEY (id) ); CREATE VIEW view1 AS SELECT table1.id, table1.col1, table2.type1, NULL AS type2 FROM table1 JOIN table2 ON table2.fk_table1 = table1.id UNION ALL SELECT table1.id, table1.col1, NULL AS type1, table3.type2 FROM table1 JOIN table3 ON table3.fk_table1 = table1.id; It's all ok except by the fact that when I retrieve data from view1 the column type1 become bpchar instead of character(3) There are some manner to make the view return type1 as character(3) when I do a command like "select * from view1" ? It's strangeous because type2 return as integer. I try to do a CREATE VIEW view1 AS SELECT table1.id, table1.col1, table2.type1, NULL AS type2 FROM table1 JOIN table2 ON table2.fk_table1 = table1.id UNION ALL SELECT table1.id, table1.col1, NULL::character(3) AS type1, table3.type2 FROM table1 JOIN table3 ON table3.fk_table1 = table1.id; But i got an error: ERROR: cannot change data type of view column "type1" I try this too but got the same error: CREATE VIEW view1 AS SELECT table1.id, table1.col1, table2.type1, NULL AS type2 FROM table1 JOIN table2 ON table2.fk_table1 = table1.id UNION ALL SELECT table1.id, table1.col1, ''::character(3) AS type1, table3.type2 FROM table1 JOIN table3 ON table3.fk_table1 = table1.id; I'm doing a cast on a query on view1 like select type1::character(3) from view1 , but if exists a manner of do this without this cast it will much appreciated. TIA -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda. ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [SQL] Cast on character columns in views
Richard Broersma Jr wrote: --- "Luiz K. Matsumura" <[EMAIL PROTECTED]> wrote: CREATE VIEW view1( id, col1, type1, type2) AS SELECT table1.id, table1.col1, CAST( table2.type1 AS CHARACTER( 3 )), NULL FROM table1 JOIN table2 ON table2.fk_table1 = table1.id UNION ALL SELECT table1.id, table1.col1, CAST( NULL AS CHARACTER( 3 )), table3.type2 FROM table1 JOIN table3 ON table3.fk_table1 = table1.id; Would the above changes work? Regards, Richard Broersma Jr. Hi Richard, Your changes works ! But now I know what mistake I did: The error is occurring because I'm doing a CREATE OR REPLACE VIEW command. The command with null:character(3) works too. The error is because I create a view then try to change the definition with the CREATE OR REPLACE VIEW command When I drop the view first , and then create again the view (in a separated transaction), now the command works! (this is a bug?) I'm using a postgres 8.2.4 on Linux. Thanks a lot! -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda. ---(end of broadcast)--- TIP 5: don't forget to increase your free space map settings
Re: [SQL] Cast on character columns in views
Luiz K. Matsumura wrote: Richard Broersma Jr wrote: --- "Luiz K. Matsumura" <[EMAIL PROTECTED]> wrote: CREATE VIEW view1( id, col1, type1, type2) AS SELECT table1.id, table1.col1, CAST( table2.type1 AS CHARACTER( 3 )), NULL FROM table1 JOIN table2 ON table2.fk_table1 = table1.id UNION ALL SELECT table1.id, table1.col1, CAST( NULL AS CHARACTER( 3 )), table3.type2 FROM table1 JOIN table3 ON table3.fk_table1 = table1.id; Would the above changes work? Regards, Richard Broersma Jr. Hi Richard, Your changes works ! But now I know what mistake I did: The error is occurring because I'm doing a CREATE OR REPLACE VIEW command. The command with null:character(3) works too. The error is because I create a view then try to change the definition with the CREATE OR REPLACE VIEW command When I drop the view first , and then create again the view (in a separated transaction), now the command works! (this is a bug?) I'm using a postgres 8.2.4 on Linux. Thanks a lot! Ugh, I'm totally crazy with this views I'm using pgadmin with postgres, when I clink on "view the data of selected object" button all works fine. But when I open a query tool window and do: SELECT * FROM view1; Now, again type1 column returns as bpchar. But if I do: SELECT type1 FROM view1; Now, type1 column returns as character(3) If I do select id, col1, type1,type2 from view1; Again type1 returns as bpchar. But if I do select id, col1,type2, type1 from view1; Now type1 return as character(3). -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda. ---(end of broadcast)--- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match
[SQL] Aggregate question (Sum)
Hi All, I want to know if there are an easy manner to do an SQL like this bellow where TotalOrdersValue sum the order.total just one time per order (as count(DISTINCT order.id) do) SELECT order.dtorder , Count( DISTINCT order.fk_customer ) AS QtyCustomer , Count( DISTINCT order.id ) AS QtyOrder , Sum( order_item.qty ) AS TotalQtyItem , Sum( order.total ) AS TotalOrders FROM order JOIN order_item ON order_item.fk_order = order.id GROUP BY 1 ORDER BY 1 Ex. ORDER Id | dtorder| fk_customer | total - 1 | 2007-01-01 | 1 | 100.00 2 | 2007-01-01 | 1 | 30.00 order_item fk_order | qty | fk_product 1 |5 | A 1 |2 | B 2 |3 | C The query acctualy returns (as expected): dtorder | QtyCustomer | QtyOrder | TotalQtyItem | TotalOrders -- 2007-01-01 | 1 |2 | 10 | 230.00 But I want dtorder | QtyCustomer | QtyOrder | TotalQtyItem | TotalOrders -- 2007-01-01 | 1 |2 | 10 | 130.00 I just want to avoid to do, if possible, things like: SELECT totals.dtorder , totals.QtyCustomer , totals.QtyOrder , totals.TotalQtyItem , Sum( order.total ) AS TotalOrders FROM ( SELECT order.dtorder , Count( DISTINCT order.fk_customer ) AS QtyCustomer , Count( DISTINCT order.id ) AS QtyOrder , Sum( order_item.qty ) AS TotalQtyItem FROM order JOIN order_item ON order_item.fk_order = order.id GROUP BY 1 ) totals JOIN order ON order.dtorder = totals.dtorder GROUP BY 1,2,3,4 ORDER BY totals.dtorder I say this because it's seem a waste of effort just to sum a value that can be calculated on the same loop where postgresql will go on table order... If someone can give me some hint I will apreciate. Tanks in advance. -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda. ---(end of broadcast)--- TIP 6: explain analyze is your friend
Re: [SQL] Aggregate question (Sum)
Hi Rodrigo, thanks for reply Rodrigo De León wrote: On Nov 19, 2007 3:46 PM, Luiz K. Matsumura <[EMAIL PROTECTED]> wrote: If someone can give me some hint I will apreciate. This is more of a normalization problem. See: http://www.deeptraining.com/litwin/dbdesign/FundamentalsOfRelationalDatabaseDesign.aspx and lookup Third Normal Form. You have a calculated total for each order, so I assume there's a price for every product. You should join order_item to whichever table has the individual cost for each product and multiply it by order_item.qty. Ok, the example is a simplified scenario. In some cases we have discount on order that can't be 'distributed' on the itens. For example: Item Qty ValueUnity item_total (calculated) 15 1.01 5.05 26 1.01 6.06 The total for itens is 11.11 but we make a discount of 0.01 , then the value of order is now 11.10 This 0.01 of discount can't be aplied on any "ValueUnity" because the precision is 2 decimals. We can have too other calculations over this values (like taxes) so in my case isn't so simple to determine the final value of the order just summing the value of the itens. Anyway, thanks again... -- Luiz K. Matsumura Plan IT Tecnologia Informática Ltda. ---(end of broadcast)--- TIP 3: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faq
[SQL] query optimizer dont treat correctly OR
Hello folks See the command bellow. I use some thing simmilar about an decade on Oracle, Sybase, MSSQL, DB2, etc. But with Postgresql , he generate an FULL TABLE SCAN, and consequenyly it take about 10 minutes to run (Very big table..) -- SELECT A.SR_RECNO , A.CDEMP, A.NRPED,A.SR_RECNO FROM FTB01 A WHERE ( A.CONTROLE <= ' ' AND A.CDEMP = '75' AND A.NRPED < '261' ) OR ( A.CONTROLE = ' ' AND A.CDEMP < '75' ) OR ( A.CONTROLE < ' ' ) ORDER BY A.CDEMP DESC, A.NRPED DESC, A.SR_RECNO DESC LIMIT 170 -- Otherwise, is i write the query on the form of an little more "dummy" and eliminating the "OR" and changing by UNION, the time of execution drops to less menos of two seconds -- SELECT TMP1.* FROM ( SELECT A.SR_RECNO, A.CDEMP, A.NRPED,A.SR_RECNO FROM FTB01 A WHERE ( A.CONTROLE <= ' ' AND A.CDEMP = '75' AND A.NRPED < '261' ) ORDER BY A.CDEMP DESC, A.NRPED DESC, A.SR_RECNO DESC LIMIT 170 ) TMP1 UNION SELECT TMP2.* FROM ( SELECT A.SR_RECNO, A.CDEMP, A.NRPED,A.SR_RECNO FROM FTB01 A WHERE ( A.CONTROLE = ' ' AND A.CDEMP < '75' ) ORDER BY A.CDEMP DESC, A.NRPED DESC, A.SR_RECNO DESC LIMIT 170 ) TMP2 UNION SELECT TMP3.* FROM ( SELECT A.SR_RECNO, A.CDEMP, A.NRPED,A.SR_RECNO FROM FTB01 A WHERE OR ( A.CONTROLE < ' ' ) ORDER BY A.CDEMP DESC, A.NRPED DESC, A.SR_RECNO DESC LIMIT 170 ) TMP3 ORDER BY CDEMP DESC, NRPED DESC, SR_RECNO DESC LIMIT 170 -- The comand above works (even being 10 x slower then other Databases ) with our generate the full scan. Why Post do this wrong julgment with the initial command? Exist some thing that i can configure to to make postgres works correctly ? Obs.: * Tested on versions 7.3.2 e 7.4.1 * Obvialy the vacuumm full analyse was executed Thanks Luiz ---(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
[SQL] number os commands inside transaction block
Dear Friends how i can increse the number of commands in an transaction block i use postgres 7.4.5 on linux Regards Luiz - Original Message - From: "Stephan Szabo" <[EMAIL PROTECTED]> To: "Don Drake" <[EMAIL PROTECTED]> Cc: Sent: Monday, January 31, 2005 7:31 PM Subject: Re: [SQL] plpgsql functions and NULLs On Sun, 30 Jan 2005, Don Drake wrote: OK, I have a function that finds records that changed in a set of tables and attempts to insert them into a data warehouse. There's a large outer loop of candidate rows and I inspect them to see if the values really changed before inserting. My problem is that when I look to see if the row exists in the warehouse already, based on some IDs, it fails when an ID is NULL. The ID is nullable, so that's not a problem. But I'm forced to write an IF statement looking for the potential NULL and write 2 queries: IF omcr_id is null select * from WHERE omcr_id is NULL AND ... ELSE select * from WHERE omcr_id=candidate.omcr_id AND END IF; Hmm, perhaps some form like: WHERE not(candidate.omcr_id is distinct from omcr_id) ---(end of broadcast)--- TIP 6: Have you searched our list archives? http://archives.postgresql.org -- No virus found in this incoming message. Checked by AVG Anti-Virus. Version: 7.0.300 / Virus Database: 265.8.2 - Release Date: 28/1/2005 ---(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
Re: [SQL] number os commands inside transaction block
Hi Michael Fuhr how i can increse the number of commands in an transaction block What do you mean? What problem are you trying to solve? i´m trying to solve the follow message current transaction is aborted, queries ignored until end of transaction block some one tell me this is defined inside postgres sources i recive this message when i execute an certain number of queries inside an begin/commit block Regards Luiz ---(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
[SQL] problem with postgres
Dear friends I hope some one can help me Server machine celeron 2.4Gb with 512 Ram postgres 7.4.5 conectiva 8 with kernel 2.4.19 i´m getting many message of Erro:canceling query due to user request how to solve this i even get with an just booted up server, and running vacuum analyze verbose inside psql with no other people connected the only changes i has on postgresql.conf is max_connection=512 shared_buffers=8192 sort_mem=8192 vacuum_mem=31792 Regards Luiz Rafael ---(end of broadcast)--- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])
[SQL] problem with postgres
Dear friends I hope some one can help me Server machine celeron 2.4Gb with 512 Ram postgres 7.4.5 conectiva 8 with kernel 2.4.19 i´m getting many message of Erro:canceling query due to user request how to solve this i even get with an just booted up server, and running vacuum analyze verbose inside psql with no other people connected the only changes i has on postgresql.conf is max_connection=512 shared_buffers=8192 sort_mem=8192 vacuum_mem=31792 Regards Luiz Rafael ---(end of broadcast)--- TIP 6: Have you searched our list archives? http://archives.postgresql.org