SQL joining multiple tables together with multiple parameters

I am a management engineer and have only minimal knowledge of SQL, but took some reports from AB FactoryTalk, SQL Express 2012 and Report Builder 3.0. The data comes from the PLC and writes the data to three tables.

  • Floating table
  • Row table
  • Tags table

The tag table contains the tag name and "TagIndex" for reference. The Float table contains all numeric data and has a "TagIndex" with a number that matches the tag name in the tags table. The same concept with a String table contains strings and has a "TagIndex" which matches the tag name in the tags table.

I am creating a table that displays the entire data log snapshot which includes some data from the Float table and the row table. Both are related to each other in that their date / time matches are the same.
I need to link the tag table to the String and Float tables, and also link the string table to the Float table using the registered date.

using Query Builder in Report Builder I have a semi-working table with the following

SELECT
    StringTable.DateAndTime AS [StringTable DateAndTime]
    ,FloatTable.DateAndTime AS [FloatTable DateAndTime]
    ,FloatTable.TagIndex AS [FloatTable TagIndex]
    ,FloatTable.Val AS [FloatTable Val]
    ,StringTable.Val AS [StringTable Val]
    ,TagTable.TagIndex AS [TagTable TagIndex]
    ,TagTable.TagName
    ,TagTable.StartEnd
    ,TagTable.TableOrder
    ,TagTable.Label
    ,TagTable.Unit
    ,StringTable.TagIndex AS [StringTable TagIndex]
FROM TagTable
    FULL OUTER JOIN FloatTable
      ON TagTable.TagIndex = FloatTable.TagIndex
    FULL OUTER JOIN StringTable
      ON TagTable.TagIndex = StringTable.TagIndex

      

^ The code works, but without the relation of FloatTable and StringTable. also replacing the last two lines with the following and was a valid request, but didn't give what I needed:

FULL OUTER JOIN StringTable
  ON TagTable.TagIndex = StringTable.TagIndex
  AND FloatTable.DateAndTime = StringTable.DateAndTime

      

Data is used in a table with columns as TagIndex / TagNames and strings are used as DateAndTime.

How do I assign DateAndTime correctly?

+3


source to share


1 answer


A full outer join is indeed a good idea here, but only for two child tables.



SELECT
     s.DateAndTime AS [StringTable DateAndTime]
    ,f.DateAndTime AS [FloatTable DateAndTime]
    ,f.TagIndex AS [FloatTable TagIndex]
    ,f.Val AS [FloatTable Val]
    ,s.Val AS [StringTable Val]
    ,t.TagIndex AS [TagTable TagIndex]
    ,t.TagName
    ,t.StartEnd
    ,t.TableOrder
    ,t.Label
    ,t.Unit
    ,s.TagIndex AS [StringTable TagIndex]
FROM FloatTable f
FULL OUTER JOIN StringTable s ON s.TagIndex = f.TagIndex AND s.DateAndTime = f.DateAndTime
INNER JOIN TagTable t ON t.TagIndex IN (s.TagIndex, f.TagIndex);

      

0


source







All Articles