[SQL] Fetch the latest log for each product
Hi, I bet people have asked this question several times, but oh well, please do anwser again. Thanks. =) I have a product table and a log file. product_tb --- prod_id prod_name ... log_tb - log_id prod_id cust_id transact_date ... How do I fetch the latest log for each product according to transaction date? - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Group by within table joins
Hi, I have the following SQL: SELECT ltb.v_id, count(ltb.v_id) AS num_of_times_borrowed, vtb.equip_attr[1] AS year, vtb.equip_attr[3] AS model, vtb.equip_attr[4] AS type FROM log_tb ltb, vehicle_tb vtb WHERE ltb.v_id=vtb.equip_id GROUP BY ltb.v_id ORDER BY year; "ERROR: Attribute vtb.equip_attr must be GROUPed or used in an aggregate function" but, it didn't work. I want to know how many time each vehicle has been borrowed. Please help. Thanks. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Division
Hi, I tried the write up the following SQL statement: result1 from sql1 = (1,2,3,4,5) result2 from sql2 = (4,5) result3 = result1/result2 (or is it result2/result1) = (1,2,3) What do I have to do to get the (1,2,3) result? I tried "query EXCEPT query", but it seems like EXCEPT needs both tables in queries to be of the same number of columns and column types. And (any, all, exist) doesn't seem to be a right solution. Thanks for help. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] nested transactions
Hi, Just out of curiousity, does Postgres support nested transactions? - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Big Sub-select statement
Hi, Let's say if I have a log table with (log_id, emp_id, book_id, author_id), and there are foreign tables related to each id except the log_id. Is it possible to write up a big sub-select statement to retrieve all possible info? eg1; (big sub_select) select ltb.log_id, ltb.emp_id, ltb.book_id, ltb.author_id from log_tb ltb where ltb.emp_id in (select etb.name etb.phone etb.email from emp_tb etb where etb.emp_id=ltb.emp_id) and ltb.book_id in (select btb.name ... ) and ... [same for author_id]; So, is it possible and efficient to retrieve all info in one big statement? or is there other better ways to do it? Right now, here's what I do, select log_id, emp_id, ... from log_tb; while(not end of rows) { select name, phone, email from emp_tb where emp_id=log_tb.emp_id; ... [same for book_tb and author_tb] } I am thinking if it's in one big statement, then I don't need to look around in the script to change the select statements. Thank you. =) - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] a script that queries database periodically
Hello, My boss wants me to write a script that will send a email report to him monthly with the result of database queries. For example, an email would be like: Monthly report +--+-+---+ | Vehicles | Mileage | # of Signouts | +--+-+---+ | Vehicle1 | 10324 | 5 | +--+-+---+ | Vehicle2 | 19384 | 6 | +--+-+---+ ... I was thinking of writing up a PHP script and put into crontab, which is somehow easier than a shell script, but PHP is an apache module, so I cannot execute PHP under crontab (it has to be executed in a browser right?). I guess a shell script is necessary. So, is it possible to call 'psql' and returning its query result and I can use sendmail to email the result? Any other idea? Thanks. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Re: a script that queries db periodically
Thank you for all your response. Not only do I benefit from your response, but hopefully others benefit as well. Thanks again. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] template or index of some kind
Hi, I have a situation where there are hundreds of inventories that needs to be stored into postgres. And I am thinking of doing a template to ease the track of those inventories. Idea 1: each inventory may be an individual table, and the problem is how can I keep track of those tables? Idea 2: since all inventories have the same attribute, we can have a template for the entire inventory, and every time each item is retrieved, the info is filled into template. (This sounds like having a table for 'item'.) Please give me some feedback. Thank you. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] constraint
Hi, I am a newbie to SQL. Could someone please explain why do I need a contraint, and how do I use it in SQL? An example is needed. Thanks - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] storing images!
Hi, Does PostgreSQL support storing image pointers or something alike. Basically, I want to retrieve relative images together with info. Thanks. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Problem with array
Hi, Instead of using select car_name from car_table where car_property[1]='1995'; (*) Can I use where car_property['year']='1995'; in Postgres?? I want to use a more descriptive name than just using index for the array (not just during insertion but as well as to specify it in table creation!) Any idea? Thanks. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Please ignore [PSQL and PHP]
Hello, Never mind the last email subjected "PSQL and PHP". I found the bug. In PHP script, instead of using insert into employee values ( 'Bernie', '{$phone, $fax, ...}' ); or insert into employee values ( 'Bernie', '{"$phone", "$fax", ...}' ); or any other possible combo, I should have used insert into employee values ( 'Bernie', '{\"$phone\", \"$fax\", ...}' ); Damn it! That makes me mad! Sorry about this. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Fetch an element in an array
Hi, This is more of a PHP problem, but I wish if someone knows this can help me. How do I fetch an element in a Postgres array? I have a table like employee col1 int4 col2 text[] now I used the php api $row = pg_fetch_array($exec, 0); which gives me the following: echo $row['col2']; (prints out ===> {"123-4567", "Bernie", "123-5670"} ) (echo $row['col2[2]']; <=== didn't work) What can I do to extract only the "Bernie" element? Thanks. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] SQL 'Case When...'
Hi, Don't know if it's possible, but how do I make the following SQL statments right? table --- name int4 attribute text[] select attribute[1] from table case when attribute[1]='yes' then select attribute[2] from table else select attribute[3] from table; Basically, I want to compare attr[1], if true then select some fields from table; if false then select others. Thank you. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Problem regarding 'select...as...'
Hi, I have a following query: "select equip_attr[1], equip_attr[2] as year, make from vehicle_tb;" which gives me the following error: "ERROR: Attribute 'make' not found" Is there something wrong with my "as" usage? Thanks - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] inheritance and primary key
Hi, Is primary key similar to 'not null columns with indices'? The reason I am asking because I found that when you inherit table B from table A, the primary key in A won't be inherited to B, but only the columns. I was wondering how do you inherit a primary key or, even, a foreign key? If it's not possible, what are the ways going around it? (not null index or...???) Thanks for your help! - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Sub_select problem
Good day, everyone, I have 2 tables and 1 datafile as following: vehicle_tb --- v_idint4 primary key display_idint4 yeartext maketext modeltext ... vehicle_borrow_log employee_idint4 (foreign key to emp_tb) v_idint4 (foreign key to vehicle_tb) borrow_timetimestamp return_timetimestamp ... datafile.gnu ( a text file) --- # DateDisplay_id 2000-XX-XX1 2000-xx-xx1 2000-XX-XX2 2000-xx-xx2 2000-XX-XX1 2000-xx-xx1 ... What I want to do is to fetch the display_id in datafile using PHP (by the way, this part is done). And then use that display_id to get the borrow_time and return time in vehicle_borrow_log. So, I used this sub-select SQL, but it doesn't seem to be working. SELECT v_id, borrow_time, return_time FROM vehicle_borrow_log vbl WHERE (SELECT equip_id FROM vehicle_tb vtb WHERE vtb.v_id=vbl.v_id AND display_id=$id_from_datafile_let_say_it_is_2); Could anyone please help? Thanks! - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Timestamp problem
Hi, I have Postgres 7.0, and I created a table table - ... borrow timestamp return timestamp Now, I thought timestamp is in sec eg; 35434434, but in PostgreSQL, it shows up as datetime datatype eg; 2000-06-07 17:00:05-07. I was wondering is this format a correct one for timestamp, or is it a bug? Abd if I want to show the time, each retrieval I have to chop of the '-07' timezone at the end. Is there a way to avoid this being show up in Postgres? Thanks. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] update some elements in the array
Hello, Could someone please tell me that is there anyway that I can update only a few elements in a Postgres array. eg; I have an array of text, declared as 'text[]'. {"1","2","3","4","5"} ==> {"1","2","7","8","9"} I want to update the last three element without affecting other elements. I could have done it by retrieving all elements in the array first and the do a UPDATE table SET arr='{"1","2","3","4","5"}' WHERE blahblah however, that might be troublesome, so I tried UPDATE table SET arr[1]=val1 arr[2]=val2... but it didn't work. If anyone knows, please tell me. Thanks for the help. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
Re: [SQL] update some elements in the array
Jouni Ahto wrote: > On Thu, 13 Jul 2000, Bernie Huang wrote: > > > I want to update the last three element without affecting other > > elements. I could have done it by retrieving all elements in the array > > first and the do a > > > > UPDATE table > > SET arr='{"1","2","3","4","5"}' > > WHERE blahblah > > > > however, that might be troublesome, so I tried > > > > UPDATE table > > SET arr[1]=val1 > > arr[2]=val2... > > > > but it didn't work. If anyone knows, please tell me. Thanks for the > > help. > > What does "it didn't work" in this case exactly mean? > > Did some testing with postgres, results; > > create table test (arr text[]); > insert into test values ('{"1","2","3","4","5"}'); > select * from test; > arr > --- > {"1","2","3","4","5"} > (1 row) > update test set arr[4]='8', arr[2]='9'; > select * from test; > arr > --- > {"1","9","3","8","5"} > (1 row) > > So, updating arrays definitely works. Are you having a syntax error > somewhere or what? > > -- Jouni Ahto Thanks. I forgot the quote...' ' (Man, that quote just drives me nuts...) - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Order by in Select
Hello, I have a list of id's that I need to sort and fetch. eg; 1,3,6,5,4,3,4,5,6,7,5,4,2 Select id >From table Order By id Asc; it becomes like: 1,2,3,3,4,4,4,5,5,5,6,6,7 What can I add to the above sql statement so that is fetches the specified id first in a sorted list? eg: if $specified_id=5; I want: 5,5,5,1,2,3,3,4,4,4,6,6,7 I could have done it in two different sql statements where one fetches specified_id and the other fetches and sorts others, but I want to be able to do it in one sql statement to reduce time. Any ideas? Thank you! - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Select by priority again
Sorry about this, but I don't quite get previous postings regarding select by prioriry. I have the same question with my sql. I want to be able to select cell_phone if there is one in the table; otherwise, I will select work_phone. table -- id, name, address, cell_phone, work_phone,... and I tried using "case when...end" but nope, it's not gonna work. Help is needed. Thanks. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
Re: [SQL] Select by priority again
"D'Arcy J.M. Cain" wrote: > [Note: PHP mailing list removed from Cc as this is strictly a > PostgreSQL question I think.] > > Thus spake Bernie Huang > > Sorry about this, but I don't quite get previous postings regarding > > select by prioriry. > > > > I have the same question with my sql. I want to be able to select > > cell_phone if there is one in the table; otherwise, I will select > > work_phone. > > > > table > > -- > > id, name, address, cell_phone, work_phone,... > > > > and I tried using "case when...end" but nope, it's not gonna work. Help > > is needed. Thanks. > > Exactly what was the SQL statement and which version of PostgreSQL? I > do this in a view and it works. > > SELECT CASE WHEN login IS NULL THEN uid::text ELSE login END AS login > FROM account; > > -- > D'Arcy J.M. Cain| Democracy is three wolves > http://www.druid.net/darcy/| and a sheep voting on > +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. hi, thanks for the help. I will avoid to CC to the wrong list. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] Change attribute of a column
Hi, I have a field in text[] and I want to change it to a datetime[]. Is it possible? how? Thanks very much. - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
[SQL] BLOBs
Hi, everyone, Browsing through the online manual, I didn't find anything related to BLOBs, but I know there must be BLOBs since many people are asking about it on the list and there is a pg_fetch_object() in PHP for Postgres. Could anyone points me an URL where I can look it up? Thanks. -Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard
Re: [SQL] Changing user passwords
Rob van der Leek wrote: > A question about the ALTER USER command I couldn't find in the > standard documentation: > > - How do I assign an 'undefined' password? > > ALTER USER nobody WITH PASSWORD ""; > > works, but doesn't assign an undefined password to nobody (like the > `pg_passwd nobody` command would do). > > DROP USER nobody; CREATE USER nobody; > > doesn't do the job either. > > There is no space between: eg; createuser whoever; dropuser whoever; - Bernie begin:vcard n:Huang;Bernie tel;fax:(604)664-9195 tel;work:(604)664-9172 x-mozilla-html:TRUE org:Environment Canada;Standards and Technology Services adr:;;700-1200 West 73 Ave.;Vancouver;BC;V6P 6H9;Canada version:2.1 email;internet:[EMAIL PROTECTED] title:Programmer x-mozilla-cpt:;0 fn:Bernie Huang end:vcard