Solved

how to cater for null

Posted on 2006-06-21
9
290 Views
Last Modified: 2006-11-18
I have 3 tables, linked using keys:

table1 link to table2
table1 link to table3

I need to select sths out of table 2 and 3, linking by 1.

Some content in 3 may not available, I want to get null intead.

Any suggestion? How to write the sql? using join?, I tried but failed.

0
Comment
Question by:turbot_yu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16957414
you need to use LEFT JOIN

select *
from table1
inner join table2
  on table1.<field> = table2.<field>
left join table3
  on table1.<field> = table3.<field>
0
 

Author Comment

by:turbot_yu
ID: 16957451
The table 1 -> table 2 is also one -> many,

Does it need 'inner' or 'left'
0
 

Author Comment

by:turbot_yu
ID: 16957472
Table1:Source
ID
Name

Table2:Wave
ID
SourceID
Timestamp

Table3:eventLog
ID
SourceID
Timestamp
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16957474
if there will be at least one record in table2 for all rows in table1, inner join is fine. left join will work the same in that case.
0
 

Author Comment

by:turbot_yu
ID: 16957485
Table1:Source
ID
Name

Table2:Wave
ID
SourceID
Timestamp

Table3:eventLog
ID
SourceID
Timestamp
String

Table1: 1 record
Table2: 3 record
Table3: 2 record

I want result as

timestamp1 start
timestamp2 null
timestamp3 end
0
 

Author Comment

by:turbot_yu
ID: 16957552
SELECT wf.TimestampSourceLT,wf.Quantity, wf.FirstFracOfASec, wf.SamplingFrequency, wf.Samples, es.String
FROM
ION_Data.dbo.Source s
inner join  ION_Data.dbo.vWFLogChannelValue wf
on(s.name = wf.sourcename)
left outer join ION_Data.dbo.EventLog el      
on( s.id=el.sourceid )
left outer join ION_Data.dbo.EventString es      
on(el.CauseValueID=es.ID)
where
wf.Quantity='I4 Waveform'
AND wf.TimestampSourceLT>'2006-06-19 18:45:05'
OR (wf.TimestampSourceLT='2006-06-19 18:45:05'
AND left(wf.FractionofASecond,8)>0.000000)
AND (es.String='Disturbance Start' or es.String='Transient Detected')
ORDER BY wf.TimestampSourceLT, wf.FractionofASecond;
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16957598
you have to move the conditions about the left joined table into the join condition:

SELECT wf.TimestampSourceLT,wf.Quantity, wf.FirstFracOfASec, wf.SamplingFrequency, wf.Samples, es.String
FROM ION_Data.dbo.Source s
inner join  ION_Data.dbo.vWFLogChannelValue wf
  on(s.name = wf.sourcename)
left outer join ION_Data.dbo.EventLog el      
  on( s.id=el.sourceid )
left outer join ION_Data.dbo.EventString es      
  on(el.CauseValueID=es.ID)
AND (es.String='Disturbance Start' or es.String='Transient Detected')
where
wf.Quantity='I4 Waveform'
AND wf.TimestampSourceLT>'2006-06-19 18:45:05'
OR (wf.TimestampSourceLT='2006-06-19 18:45:05'
AND left(wf.FractionofASecond,8)>0.000000)
ORDER BY wf.TimestampSourceLT, wf.FractionofASecond;
0
 

Author Comment

by:turbot_yu
ID: 16965691
Thanks, now null is there, but many recorder duplicate in results.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 16967219
this is either due to
* missing join conditions
* solvable by adding "DISTINCT" or "GROUP BY" clause
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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