Query Oracle SqlDeveloper - oracle-sqldeveloper

I want to write a SELECT statement to create a list of jobs containing the job_title and the mean value of the two fields MIN_SALARY and MAX_SALARY sorted from the highest mean value to the lowest.
I wrote this:
select job_title, MAX_SALARY, MIN_SALARY, count(*)
from HR.EMPLOYEES, HR.JOBS
order by count(*) desc
group by j.job_title;
but it gives error:
ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended"

I think that you should have this query
select job_title, MAX_SALARY, MIN_SALARY, count(*)
from HR.EMPLOYEES, HR.JOBS
group by job_title
order by count(*) desc
I have reordered the clauses and removed the spurious j
See - select

Related

In PostgresSQL how to write a query returning multiple rows and columns

My original query was like this:
SELECT *,
(SELECT COUNT(*), user_id FROM user_sessions WHERE company_id=companies.id GROUP BY user_id) as user_sessions
FROM companies
Which comes back with this error:
error: more than one row returned by a subquery used as an expression
I found a way past that error with this:
SELECT *,
ARRAY (SELECT COUNT(*), user_id FROM user_sessions WHERE company_id=companies.id GROUP BY user_id) as user_sessions
FROM companies
But then it has this error:
error: subquery must return only one column
If I remove either COUNT(*) or user_id from the returned columns it works, however I need both sets of data. How do I return more than one column in a sub-query like this?
I guess a join should do the trick:
select * from
companies
join
( select count(*), company_id, user_id
from user_sessions
group by (company_id, user_id)
) as user_sessions
on companies.id = company_id
For anyone who runs into this in the future, tested the best way to dot this is what #Matt mentioned in the comments above:
Replace COUNT(*), user_id with ARRAY[COUNT(*), user_id]
Works perfectly

Subquery count() for postgres query

I have two tables with web traffic that I am joining and visualizing on a map.
I am trying to write a counter that creates a query result with a count of the number of times a particular IP address shows up in the logs. I think this will take the form of a subquery that returns the count of rows of the specific row the main query is selecting.
When I run the following query, the error I get is more than one row returned by a subquery used as an expression.
select
squarespace_ip_addresses.ip,
squarespace_ip_addresses.latitude,
squarespace_ip_addresses.longitude,
st_SetSrid(ST_MAKEPOINT(squarespace_ip_addresses.longitude, squarespace_ip_addresses.latitude), 4326) as geom,
(select count(hostname) from squarespace_logs group by hostname) as counter,
squarespace_logs.referrer
from
squarespace_ip_addresses
left outer join
squarespace_logs
on
squarespace_ip_addresses.ip = squarespace_logs.hostname
Something that has been suggested to me is a subquery in a select that filters by the main query output runs that query for every row.
Does anyone have any insights?
Aggregate the data in a derived table (a subquery in FROM clause):
select
a.ip,
a.latitude,
a.longitude,
st_SetSrid(ST_MAKEPOINT(a.longitude, a.latitude), 4326) as geom,
c.count,
l.referrer
from squarespace_ip_addresses a
left join squarespace_logs l on a.ip = l.hostname
left join (
select hostname, count(*)
from squarespace_logs
group by hostname
) c on a.ip = c.hostname

selecting a distinct column with alias table not working in postgres

SELECT pl_id,
distinct ON (store.store_ID),
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID;
while running this query in postgres server i am getting the below error..How to use the distinct clause..in above code plan 1 is the schema name.
ERROR: syntax error at or near "distinct" LINE 2: distinct ON
(store.store_ID),
You are missing an order by where the first set of rows should be the ones specified in the distinct on clause. Also, the distinct on clause should be at start of the selection list.
Try this:
SELECT distinct ON (store_ID) store.store_ID, pl_id,
in_user_id
FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store
ON copy1.PLAN_ID = store .PLAN_ID
order by store_ID, pl_id;

Getting group by attribute in nested query

I am trying to find the most frequent value in a postgresql table. The problem is that I also want to "group by" in that table and only get the most frequent from the values that have the same name.
So I have the following query:
select name,
(SELECT value FROM table where name=name GROUP BY value ORDER BY COUNT(*) DESC limit 1)
as mfq from table group by name;
So, I am using where name=name, trying to get the outside group by attribute "name", but it doesn't seem to work. Any ideas on how to do it?
Edit: for example in the following table:
name value
a 3
a 3
a 3
b 2
b 2
I want to get:
name value
a 3
b 2
but the above statement gives:
name value
a 3
b 3
instead, since where doesn't work correctly.
There is a dedicated function in PostgreSQL for this case: the mode() ordered-set aggregate:
select name, mode() within group (order by value) mode_value
from table
group by name;
which returns the most frequent input value (arbitrarily choosing the first one if there are multiple equally-frequent results) -- which is the same behavior as with your order by count(*) desc limit 1.
It is available from PostgreSQL 9.4+.
http://rextester.com/GHGJH15037
If you want your query to work, you need table aliases. Table aliases and qualified column names are always a good idea:
select t.name,
(select t2.value
from table t2
where t2.name = t.name
group by t2.value
order by COUNT(*) desc
limit 1
) as mfq
from table t
group by t.name;

T-SQL Count returns ALL records Instead of Subset

trying to figure out this dumb syntax issue
select distinct Count(Mgr) from CarsManager
returns all records..should be a subset count.
Select Mgr, Count(*)
From CarsManager
Group By Mgr
You did not specify on what the subset count should be made. Given your example, I assumed it was the Mgr column.
If what you seek is a count of unique managers, then you can do:
Select Count(Distinct Mgr)
From CarsManager
Difference between Count(*) and Count(SomeColumn)
In comments, you asked about the difference between Count(*) and Count(SomeCol). The difference isn't in performance but logic. Count(*) counts rows regardless of column. Count(SomeCol) counts non-null values in SomeCol.
COUNT (Transact-SQL)
COUNT(ALL expression) evaluates expression for each row in a group and returns the number of nonnull values.
In this case of Count(SomeCol), ALL is implied.
You can't do this all in one go - you need a subquery:
SELECT count(*) FROM (SELECT DISTINCT Mgr FROM CarsManager) as tbl1