Solved

SQL Syntax

Posted on 2013-10-30
4
342 Views
Last Modified: 2013-10-30
This query works great but is limited to one job (i.e., JOB_ID = 7398).  I wish to open up the query so the first column JOB_ID displays all jobs.  However, if I remove WHERE j.job_id = 7398 in query a & b I get a cartesian product.

SELECT a.job_id,
       a.sctype,
       a.esthours,
       b.poest,
       (a.esthours * b.poest) AS total
  FROM (  SELECT j.job_id, s.sctype, SUM(c.popu) / (SUM(c.popu) / SUM(c.popu / c.pouest)) AS esthours
            FROM ccode c
                 INNER JOIN sccode s ON s.sccode_id = c.sccode_id
                 INNER JOIN job j ON c.job_id = j.job_id
           WHERE j.job_id = 7398
             AND s.sctype IN (1,
                              2,
                              3,
                              4,
                              5,
                              6,
                              7)
             AND c.popu > 0
             AND (j.deleted != 'Y' OR j.deleted IS NULL)
             AND (c.deleted != 'Y' OR c.deleted IS NULL)
             AND (s.deleted != 'Y' OR s.deleted IS NULL)
        GROUP BY j.job_id, s.sctype) a
       INNER JOIN (SELECT x.sctype, x.poest
                     FROM (  SELECT s.sctype, MAX(c.popu) popu, c.poest
                               FROM ccode c
                                    JOIN sccode s ON s.sccode_id = c.sccode_id
                                    JOIN job j ON j.job_id = c.job_id
                              WHERE j.job_id = 7398
                                AND s.sctype IN (1,
                                                 2,
                                                 3,
                                                 4,
                                                 5,
                                                 6,
                                                 7)
                                AND c.popu > 0
                           GROUP BY j.job_id, s.sctype) x) b
           ON a.sctype = b.sctype;
0
Comment
Question by:hdcowboyaz
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39612623
Add Job_ID as a column in the second query and then use the job_ID from both queries in the ON clause.
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 39612636
To put a better way, change:

        GROUP BY j.job_id, s.sctype) a
       INNER JOIN (SELECT x.sctype, x.poest
                   FROM (  SELECT s.sctype, MAX(c.popu) popu, c.poest
To:

        GROUP BY j.job_id, s.sctype) a
       INNER JOIN (SELECT x.sctype, x.poest, j.job_ID
                   FROM (  SELECT s.sctype, MAX(c.popu) popu, c.poest, j.job_ID
and modify the last row from:

ON a.sctype = b.sctype;

To:

ON a.sctype = b.sctype  AND a.job_ID = b.job_ID.
0
 

Author Comment

by:hdcowboyaz
ID: 39612911
I used this..

SELECT
  a.job_id 'JOB ID',
  a.sctype 'TYPE',
  a.units 'UNITS',  
  FORMAT(a.esthours, 2) 'EstHours',
  b.poest 'RATE',
  FORMAT((a.esthours * b.poest), 2) 'EstCost',
  FORMAT((a.esthours * b.poest) / a.units, 4) 'UnitCost'
FROM
(SELECT
  j.JOB_ID,
  s.SCTYPE,
  SUM(c.POPU) AS UNITS,
  SUM(c.POPU)/(SUM(c.POPU)/SUM(c.POPU/c.POUEST)) AS EstHours
FROM ccode c
INNER JOIN sccode s ON s.sccode_id = c.sccode_id
INNER JOIN job j ON c.job_id = j.job_id
WHERE s.sctype IN (1,2,3,4,5,6,7)
AND c.popu > 0
AND (j.deleted != 'Y' OR j.deleted IS NULL)
AND (c.deleted != 'Y' OR c.deleted IS NULL)
AND (s.deleted != 'Y' OR s.deleted IS NULL)
GROUP BY j.job_id, s.sctype) a
INNER JOIN (SELECT x.sctype, x.poest, j.job_ID
FROM (SELECT s.sctype, MAX(c.popu) popu, c.poest, j.job_ID
FROM ccode c
JOIN sccode s ON s.sccode_id = c.sccode_id
JOIN job j ON j.job_id = c.job_id
WHERE s.sctype IN (1,2,3,4,5,6,7)
AND c.popu > 0
GROUP BY j.job_id, s.sctype) X) b
ON a.sctype = b.sctype  AND a.job_ID = b.job_ID;

I got this error...
Query : SELECT    a.job_id 'JOB ID',    a.sctype 'TYPE',   a.units 'UNITS',     format(a.esthours, 2) 'EstHours',    b.poest 'RATE',   f...
Error Code : 1054
Unknown column 'j.job_ID' in 'field list'
0
 

Author Closing Comment

by:hdcowboyaz
ID: 39613053
Only one small error

>    GROUP BY j.job_id, s.sctype) a
>    INNER JOIN (SELECT X.sctype, X.poest, X.job_ID
>    FROM (  SELECT s.sctype, MAX(c.popu) popu, c.poest, j.job_ID


There, in the 2nd line, X.job_ID
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Optimizing a query 3 38
How to base a filter depending on fields contents? 15 57
What logic to build in order to get a weekly reminder 9 51
SQL Server syntax question 13 30
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. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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