Solved

Combine Two Datasets

Posted on 2014-12-10
3
90 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

862 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