Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cross Joins - SQL

Posted on 2014-11-17
8
Medium Priority
?
114 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Stressed Out?

Watch some penguins on the livecam!

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

715 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