Solved

Cross Joins - SQL

Posted on 2014-11-17
8
109 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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
 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

805 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