clientid employeeid
customers <---- jobcards ------> employees
| handedovertoid
__________________________|_______________________ | | |
jobcarddetails-->employees jobcardlabor-->employees jobcardparts-->parts
hi Guys,
above is the structure of the transaction tables:
jobcards, jobcarddetails,jobcardlabor,jobcardparts
where
customer,employees and parts are the master reference tables.
I want to retrieve the records from these tables in a single recordset, so i avoid roundtrips, the sql query is below. Problem with this query is that i want it to return the maximum rows from each of the child table, so i can iterate and fill through a single recordset. Problem area is the employee table which i referred by jobcards, jobcarddetails and jobcardlabor.
example : if 2 records in jobcarddetails then it return 2 rows with duplicating record information for jobcard master and other tables. but if there are 2 records in jobcardlabor then i want it to return 2 records in all with jobcard details and jobcardlabor having different record data.
I hope my requirment is clear. now if some1 can solve this query or just suggest an alternative.
SELECT JobCards.JobCardID, JobCards.CustomerID,
JobCards.EmployeeID, JobCards.PersonReporting,
employees.firstname + ' ' + employees.lastname as reportTakenbyName,
JobCards.NatureOfProblem, JobCards.HandedToID,
e1.firstname + ' ' + e1.lastname as HandedToName,
JobCards.InvoiceNumber, JobCards.DateReceived,
JobCards.DateRequired, JobCards.MakeAndModel,
JobCards.SerialNumber, JobCards.SoftwareProblem,
JobCards.DateFinished, JobCards.DatePickedUp,
JobCards.SalesTaxRate, JobCards.JobSite,
JobCards.Accessories, Customers.CompanyName,
JobCardDetails.Description, jobCardDetails.ActionTaken,
JobCardDetails.DateTakenUp,
JobCardDetails.DateFinished AS detailsDtFinished,
JobCardLabor.EmployeeID AS EmployeeLabourID,
JobCardLabor.BillableHours, JobCardLabor.BillingRate,
JobCardLabor.Comment, JobCardLabor.JobCardLaborID,
e3.firstname + ' ' + e3.lastname as JobCardLaborName,
JobCardParts.UnitPrice, JobCardParts.Quantity,
Parts.PartName, JobCardParts.JobCardPartID,
JobCardDetails.AttendedBy,
e2.firstname + ' ' + e2.lastname as AttendedByName
FROM jobcards, jobcarddetails, jobcardlabor, jobcardparts, employees, employees e1,
employees e2, employees e3,customers, parts
where jobcards.jobcardid = jobcarddetails.jobcardid and
jobcards.jobcardid = jobcardlabor.jobcardid and
jobcards.jobcardid = jobcardparts.jobcardid and
jobcards.employeeid = employees.employeeid and
jobcards.handedtoid = e1.employeeid and
jobcarddetails.attendedby = e2.employeeid and
jobcardlabor.employeeid = e3.employeeid and
jobcards.customerid = customers.customerid and
jobcardparts.partid = parts.partidPost DDL, test data and desired resultset to speed up the process.|||hi,
have attached the ddl script, test data in text document(csv) per table and desired result in a jpg file as well(which i generated using msdatashape)
hope it helps.
thanks.|||Try this FROM clause
FROM JobCards
LEFT JOIN JobCardDetails ON JobCards.JobCardID = JobCardDetails.JobCardID
LEFT JOIN JobCardLabor ON JobCards.JobCardID = JobCardLabor.JobCardID
LEFT JOIN JobCardParts ON JobCards.JobCardID = JobCardParts.JobCardID
LEFT JOIN Employees ON JobCards.EmployeeID = Employees.EmployeeID
LEFT JOIN Employees e1 ON JobCards.HandedToID = e1.EmployeeID
FULL JOIN Employees e2 ON JobCardDetails.AttendedBy = e2.EmployeeID
FULL JOIN Employees e3 ON JobCardLabor.EmployeeID = e3.EmployeeID
LEFT JOIN customers ON JobCards.CustomerID = customers.customerid
FULL JOIN Parts ON JobCardParts.PartID = Parts.PartID
I am not sure what outer joins are necessary. You can optimize.|||Doesnt quite give me the desired result.
if u use dataenvironment and create a Hierarchial recordset then it will show u the results which i m looking for.
coz if u add a single record in lets say parts then it gives 3 records more: 1 for jobcarddetails and two for jobcardlabor.
the full outer joins r not serving any purpose as i tried those as well.|||MSSQLOLEDB data provider cannot return a hieraset.|||yes i knw abt msoledb provider, thats the reason y this problem.
but no solution yet except that i have to use that hierach recordset.
Showing posts with label customers. Show all posts
Showing posts with label customers. Show all posts
Wednesday, March 7, 2012
Saturday, February 25, 2012
Complex Join, I think.
I need a little help and I hope I can write a clear description of the problem. I have to tables, customers and custrate. The customer table simply has a custid of other misc info. The custrate contains an entry for each rate for any particular customer (1 customer to many rates). However the rates are suppossed to come in pairs. For example, if a customer has a rate id of 120 then the customer should also have a corresponding custrate record with a rate_id of 200. If the customer has a rate_id of 130, then the customer should have a corresponding custrate record containing rate_id 300, and so on and so forth. How do I find the customer id with are missing the corresponding rate? Here's an example or what I'm trying to explain.
customer
id | name
1 | fred
2 | tom
3 | eric
4 | fred
custrate
cust_id | rate_id
1 | 120
1 | 200
2 | 130
2 | 300
3 | 120
4 | 130
I looking for the sql statement that would return customer id of 3 and 4, since those are the only 2 records that don't have a corresponding rate. I have hunch that it requires a intra table join, but maybe i'm totally wrong. Can anyone help me please? I would greatly appreciate it!select c.name
from customer as c
inner
join custrate as cr1
on cr1.cust_id = c.id
and cr1.rate_id in (120,130)
inner
join custrate as cr2
on cr2.cust_id = c.id
and cr2.rate_id =
case when cr1.rate_id = 120
then 200
case when cr1.rate_id = 130
then 300
end
where cr2.cust_id is null|||select * from customer c
where not exists
(select 1 from custrate r
where c.id=r.cust_id
group by r.cust_id
having count(*)%2=0 -- Must be paired
)
PS. There is something amiss with Rudy's query|||PS. There is something amiss with Rudy's queryindeed there was, 2 things amiss
try it now --select c.name
from customer as c
inner
join custrate as cr1
on cr1.cust_id = c.id
and cr1.rate_id in (120,130)
left outer
join custrate as cr2
on cr2.cust_id = c.id
and cr2.rate_id =
case when cr1.rate_id = 120
then 200
when cr1.rate_id = 130
then 300
end
where cr2.cust_id is null:)|||4:30?
As in AM?
you just getting in?|||Rudy has no need for sleep.
Sleep is inefficient.
:p|||sorry for the late reply, was out all day
yeah, i normally get up around 4:00 or 4:30 am
go to bed when it gets dark
:)|||sorry for the late reply, was out all day
yeah, i normally get up around 4:00 or 4:30 am
go to bed when it gets dark
:)
You must have a heck of a time during the summer when it doesn't get dark until after 10:00 PM ... or is it about 10:30 in your area?|||indeed, sometimes i get sleepy well before it gets dark!
customer
id | name
1 | fred
2 | tom
3 | eric
4 | fred
custrate
cust_id | rate_id
1 | 120
1 | 200
2 | 130
2 | 300
3 | 120
4 | 130
I looking for the sql statement that would return customer id of 3 and 4, since those are the only 2 records that don't have a corresponding rate. I have hunch that it requires a intra table join, but maybe i'm totally wrong. Can anyone help me please? I would greatly appreciate it!select c.name
from customer as c
inner
join custrate as cr1
on cr1.cust_id = c.id
and cr1.rate_id in (120,130)
inner
join custrate as cr2
on cr2.cust_id = c.id
and cr2.rate_id =
case when cr1.rate_id = 120
then 200
case when cr1.rate_id = 130
then 300
end
where cr2.cust_id is null|||select * from customer c
where not exists
(select 1 from custrate r
where c.id=r.cust_id
group by r.cust_id
having count(*)%2=0 -- Must be paired
)
PS. There is something amiss with Rudy's query|||PS. There is something amiss with Rudy's queryindeed there was, 2 things amiss
try it now --select c.name
from customer as c
inner
join custrate as cr1
on cr1.cust_id = c.id
and cr1.rate_id in (120,130)
left outer
join custrate as cr2
on cr2.cust_id = c.id
and cr2.rate_id =
case when cr1.rate_id = 120
then 200
when cr1.rate_id = 130
then 300
end
where cr2.cust_id is null:)|||4:30?
As in AM?
you just getting in?|||Rudy has no need for sleep.
Sleep is inefficient.
:p|||sorry for the late reply, was out all day
yeah, i normally get up around 4:00 or 4:30 am
go to bed when it gets dark
:)|||sorry for the late reply, was out all day
yeah, i normally get up around 4:00 or 4:30 am
go to bed when it gets dark
:)
You must have a heck of a time during the summer when it doesn't get dark until after 10:00 PM ... or is it about 10:30 in your area?|||indeed, sometimes i get sleepy well before it gets dark!
Subscribe to:
Posts (Atom)