I am geting this error ; SELECT list expression references column Id which is neither grouped nor aggregated at [2:3] - operators

SELECT
Id, SleepDay, TotalMinutesAsleep,TotalTimeInBed,(sum(TotalTimeInBed) - sum(TotalMinutesAsleep)) AS Sleep_Time
FROM
`bigquery-public-data-353920.Fitabase_Data.sleepDay`
ORDER BY Sleep_Time DESC

Related

Getting MAX of a column and adding one more

I'm trying to make an SQL query that returns the greatest number from a column and its respective id.
For more information I have two columns ID and NUMBER. Both of them have 2 entries and I want to get the highest number with the ID next to it. This is what I tried but didn't success.
SELECT ID, MAX(NUMBER) AS MAXNUMB
FROM TABLE1
GROUP BY ID, MAXNUMB;
The problem I'm experiencing is that it just shows ALL the entries and if I add a "where" expression it just shows the same (all entries [ids+numbers]).
Pd.: Yes, I got what I wanted but only with one column (number) if I add another column (ID) to select it "brokes".
Try:
SELECT
ID,
A_NUMBER
FROM TABLE1
WHERE A_NUMBER = (
SELECT MAX(A_NUMBER)
FROM TABLE1);
Presuming you want the IDs* of the row with the highest number (and not, instead, the highest number for each ID -- if IDs were not unique in your table, for example).
* there may be more than one ID returned if there are two or more IDs with equal maximum numbers
you can try this
Select ID,maxNumber
From
(
SELECT
ID,
(Select Max(NUMBER) from Tmp where Id = t.Id) maxNumber
FROM
Tmp t
)T1
Group By ID,maxNumber
The query you posted has an illegal column name (number) and is group by the alias for the max value, which is illegal and also doesn't make sense; and you can't include the unaliased max() within the group-by either. So it's likely you're actually doing something like:
select id, max(numb) as maxnumb
from table1
group by id;
which will give one row per ID, with the maximum numb (which is the new name I've made up for your numeric column) for each ID. Or as you said you get "ALL the entries" you might have group by id, numb, which would show all rows from the table (unless there are duplicate combinations).
To get the maximum numb and the corresponding id you could group by id only, order by descending maxnumb, and then return the first row only:
select id, max(numb) as maxnumb
from table1
group by id
order by maxnumb desc
fetch first 1 row only
If there are two ID with the same maxnumb then you would only get one of them - and which one is indeterminate unless you modify the order by - but in that case you might prefer to use first 1 row with ties to see them all.
You could achieve the same thing with a subquery and analytic function to generating a ranking, and have the outer query return the highest-ranking row(s):
select id, numb as maxnumb
from (
select id, numb, dense_rank() over (order by numb desc) as rnk
from table1
)
where rnk = 1
You could also use keep to get the same result as first 1 row only:
select max(id) keep (dense_rank last order by numb) as id, max(numb) as maxnumb
from table1
fiddle

PostgreSQL get results grouped by Name but ordered by modified_date desc for the first element of each group

Suppose that my query:
select modified_date, name from table1 where name in (select name from table2 group by name) as a order by name
return this results:
I need to change my query to get results grouped by Name but ordered by modified_date desc for the first element of each group.
Mean the result should be:
First, your subquery does not need a GROUP BY clause because you are not doing any aggregation.
It could be:
SELECT DISTINCT name FROM table2
but it works fine with a simple:
SELECT name FROM table2
For your sorting problem, if modified_date's data type is DATE you can use MAX() window function:
SELECT modified_date, name
FROM table1
WHERE name IN (SELECT name FROM table2)
ORDER BY MAX(modified_date) OVER (PARTITION BY name) DESC,
name, -- just in case 2 names have the same max modified_date
modified_date DESC;
See the demo.

Get unique value as per sorting key up to certain count (SQL)

I would like to get 5 unique value as per sorted key.
Below is the table example:-
Expected result is 5,10,3,9,1. First unique 5 value from table.
Thanks.
You can do the following for Oracle:
select * from (
select value
from table
group by value
order by min(serial)
) where rownum <=5
Try this(For MySQL):
SELECT DISTINCT value FROM <table-name> ORDER BY serial LIMIT 5;
For SQL Server
select TOP 5 value from table1
group by value
order by min(Serial)

Postgres another issue with "column must appear in the GROUP BY clause or be used in an aggregate function"

I have 2 tables in my Postgres database.
vehicles
- veh_id PK
- veh_number
positions
- position_id PK
- vehicle_id FK
- time
- latitude
- longitude
.... few more fields
I have multiple entries in Position table for every Vehicle. I would like to get all vehicle positions but the newest ones (where time field is latest). I tried query like this:
SELECT *
FROM positions
GROUP BY vehicle_id
ORDER BY time DESC
But there's an error:
column "positions.position_id" must appear in the GROUP BY clause or be used in an aggregate function
I tried to change it to:
SELECT *
FROM positions
GROUP BY vehicle_id, position_id
ORDER BY time DESC
but then it doesn't group entries.
I tried to found similiar problems e.g.:
PostgreSQL - GROUP BY clause or be used in an aggregate function
or
GroupingError: ERROR: column must appear in the GROUP BY clause or be used in an aggregate function
but I didn't really helped with my problem.
Could you help me fix my query?
Is simple if you have columns on the SELECT those should be also on the GROUP section unless they are wrapped with aggregated function
Also dont use * use the column names
SELECT col1, col2, MAX(col3), COUNT(col4), AVG(col5) -- aggregated columns
-- dont go in GROUP BY
FROM yourTable
GROUP BY col1, col2 -- all not aggregated field
Now regarding your query, looks like you want
SELECT *
FROM (
SELECT * ,
row_number() over (partition by vehicle_id order by time desc) rn
FROM positions
) t
WHERE t.rn = 1;
try to use this group by clause
GROUP BY position_id,vehicle_id
primary key then FK

I would like to select the max field in a sql column but list the rest in order as well

My sql table only has one column and it's "ID"
select MAX(id) from eve.db.dde
it only returns the 1 field (the highest)
How do i return all the fields in order from highest to lowest?
SELECT id FROM eve.db.dde ORDER BY ID DESC