0

I have two tables of data for example:

Network Status

ipaddress       time       violation
192.168.1.1     2:00       yes
192.168.1.6     2:00       no
192.168.1.11    2:00       no
192.168.1.1     3:00       no
192.168.1.6     3:00       no
192.168.1.11    3:00       yes

and

Firewall Log

ipaddress       machinetype    location
192.168.1.1     computer       London
192.168.1.6     server         New York
192.168.1.11    server         Bejing

and I wish to merge the data from Firewall Log into the Network Status table so I will have the following:

Network Status

ipaddress       time       violation       machinetype       location
192.168.1.1     2:00       yes             computer          London
192.168.1.6     2:00       no              server            New York
192.168.1.11    2:00       no              server            Bejing
192.168.1.1     3:00       no              computer          London
192.168.1.6     3:00       no              server            New York
192.168.1.11    3:00       yes             server            Bejing

Does anyone have any ideas? I assumed there would be a command to merge tables by a specific column value but after research I have only come across the join and select into commands. But I can't figure out how to use these to achieve what I want.

Thanks!

1
  • UNION will not work, different column count Commented Nov 21, 2012 at 14:49

2 Answers 2

3

What do you think about a view? Combine the two tables with a select and create a view from it

CREATE VIEW myView AS SELECT log.*,status.* FROM log, status 
WHERE log.ipaddress=status.ipaddress 

/* OR */
CREATE VIEW myView AS SELECT log.*,status.* FROM log l
INNER JOIN status s ON l.ipaddress=s.ipaddress
Sign up to request clarification or add additional context in comments.

4 Comments

Please use explicit JOIN syntax instead of the implicit joins in the where clause.
you are right ... my mistake, usually I only use the join clause with numeric columns but chars will work too.
Also never use * or table_name.*. It can cause some nasty bugs and in this particular VIEW it will duplicate ipaddress column
Thanks, I opted to use the add column, update method as the create view method takes up alot of memory (I am trying to merge the firewall log containing 2.7m pieces of data with the network status containing 168m pieces of data), and the update method doesn't require new memory to be alocated for each row...It was also easier for me to understand.
1

To "merge" this tables you need:

1) Add "machinetype" and "location" columns to the "Network Status" table:

ALTER TABLE "Network Status"
  ADD COLUMN "machinetype" TEXT;
ALTER TABLE "Network Status"
  ADD COLUMN "location" TEXT;

2) Populate them with data

UPDATE "Network Status" NS
SET "machinetype" = FL."machinetype",
    "location" = FL."location"
FROM "Firewall Log" FL
WHERE FL."ipaddress" = NS."ipaddress"

Or you can create a VIEW and use it to SELECT data as if it is a table:

CREATE VIEW "Status and log" AS
SELECT NS."ipaddress", NS."time", NS."violation", FL."machinetype", FL."location"
FROM "Network Status" NS
JOIN "Firewall Log" FL ON FL."ipaddress" = NS."ipaddress"

1 Comment

Thanks, works perfectly :) see my comment above for the reason as to which I chose to use the add column/update method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.