Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

how to cater for null

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
turbot_yu
Asked:
turbot_yu
  • 5
  • 4
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
turbot_yuAuthor Commented:
The table 1 -> table 2 is also one -> many,

Does it need 'inner' or 'left'
0
 
turbot_yuAuthor Commented:
Table1:Source
ID
Name

Table2:Wave
ID
SourceID
Timestamp

Table3:eventLog
ID
SourceID
Timestamp
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
turbot_yuAuthor Commented:
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
 
turbot_yuAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
turbot_yuAuthor Commented:
Thanks, now null is there, but many recorder duplicate in results.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this is either due to
* missing join conditions
* solvable by adding "DISTINCT" or "GROUP BY" clause
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now