Solved

how to cater for null

Posted on 2006-06-21
9
289 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

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)

Question has a verified solution.

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

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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