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
Solved

Cross Joins - SQL

Posted on 2014-11-17
8
110 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on t-sql 2012 10 56
SQL Server Count where two id types exist in column 8 29
SubQuery link 4 35
Syntax Issue with SSIS module 26 103
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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