Solved

SQL Syntax

Posted on 2014-01-23
5
265 Views
Last Modified: 2014-01-23
What I really want is where c.POPU is MAX

SELECT
     s.SCTYPE,
     ROUND(SUM(c.POPU / c.POUEST) * c.POEST / SUM(c.POPU), 4) 'UNIT COST'
     FROM ccode c
     INNER JOIN job j ON c.JOB_ID = j.JOB_ID
     INNER JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
     INNER JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
WHERE j.JOB_ID = 7398
AND c.POPU > 1
GROUP BY t.SCTYPE;
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
  • 3
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39804939
you could add a "simple" condition like this:
SELECT
     s.SCTYPE,
     ROUND(SUM(c.POPU / c.POUEST) * c.POEST / SUM(c.POPU), 4) 'UNIT COST'
     FROM ccode c
     INNER JOIN job j ON c.JOB_ID = j.JOB_ID
     INNER JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
     INNER JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
WHERE j.JOB_ID = 7398
AND c.POPU > 1
and c.popu = ( select max ( x.popu ) from ccode x )
GROUP BY t.SCTYPE;  

Open in new window


this new subquery might need more, especially if you want this for the relevant job


SELECT
     s.SCTYPE,
     ROUND(SUM(c.POPU / c.POUEST) * c.POEST / SUM(c.POPU), 4) 'UNIT COST'
     FROM ccode c
     INNER JOIN job j ON c.JOB_ID = j.JOB_ID
     INNER JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
     INNER JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
WHERE j.JOB_ID = 7398
AND c.POPU > 1
and c.popu = ( select max ( x.popu ) from ccode x where x.job_id = j.job_id )
GROUP BY t.SCTYPE;  

Open in new window


hope this helps
0
 

Author Comment

by:hdcowboyaz
ID: 39804997
Query : SELECT      s.SCTYPE,      ROUND(SUM(c.POPU / c.POUEST) * c.POEST / SUM(c.POPU), 4) 'UNIT COST'      FROM ccode c      INNER JOI...
Error Code : 1305
FUNCTION jds.max does not exist
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39805005
that error code is not really related to the code posted?
0
 

Author Comment

by:hdcowboyaz
ID: 39805019
Yes it is. Why does it make sense for me to post some other error?
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39805031
I think I know the issue. MySQL doens't like spaces with function/brackets
SELECT
     s.SCTYPE,
     ROUND(SUM(c.POPU / c.POUEST) * c.POEST / SUM(c.POPU), 4) 'UNIT COST'
     FROM ccode c
     INNER JOIN job j ON c.JOB_ID = j.JOB_ID
     INNER JOIN sccode s ON s.SCCODE_ID = c.SCCODE_ID
     INNER JOIN sctype t ON t.SCTYPE_ID = s.SCTYPE
WHERE j.JOB_ID = 7398
AND c.POPU > 1
and c.popu = ( select max(x.popu) from ccode x where x.job_id = j.job_id )
GROUP BY t.SCTYPE;  

Open in new window

0

Featured Post

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql query on a varchar that is numeric. 8 51
Insert values are dynamic 11 61
MySQL Query Using Up Memory 6 53
SubQuery link 4 45
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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