Solved

how to cater for null

Posted on 2006-06-21
9
284 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
  • 5
  • 4
9 Comments
 
LVL 142

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
 
LVL 142

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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 142

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 142

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Creating and Managing Databases with phpMyAdmin in cPanel.
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

20 Experts available now in Live!

Get 1:1 Help Now