Solved

Cross Joins - SQL

Posted on 2014-11-17
8
113 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Part One of the two-part Q&A series with MalwareTech.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

623 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