STDDEV function and with clause - sql

I am using Oracle. I have table like:
Company Employee salary
A1 Jim 122000
...
I want to return the company with the highest number of employee whose salary is above 2 standard deviations (~>95%). Here is my code
With Com_2Std as (
Select company-name, AVG(salary)+2*STDDEV(salary) as AboveS
From works
Group By company-name)
Select company-name, count(employee-name) as ENumber
From works
Where ENumber=MAX(
Select count(a.employee-name)
From works a, Com_2Std b
Where a.company-name=b.company-name
And a.salary>b.AboveS;
Group by a.company-name)
Group by company-name;
I have two quesions:
(1) I can't access to oracle today and can't test it. Is my code correct please?
(2) It looks quite complicated, any way to simplify it please?

with Com_2Std as (
select company-name, AVG(salary)+2*STDDEV(salary) as AboveS
from works
group by company-name
),
CompanyCount as (
select a.company-name, count(*) as CountAboveS
from
works a
inner join Com_2Std b on a.company-name=b.company-name
where
a.salary > b.AboveS
group by a.company-name
)
select company-name
from CompanyCount
where CountAboveS = (select max(CountAboveS) from CompanyCount)
This ought to be close. It will produce ties as well.

Related

How do I select all id's that are present in one table but not in another

I am trying to get a list of department Ids are present in one table, (PS_Y_FORM_HIRE), but which don't exist in another table (PS_DEPARTMENT_VW).
Here is the basics of what I have which isn't working:
SELECT h.DEPTID FROM PS_Y_FORM_HIRE h, PS_DEPARTMENT_VW d WHERE NOT EXISTS (
SELECT d1.DEPTID FROM PS_DEPARTMENT_VW d1 WHERE d1.DEPTID = h.DEPTID
and d1.SETID_GL_DEPT = 'IDBYU'
);
I'm trying to form this query in SQL Developer, but it just returns a long list of blanks (after spinning/running the query for a very long time).
In addition, I need this to be effective dated, so that it only grabs the correct effective-dated row, but I was unsure how and where to incorporate this into the query.
EDIT I neglected to mention that only the department table is effective dated. The form hire table is not. I need to get the current effectively dated row from that in this query (to make sure the data is accurate).
Also note that DEPTID isn't a key on PS_Y_FORM_HIRE, but is on PS_DEPARTMENT_VW. (Along with SETID_GL_DEPT and EFFDT).
So again, ideally, I will have a list of all the department ids that appear in PS_Y_FORM_HIRE, but which are not in PS_DEPARTMENT_VW.
SELECT DEPTID
FROM PS_Y_FORM_HIRE
MINUS
SELECT DEPTID
FROM PS_DEPARTMENT_VW
WHERE SETID_GL_DEPT = 'IDBYU';
or
SELECT DEPTID
FROM PS_Y_FORM_HIRE
WHERE DEPTID NOT IN (
SELECT DEPTID
FROM PS_DEPARTMENT_VW
WHERE SETID_GL_DEPT = 'IDBYU'
)
or
SELECT DEPTID
FROM PS_Y_FORM_HIRE h
WHERE NOT EXISTS (
SELECT 1
FROM PS_DEPARTMENT_VW d
WHERE SETID_GL_DEPT = 'IDBYU'
AND d.DEPTID = h.DEPTID
)
This seems like a job for the MINUS operation. Something like
select deptid from ps_y_form_hire where eff_date = <whatever>
minus
select deptid from ps_department_vw <where eff_date = ...>
You didn't provide information to determine what exactly you want done with the effective dates; adapt as needed.
SELECT h.DEPTID
FROM PS_Y_FORM_HIRE h
WHERE h.DEPTID NOT IN (SELECT p.DEPTID
FROM PS_DEPARTMENT_VW p
WHERE p.SETID_GL_DEPT = 'IDBYU')
Your question is a bit unclear around why you want effective dated rows as you are not checking effective status or any other field that may have changed between effective rows. If your question is, You want to know all DEPTIDs in PS_Y_FORM_HIRE that either don't exist or are inactive as of a current effective date, then the SQL below should help
SELECT DEPTID
FROM PS_Y_FORM_HIRE h
WHERE
H.DEPTID NOT IN ( SELECT d.DEPTID
FROM PS_DEPARTMENT_VW d
WHERE d.EFF_STATUS = 'A'
AND d.EFFDT = (SELECT MAX(EFFDT)
FROM PS_DEPARTMENT_VW d2
WHERE d2.SETID_GL_DEPT = d.SETID_GL_DEPT
AND d2.DEPTID = d.DEPTID
AND d2.EFFDT <= CURRENT_DATE)
)

SQL Nested Select -Subquery returned more than 1 value-

I have a table Sales with columns SalesID, SalesName, SalesCity, SalesState.
I am trying to come up with a query that only shows salesName where there is one SalesName per SalesCity. So for example, if SaleA is in Houston and SaleB is in Houston, SaleA and SaleB will not be returned.
select
SalesName, SalesCity, SalesState
from
Sales
where
(select count(*) from Sales group by SalesCity) = 1;
I am not entirely sure how to link the inner select back out. I need another column in the nested select to identify the SalesID. I am currently stuck and have made no progress.
You can get the names of cities that have only 1 sale by using GROUP BY and HAVING operators. Then use these results in your where clause:
SELECT SalesName, SalesCity, SalesState
FROM Sales WHERE SalesCity IN
(
SELECT SalesCity
FROM Sales
GROUP BY SalesCity
HAVING COUNT(SalesCity) = 1
)
You can do this without a subquery:
select MIN(SalesName) as SalesName, SalesCity, MIN(SalesState) as SalesState
from Sales
group by SalesCity
having count(*) = 1;
If there is only one row for the city, then the min() will return the value on that row.

SQL nested aggregate functions MAX(COUNT(*))

I'm trying to select max(count of rows).
Here is my 2 variants of SELECT
SELECT MAX(COUNT_OF_ENROLEES_BY_SPEC) FROM
(SELECT D.SPECCODE, COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
FROM DECLARER D
GROUP BY D.SPECCODE
);
SELECT S.NAME, MAX(D.ENROLEECODE)
FROM SPECIALIZATION S
CROSS JOIN DECLARER D WHERE S.SPECCODE = D.SPECCODE
GROUP BY S.NAME
HAVING MAX(D.ENROLEECODE) =
( SELECT MAX(COUNT_OF_ENROLEES_BY_SPEC) FROM
( SELECT D.SPECCODE, COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
FROM DECLARER D
GROUP BY D.SPECCODE
)
);
The first one is working OK, but I want to rewrite it using "HAVING" like in my second variant and add there one more column. But now 2nd variant don't output any data in results, just empty columns.
How can I fix it ? Thank YOU!)
This query based on description given in comments and some suggestions, so it may be wrong:
select -- 4. Join selected codes with specializations
S.Name,
selected_codes.spec_code,
selected_codes.count_of_enrolees_by_spec
from
specialization S,
(
select -- 3. Filter records with maximum popularity only
spec_code,
count_of_enrolees_by_spec
from (
select -- 2. Count maximum popularity in separate column
spec_code,
count_of_enrolees_by_spec,
max(count_of_enrolees_by_spec) over (partition by null) max_count
from (
SELECT -- 1. Get list of declarations and count popularity
D.SPECCODE AS SPEC_CODE,
COUNT(D.ENROLEECODE) AS COUNT_OF_ENROLEES_BY_SPEC
FROM DECLARER D
GROUP BY D.SPECCODE
)
)
where count_of_enrolees_by_spec = max_count
)
selected_codes
where
S.SPECCODE = selected_codes.spec_code
Also query not tested and some syntax errors are possible.

How do I get the top 10 results of a query?

I have a postgresql query like this:
with r as (
select
1 as reason_type_id,
rarreason as reason_id,
count(*) over() count_all
from
workorderlines
where
rarreason != 0
and finalinsdate >= '2012-12-01'
)
select
r.reason_id,
rt.desc,
count(r.reason_id) as num,
round((count(r.reason_id)::float / (select count(*) as total from r) * 100.0)::numeric, 2) as pct
from r
left outer join
rtreasons as rt
on
r.reason_id = rt.rtreason
and r.reason_type_id = rt.rtreasontype
group by
r.reason_id,
rt.desc
order by r.reason_id asc
This returns a table of results with 4 columns: the reason id, the description associated with that reason id, the number of entries having that reason id, and the percent of the total that number represents.
This table looks like this:
What I would like to do is only display the top 10 results based off the total number of entries having a reason id. However, whatever is leftover, I would like to compile into another row with a description called "Other". How would I do this?
with r2 as (
...everything before the select list...
dense_rank() over(order by pct) cause_rank
...the rest of your query...
)
select * from r2 where cause_rank < 11
union
select
NULL as reason_id,
'Other' as desc,
sum(r2.num) over() as num,
sum(r2.pct) over() as pct,
11 as cause_rank
from r2
where cause_rank >= 11
As said above Limit and for the skipping and getting the rest use offset... Try This Site
Not sure about Postgre but SELECT TOP 10... should do the trick if you sort correctly
However about the second part: You might use a Right Join for this. Join the TOP 10 Result with the whole table data and use only the records not appearing on the left side. If you calculate the sum of those you should get your "Sum of the rest" result.
I assume that vw_my_top_10 is the view showing you the top 10 records. vw_all_records shows all records (including the top 10).
Like this:
SELECT SUM(a_field)
FROM vw_my_top_10
RIGHT JOIN vw_all_records
ON (vw_my_top_10.Key = vw_all_records.Key)
WHERE vw_my_top_10.Key IS NULL

Kinda complex inner join and sum query

I am stuck with a query and would appreciate if you guys could help me.
We are using a table (MS Access) to store project plans for multiple companies as follows:
ID PROJECT_NAME COMPANY_NAME MILESTONE_NAME MILESTONE_TYPE
The objective of my query is to return the count of "applicable" rollouts.
A rollout is the unique combination of project and company. Example:
Project 1 being executed in the Company A
Project 2 being executed in the Company A
Project 1 being executed in the Company B
An applicable project is whatever project which it last milestone has a MILESTONE_TYPE different than "DUMMY".
An applicable rollout should look like this:
ID PROJECT_NAME COMPANY_NAME MILESTONE_NAME MILESTONE_TYPE
1 Project 3 Company D 6.2 Rollout Completed Applicable
In order to retrieve the last milestone of the project I am using the following SQL:
SELECT Sheet1.[PROJECT_NAME], MAX(Sheet1.[MILESTONE_NAME])
FROM Sheet1
INNER JOIN Sheet1 AS Sheet1_1 ON (Sheet1.[PROJECT_NAME] = Sheet1_1.[PROJECT_NAME])
AND (Sheet1.[MILESTONE_NAME] = Sheet1_1.[MILESTONE_NAME])
GROUP BY Sheet1.[PROJECT_NAME]
ORDER BY Sheet1.[PROJECT_NAME]
The expected result of the query I need your support would be:
PROJECT_NAME COUNT_OF_APPLICABLE_ROLLOUTS
Project A 5
Project B 6
However I have absolutely no idea how to even get started...
Does anyone know how to put in all in a SQL query?
Thanks in advance.
Assuming that bigger "id" means later, the following query returns information about the latest non-dummy row:
select p.*
from Sheet1.p join
(select project_name, company_name, max(id) as maxid
from Sheet1
where Milestone_Name <> 'Dummy'
group by project_name, company_name
) pc
on p.project_name, pc.company_name
(Note: I haven't tested this so it might have syntax errors.)
You can try this:
SELECT myTable.*
FROM (
SELECT DISTINCT COMPANY_NAME
FROM Sheet1
) CN
CROSS APPLY
(
SELECT TOP 1 *
FROM Sheet1 myTable2
WHERE myTable2.COMPANY_NAME = CN.COMPANY_NAME
ORDER BY
MILESTONE_NAME DESC
) myTable