Solved

Cross Joins - SQL

Posted on 2014-11-17
8
105 Views
Last Modified: 2014-11-18
This query works and returns 5 columns of data. The last column is the hours worked by an employee for the date (l.DATE_WORK = '2014-11-03') in the query. I want the next four days to be added to the query. That is Tuesday, Wednesday, Thursday, Friday. Would this be done with a join?

SELECT
j.JOBID 'Job',
j.DSCR 'Job Name',
e.EMPID 'Emp ID',
CONCAT(e.EFIRST, ' ', e.EMIDDLE, ' ', e.ELAST) 'Name',
l.HOURS 'Monday'
FROM ccode c
JOIN labor l ON (l.CCODE_ID = c.CCODE_ID)
JOIN job j ON (l.JOB_ID = j.JOB_ID)
JOIN emp e ON (e.EMP_ID = l.EMP_ID)
WHERE l.DATE_WORK = '2014-11-03'
AND l.DELETED = 'N'
GROUP BY e.EMP_ID
ORDER BY j.JobID;
0
Comment
Question by:hdcowboyaz
  • 4
  • 2
  • 2
8 Comments
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 40448655
You can select a date range like this:
WHERE l.DATE_WORK >= '2014-11-03' AND I.DATE_WORK <= '2014-11-07'
0
 

Author Comment

by:hdcowboyaz
ID: 40448676
that still returned one Date column.
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 40448723
Just posted the response below over at http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_28564096.html#a40448643

Cross joins are expensive. You might be better off running separate queries for each day, if your requirements allow.

I don't know if this will improve performance and I didn't test it, but you could try the following. Let me know if you have questions about the query.

SELECT 
j.JOBID 'Job', 
j.DSCR 'Job Name', 
ex.ExtraID 'Extra',
c.CostCode 'Cost Code',
e.EMPID 'Emp ID',
CONCAT(e.EFIRST, ' ', e.EMIDDLE, ' ', e.ELAST) 'Name', 
IsNull(select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-03' group by l.HOURS, 0) 'Monday',
IsNull(select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-04' group by l.HOURS, 0) 'Tuesday',
IsNull(select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-05' group by l.HOURS, 0) 'Wednesday',
IsNull(select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-06' group by l.HOURS, 0) 'Thursday',
IsNull(select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-07' group by l.HOURS, 0) 'Friday'
FROM ccode c
JOIN job j ON (l.JOB_ID = j.JOB_ID)
JOIN emp e ON (e.EMP_ID = l.EMP_ID)
LEFT JOIN extra ex ON (ex.JOB_ID = j.JOB_ID AND ex.EXTRA_ID = c.EXTRA_ID)
GROUP BY e.EMP_ID
ORDER BY j.JobID;

Open in new window

0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 40448730
I'm sorry I just realized you are looking to have 5 columns with hours for each 5 working days, is that correct?

You could do a subquery for each day of the week.
SELECT
j.JOBID 'Job',
j.DSCR 'Job Name',
e.EMPID 'Emp ID',
CONCAT(e.EFIRST, ' ', e.EMIDDLE, ' ', e.ELAST) 'Name',
(SELECT I1.HOURS FROM ccode c1 JOIN labor l1 ON (l1.CCODE_ID=c.CCODE_ID) WHERE l1.DATE_WORK = '2014-11-03' AND l1.DELETED = 'N')  'Monday',
(SELECT I2.HOURS FROM ccode c2 JOIN labor l2 ON (l2.CCODE_ID=c.CCODE_ID) WHERE l2.DATE_WORK = '2014-11-04' AND l2.DELETED = 'N')  'Tuesday'
(SELECT I3.HOURS FROM ccode c3 JOIN labor l3 ON (l3.CCODE_ID=c.CCODE_ID) WHERE l3.DATE_WORK = '2014-11-05' AND l3.DELETED = 'N'))  'Wednesday',
(SELECT I4.HOURS FROM ccode c4 JOIN labor l4 ON (l4.CCODE_ID=c.CCODE_ID) WHERE l4.DATE_WORK = '2014-11-06' AND l4.DELETED = 'N'))  'Thursday',
(SELECT I5.HOURS FROM ccode c5 JOIN labor l5 ON (l5.CCODE_ID=c.CCODE_ID) WHERE l5.DATE_WORK = '2014-11-07' AND l5.DELETED = 'N'))  'Friday'
FROM ccode c
JOIN labor l ON (l.CCODE_ID = c.CCODE_ID)
JOIN job j ON (l.JOB_ID = j.JOB_ID)
JOIN emp e ON (e.EMP_ID = l.EMP_ID)
GROUP BY e.EMP_ID
ORDER BY j.JobID;

Without knowing your database schema, you may need to change the joins and/or grouping in the subqueries.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:hdcowboyaz
ID: 40448741
Here is the ERD
ERDTheEdgeLaborEntry.jpg
0
 

Author Comment

by:hdcowboyaz
ID: 40448742
Yes, the last five columns will be Hours for each day of the week.
0
 

Author Comment

by:hdcowboyaz
ID: 40448796
1 queries executed, 0 success, 1 errors, 0 warnings

Query: SELECT j.JOBID 'Job', j.DSCR 'Job Name', e.EMPID 'Emp ID', CONCAT(e.EFIRST, ' ', e.EMIDDLE, ' ', e.ELAST) 'Name', (SELECT I1.HOU...

Error Code: 1054
Unknown column 'I1.HOURS' in 'field list'

I don't understand what 'I1.HOURS' represents?
0
 
LVL 14

Accepted Solution

by:
quizwedge earned 500 total points
ID: 40450241
Unknown column 'I1.HOURS' in 'field list' probably means that you're putting single quotes around l1.HOURS

l1 is the alias for the table name since you have it in your query more than once. As in

 JOIN labor l1

Open in new window


You can also try the query below

SELECT 
j.JOBID 'Job', 
j.DSCR 'Job Name', 
ex.ExtraID 'Extra',
c.CostCode 'Cost Code',
e.EMPID 'Emp ID',
CONCAT(e.EFIRST, ' ', e.EMIDDLE, ' ', e.ELAST) 'Name', 
IsNull((select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-03' group by l.HOURS), 0) 'Monday',
IsNull((select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-04' group by l.HOURS), 0) 'Tuesday',
IsNull((select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-05' group by l.HOURS), 0) 'Wednesday',
IsNull((select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-06' group by l.HOURS), 0) 'Thursday',
IsNull((select sum(l.HOURS) from labor l where l.C=CCODE_ID = cCCODE_ID and l.EMP_ID = e.EMP_ID and l.DELETED = 'N' and DATE_WORK = '2014-11-07' group by l.HOURS), 0) 'Friday'
FROM ccode c
JOIN job j ON (l.JOB_ID = j.JOB_ID)
JOIN emp e ON (e.EMP_ID = l.EMP_ID)
LEFT JOIN extra ex ON (ex.JOB_ID = j.JOB_ID AND ex.EXTRA_ID = c.EXTRA_ID)
GROUP BY e.EMP_ID
ORDER BY j.JobID;

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

708 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

15 Experts available now in Live!

Get 1:1 Help Now