Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Statement for matching data to get other data

Author  Topic 

dreinhard
Starting Member

3 Posts

Posted - 2012-02-11 : 08:39:05
Hello,

Here is my dilemma:

I have the following tables with following fields:

TableA
JobNum|StepNum|StartTime
TableB
JobNum|StepNum|CustID
TableC
CustID|StepNum|IsEnabled


I need to return the following data from the three tables below.


From TableA & TableC:

JobNum|StepNum|StartTime|IsEnabled

What I need to do is find same Jobnum and StepNum from TableA and TableB. Whatever CustID in TableB for JobNum and StepNum, match that CustID to TableC's CustID and return IsEnabled value. Can someone please help with a SELECT Statement?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-11 : 08:42:30
its a straightforward join involving tables on common columns

refer the below link and make a try. if you face any issue, we will help you out

http://www.w3schools.com/sql/sql_join.asp

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-02-11 : 10:41:31
I would say the design is flawed.
What if same CustID has same StepNum in multiple JobNum?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

dreinhard
Starting Member

3 Posts

Posted - 2012-02-11 : 15:31:55
CustID and StepNum in TableC can never have duplicates:

CustID = Acme
StepNum = 3

In the example above, there can only be one Acme with stepnum 3 in TableC.

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-02-13 : 03:55:52
So Acme can never has StepNum 3 for another JobNum?
Where is the constraint for that?


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

dreinhard
Starting Member

3 Posts

Posted - 2012-02-16 : 11:05:48
Hi Visakh16,

I have tried numerous testing with joins, inner joins and multiple joins to no success. My closest results were dublicate returns. I will try to be exact with what I am trying to do:

I have 3 tables. Here are the feilds in each table that I feel are important:

JobDetail

JobNum
TimeStart
TimeEmd
tgtQName
StepNum

JobSummary

JobNum
StepNum
CustID

Application

CustID
StepNum
RunInSingleMode

I need to return 4 values from JobDetail and 1 value from Application

For JobDetail I need to return JobNum, StepNum, TimeStart and tgtQName.
For Application I need to return RunInSingleMode (True or False).

Here is my SQL Statement:

SELECT JobDetail.Jobnum, JobDetail.TimeStart, JobDetail.Stepnum, JobDetail.tgtQName, [Application].RunInSingleMode FROM JobDetail JOIN JobSummary ON JobDetail.JobNum = JobSummary.JobNum JOIN [Application] ON JobSummary.CustID = [Application].CustID JOIN JobDetail as JD2 ON RIGHT([Application].Step, 1) = JOBSUMMARY.CurrentStep WHERE JobDetail.TimeEnd is null AND JobDetail.TimeStart is not null AND JobSummary.TimeEnd is null AND JOBSUMMARY.TimeStart is not null ORDER BY JobDetail.TimeStart ASC

With this query I am having no problems getting data to return BUT I am receiving duplicates. Each one of these returns should be unique because in JobDetail you cannot have 2 of the same JobNums with the same StepNum with a null TimeEnd running at the same time. Part of the problem is that I am trying to get for each return, the value of Application.RunInSingleMode. I know the way to get those values is to relate JobDetail.Jobnum with JobSummary.Jobnum And JobDetail.StepNum with JobSummary.CurrentStep then find the value of JobSummary.CustId and JobSummary.CurrentStep and relate it to Application.CustId and Application.StepNum to get that value of Application.RunInSingleMode.

Run reason this logic returns duplicates. Any ideas?










Go to Top of Page

jacob6580
Starting Member

1 Post

Posted - 2012-03-12 : 07:39:08
CustID and StepNum in TableC can never have duplicates:

CustID = Acme
StepNum = 3

In the example above, there can only be one Acme with stepnum 3 in TableC.

-------------------------------------
[URL=http://www.marjinalescort.com/]Escort Bayan[/URL]

jacob oram
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-12 : 10:04:47
quote:
Originally posted by dreinhard

Hi Visakh16,

I have tried numerous testing with joins, inner joins and multiple joins to no success. My closest results were dublicate returns. I will try to be exact with what I am trying to do:

I have 3 tables. Here are the feilds in each table that I feel are important:

JobDetail

JobNum
TimeStart
TimeEmd
tgtQName
StepNum

JobSummary

JobNum
StepNum
CustID

Application

CustID
StepNum
RunInSingleMode

I need to return 4 values from JobDetail and 1 value from Application

For JobDetail I need to return JobNum, StepNum, TimeStart and tgtQName.
For Application I need to return RunInSingleMode (True or False).

Here is my SQL Statement:

SELECT JobDetail.Jobnum, JobDetail.TimeStart, JobDetail.Stepnum, JobDetail.tgtQName, [Application].RunInSingleMode FROM JobDetail JOIN JobSummary ON JobDetail.JobNum = JobSummary.JobNum JOIN [Application] ON JobSummary.CustID = [Application].CustID JOIN JobDetail as JD2 ON RIGHT([Application].Step, 1) = JOBSUMMARY.CurrentStep WHERE JobDetail.TimeEnd is null AND JobDetail.TimeStart is not null AND JobSummary.TimeEnd is null AND JOBSUMMARY.TimeStart is not null ORDER BY JobDetail.TimeStart ASC

With this query I am having no problems getting data to return BUT I am receiving duplicates. Each one of these returns should be unique because in JobDetail you cannot have 2 of the same JobNums with the same StepNum with a null TimeEnd running at the same time. Part of the problem is that I am trying to get for each return, the value of Application.RunInSingleMode. I know the way to get those values is to relate JobDetail.Jobnum with JobSummary.Jobnum And JobDetail.StepNum with JobSummary.CurrentStep then find the value of JobSummary.CustId and JobSummary.CurrentStep and relate it to Application.CustId and Application.StepNum to get that value of Application.RunInSingleMode.

Run reason this logic returns duplicates. Any ideas?













No idea until I see any data from table
I'm guessing it may be because tables are related as 1 to many but to confirm i need to see some data. Can you post some sample data ?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -