Solved

SQL Join Query question

Posted on 2011-09-12
7
257 Views
Last Modified: 2012-05-12
Hi,

I have the below query that works on one column, then creates a table from the result that gets.

I need a query that checks 3 columns then creates the table from the result.

I need to check for any change for the columns [ps].ASE, [ps].Env, [ps].City

Thank you,
RD11
SELECT [ps].ETag, [ps].Host, [ps].[Private Input], [ps].[From ci db], [ps].[ID-PK], [ps].Src, [ps].ASE, [ps].Env, [ps].EnvD, [ps].Cls [ps].City, [ps].[Surpluss EST], [ps].[Monthly Cost], [ps].Description, [ps].[Install Date], [ps].[Purchase Date], [ps].Env, [ps].Pool, [ps].Billed, [ps].Price, [ps].[C Org], [ps].[Resp], [ps].Function,[ps].Comments, [ps].[Last Update], [ps].OS, [ps].Status INTO Diff_table
FROM [ps] LEFT JOIN cI_table ON [ps].Host = cI_table.HOST
WHERE (((cI_table.HOST) Is Null));

Open in new window

0
Comment
Question by:RD11
  • 4
  • 3
7 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 36526526

Checks the three columns exactly for what?

If they are different in the diff table, you can use exists
SELECT [ps].ETag, [ps].Host, [ps].[Private Input], [ps].[From ci db], [ps].[ID-PK], [ps].Src, [ps].ASE, [ps].Env, [ps].EnvD, [ps].Cls [ps].City, [ps].[Surpluss EST], [ps].[Monthly Cost], [ps].Description, [ps].[Install Date], [ps].[Purchase Date], [ps].Env, [ps].Pool, [ps].Billed, [ps].Price, [ps].[C Org], [ps].[Resp], [ps].Function,[ps].Comments, [ps].[Last Update], [ps].OS, [ps].Status 
INTO Diff_table
FROM [ps] 
LEFT JOIN cI_table ON [ps].Host = cI_table.HOST
where not exists (select 1 from Diff_table B
                  where [ps].ASE = B.ASE and [ps].Env = B.Env and [ps].City = B.City)
and (((cI_table.HOST) Is Null));

Open in new window

0
 

Author Comment

by:RD11
ID: 36526530
If any change in either any one of them.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 36526541

This line will get the rows that are not in the diff table based on the three columns

where not exists (select 1 from Diff_table B where [ps].ASE = B.ASE and [ps].Env = B.Env and [ps].City = B.City)
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 36526548
You will actually need  a primary key for all this to work properly, so assuming the primary key is ETag

where not exists (select 1 from Diff_table B where [ps].ETag = B.ETag and [ps].ASE = B.ASE and [ps].Env = B.Env and [ps].City = B.City)
0
 

Author Comment

by:RD11
ID: 36526633
I actually DO NOT have a primary Key, I am using the Host name as a PK.  Can I still do that?
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 36526859

That should work
0
 

Author Closing Comment

by:RD11
ID: 36532913
Thank you
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question