Solved

Combine Two Datasets

Posted on 2014-12-10
3
89 Views
Last Modified: 2015-01-19
Dear all,

I'm trying to combine two datasets, tables LocationData and MonitoredData based on matching values in field2 and field3.  All field types are string.  Sorry for the names I decided to make it simple to explain it.

Does the below query look like it will work?  Its not returning any results.

SELECT [LocationData].Field1, [LocationData].Field4, MonitoredData.Field2, MonitoredData.Field3 INTO MappingTable
FROM [LocationData] INNER JOIN MonitoredData ON [LocationData].Field3= MonitoredData.Field3
WHERE ((([LocationData].Field1) Is Not Null) AND ((MonitoredData.Field2)=[LocationData].Field2) AND ((MonitoredData.Field3)=[LocationData].Field3));

Open in new window


Thanks
0
Comment
Question by:AndyC1000
3 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40493274
It could be reduced to:

SELECT
    [LocationData].Field1,
    [LocationData].Field4,
    MonitoredData.Field2,
    MonitoredData.Field3
INTO
    MappingTable
FROM
    [LocationData],
    MonitoredData
WHERE
    [LocationData].Field1 Is Not Null
    AND
    MonitoredData.Field2 = [LocationData].Field2
    AND
    MonitoredData.Field3 = [LocationData].Field3;

/gustav
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 40494080
Its not returning any results.

of course it will not... it is inserting into some table...

remove into statement...

SELECT l.Field1, l.Field4, m.Field2, m.Field3
FROM [LocationData] l 
INNER JOIN MonitoredData m ON l.Field3= m.Field3 AND m.Field2=l.Field2 AND l.Field1 Is Not Null

Open in new window

0
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
ID: 40494096
I would change the join to include both fields.

SELECT [LocationData].Field1, [LocationData].Field4, MonitoredData.Field2, MonitoredData.Field3 INTO MappingTable
FROM [LocationData] INNER JOIN MonitoredData ON [LocationData].Field3 = MonitoredData.Field3 AND MonitoredData.Field2 =[LocationData].Field2
WHERE [LocationData].Field1 Is Not Null;

But the suggested change is for efficiency.  It shouldn't change the result.  If the query is not returning any rows, it is because there are none that match.

To debug, I would back out one condition at a time.  So start by removing the where clause.  If that doesn't work, remove the second field in the join.  If that doesn't work, put the second field back and remove the first.

You should also examine the data.  It is possible that one table has trailing blanks or something in the columns and that will prevent the join from working.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now