What's wrong with this Postgres query? - sql

: SELECT created_at, count(created_at) as count FROM "candidates" GROUP BY date(created_at)
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "candidates.created_at" must appear in the GROUP BY clause or be used in an aggregate function
created_at is a datetime column
candidates is a table
How do I make it work? Thanks.

The problem is that you are selecting created_at also as a separate column. You need to use an aggregate function (like MIN or MAX) around it or add it to the GROUP BY.
Since you are already grouping by date(created_at) you probably want to use that in the select instead. So something like this:
SELECT
DATE(created_at),
COUNT(created_at) as count
FROM "candidates"
GROUP BY date(created_at)

Related

Why am I getting an error while running the following SQL code

I am trying to query a public dataset "new_york_citibike" on google bigquery to return the num_of_bikes_available and average num_of_bikes_available for each station_id but when I am running the code I am geeting an error:
SELECT list expression references column num_bikes_available which is neither grouped nor aggregated at [2:5]
The SQL code is:
SELECT
num_bikes_available,
station_id,
AVG(num_bikes_available) AS avg_num_bikes_available
FROM
`bigquery-public-data.new_york_citibike.citibike_stations`
GROUP BY
station_id;
In BigQuery if you are using GROUP BY clasue every other column has to be either GROUPED BY or you have to do some calculation like: SUM/MIN/MAX... etc, so in this case you have to do:
SELECT
station_id,
SUM(num_bikes_available) AS sum_num_bikes_available,
AVG(num_bikes_available) AS avg_num_bikes_available
FROM
`bigquery-public-data.new_york_citibike.citibike_stations`
GROUP BY
station_id;
I also suggest you to switch station_id column to the first column because it will look much clearer this way.
Are you trying to organize your table by station_id? If so, it may be more useful to use ORDER BY rather than GROUP BY since station_id is an integer class.
SELECT
station_id,
num_bikes_available,
(SELECT
AVG(num_bikes_available)
FROM
`bigquery-public-data.new_york_citibike.citibike_stations`) AS avg_num_bikes_available
FROM
`bigquery-public-data.new_york_citibike.citibike_stations`
ORDER BY
station_id
This will show the list from lowest to highest station_id.

Group by clause not working in DB2 (sql code -119)

I want to get the amount of results for each day of the past week. Unfortunately, I got this error for the query:
An expression starting with "APP_ID" specified in a SELECT clause,
HAVING clause, or ORDER BY clause is not specified in the GROUP BY
clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause
with a column function and no GROUP BY clause is specified..
SQLCODE=-119, SQLSTATE=42803, DRIVER=3.67.27
The query:
SELECT DAYNAME(created), app_id
FROM Annotation
WHERE app_id = 1 AND (created < CURRENT DATE - 7 DAYS)
GROUP BY DAYNAME(created) ORDER BY created
The problem has something to do with the GROUP BY statement. What is wrong with it?
I think the error is pretty clear -- appid is in the SELECT but not the GROUP BY. The solution is that you need an aggregation function. I would expect something like this:
SELECT DAYNAME(created), COUNT(*)
FROM Annotation a
WHERE app_id = 1 AND (created < CURRENT DATE - 7 DAYS)
GROUP BY DAYNAME(created)
ORDER BY MIN(created);
If you want to use group by, then every column must either by in your group by statement, or aggregated.
select col1, col2, 'same-for-every-row', sum(col3) as col3_sum, avg(col4) as col4_avg
from schema.table
group by col1, col2
This works because col1 and col2 have been grouped by, but every other column has some aggregation to know how to group up all the values.
Your current statement won't work, because although you've grouped by date, you haven't specified how to group all of the rows for app_id, you need to specify that they should be grouped by summing or averaging or finding the minimum or aggregating in some other way, all of the values in that group.
The exception being a column that's created using a string, 'same-for-every-row', this won't need to be aggregated as it's the same every time.

AVG , group by, WHERE AVG greater (>) issue

this is my database
CREATE TABLE korisnici(
name VARCHAR(30) NOT NULL,
amount DECIMAL(65,2)
);
INSERT INTO korisnici VALUES
("Marina",20.10),
("Petar",300.50),
("Ivana",100.70),
("Tomislav",50.20),
("Ivana",80.60),
("Petar",10.40),
("Marina",80.50),
("Ivana",70.50),
("Marina",130.20),
("Robert",60.20),
("Blanka",130.20),
("Blanka",220.40),
("Tomislav",150.20);
I would like to fetch all names from list which has average ammount of all their amounts greater than 150. Something like I tried
SELECT name, AVG(amount) AS avg FROM `korisnici` WHERE avg > 150 GROUP BY name
However my query fails, with error "Unknown column 'avg' in 'where clause'". Can someone give me a hint.
You can't use a column alias in a WHERE, JOIN, or HAVING clause, so you need to repeat the expression, but that's not the only problem. When filtering on the result of an aggregation, the HAVING clause should be used instead of WHERE:
SELECT name, AVG(amount) AS avg
FROM `korisnici`
GROUP BY name
HAVING AVG(amount) > 150
The reason is that the WHERE clause is applied before the grouping and aggregation (and is used to determine which records get grouped and aggregated), while HAVING is applied after the aggregation.
You can not write like that: it is a common SQL error.
avg is the identifier and you can not use an identifier in the where clause..
SELECT name, AVG(amount) AS avg
FROM `korisnici`
WHERE AVG(amount) > 150 GROUP BY name;
There you go..

Tallying up similar SQL rows

I have a table that is described with two columns: an index, and a date.
How would I run a query so that: for each date, it tallies how many entries are for that date, and does it for every date that appears?
I know I can COUNT for a specific date, but I'm lost as to how to do this for each date.
(I'm using SQLite, but a description for any SQL language would be very helpful). Thanks!
select `date`, count(*)
from your_table
group by `date`
select index, date, COUNT(*) from tbl
group by index, date
check this.... let me know if it works.....
If a field is not in the "group by" list then you cannot include it unless you are performing some aggregate function on it (count, sum, etc.).
select "date_field", count(*) from "table" group by "date_field";
Also, SQLite does not have to use backticks (`) like MySQL, you can use double quotes.

pgSQL query error

i tried using this query:
"SELECT * FROM guests WHERE event_id=".$id." GROUP BY member_id;"
and I'm getting this error:
ERROR: column "guests.id" must appear in the GROUP BY clause or be used in an aggregate function
can anyone explain how i can work around this?
You can't Group By without letting the Select know what to take, and how to group.
Try
SELECT guests.member_id FROM guests WHERE event_id=".$id." GROUP BY member_id;
IF you need to get more info from this table about the guests, you'll need to add it to the Group By.
Plus, it seems like your select should actually be
SELECT guests.id FROM guests WHERE event_id=".$id." GROUP BY id;
Each of the columns used in a group by query needs to be specifically called out (ie, don't do SELECT * FROM ...), as you need to use them in some sort of aggregate function (min/max/sum/avg/count/etc) or be part of the group by clause.
For example:
SELECT instrument, detector, min(date_obs), max(date_obs)
FROM observations
WHERE observatory='SOHO'
GROUP BY instrument, detector;