0

I have two tables like:

ID | TRAFFIC fd56756 | 4398 645effa | 567899 894fac6 | 611900 894fac6 | 567899 

and

USER | ID | TRAFFIC andrew | fd56756 | 0 peter | 645effa | 0 john | 894fac6 | 0 

I need to get SUM ("TRAFFIC") from first table AND set column traffic to the second table where first table ID = second table ID. ID's from first table are not unique, and can be duplicated.
How can I do this?

2
  • Use code tags next time, no need to insert HTML spaces. Code tags are where you write your question, in the toolbar at the left and are like this {}. Commented May 19, 2014 at 14:22
  • 1
    There is no column traffic in your second table. Please clarify. Also, columns and values in the second table seem to be switched. Commented May 19, 2014 at 21:09

2 Answers 2

3

Table names from your later comment. Chances are, you are reporting table and column names incorrectly.

UPDATE users u SET "TRAFFIC" = sub.sum_traffic FROM ( SELECT "ID", sum("TRAFFIC") AS sum_traffic FROM stats.traffic GROUP BY 1 ) sub WHERE u."ID" = sub."ID"; 

Aside: It's unwise to use mixed-case identifiers in Postgres. Use legal, lower-case identifiers, which do not need to be double-quoted, to make your life easier. Start by reading the manual here.

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this?

UPDATE users t2 SET t2.traffic = t1.sum_traffic FROM (SELECT sum(t1.traffic) t1.sum_traffic FROM stats.traffic t1) WHERE t1.id = t2.id; 

2 Comments

I have first table called stats.traffic with column "ID" AND "TRAFFIC" (uppercase) and second called users with "USER" , "ID" and "TRAFFIC" (uppercase). So what mean t2 and t1 ? This something like temp var ? I'm trying many times but unfortunately cannot execute query (I'm newbie, sorry) How query will looks like with my "uppercase" column names?
Modified my example to suit your case then. The t1 and t2 are aliases for the table name, just to save typing table1.xxx.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.