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.
Related
I keep getting an error saying this is not a group by expression when i run this statement in oracle XE.
SELECT PROJECTID, HOURSWORKED FROM ASSIGNMENT GROUP BY PROJECTID HAVING HOURSWORKED > 20;
GROUP BY is used for an aggregating expression, such as MAX() or SUM().
You can either write
SELECT PROJECTID, HOURSWORKED
FROM ASSIGNMENT
WHERE HOURSWORKED > 20;
or
SELECT PROJECTID, SUM(HOURSWORKED)
FROM ASSIGNMENT
GROUP BY PROJECTID
HAVING SUM(HOURSWORKED) > 20;
(or some other aggregate function, eg: MAX, MIN) depending on your intent.
WHERE is used to filter individual records
HAVING is used to filter the grouped aggregate values.
You have to have an aggregate function on hoursworked - e.g. MAX(HOURSWORKED), AVERAGE(HOURSWORKED) etc. Or you also have to add it to the group by, though that would seem strange..
One other suggestion for asking SQL questions on stackoverflow - create a minimal test case using SQL Fiddle. E.g. http://sqlfiddle.com/#!17/fea91/5 for your example. Unless it's something very Oracle specific, Postgres is usually close enough for ordinary SQL questions. Try changing the WHERE to a HAVING, to see the difference between the two...
: 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)
Is it possible in PostgreSQL to select one column from a table within a particular date span (there is a date column) and - here is the catch! - add the table together. Like for making a sales report?
Based on your comment, I think you are referring to SUM(). This is an aggregate function
SELECT SUM(amount)
FROM sales_orders
WHERE date BETWEEN '2011-03-06' and '2011-04-06' -- not sure what your date is.
If I understand you correctly, you are looking for this:
SELECT sum(amount)
FROM sales_orders
WHERE date ...
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;
Given a command in SQL;
SELECT ...
FROM ...
GROUP BY ...
Can I group by something that isn't in the SELECT line?
Yes.
This is often used in the superaggregate queries like this:
SELECT AVG(cnt)
FROM (
SELECT COUNT(*) AS cnt
FROM sales
GROUP BY
product
HAVING COUNT(*) > 10
) q
, which aggregate the aggregates.
Yes of course e.g.
select
count(*)
from
some_table_with_updated_column
group by
trunc(updated, 'MM.YYYY')
Yes you can do it, but if you do that you won't be able to tell which result is for which group.
As a result, you almost always want to return the columns you've grouped by in the select clause. But you don't have to.
Yes, you can. Example:
select count(1)
from sales
group by salesman_id
What you can't do, of course, if having something on your select clause (other than aggregate functions) that are not part of the group by clause.
Hmm, I think the question should have been in the other way round like,
Can I SELECT something that is not there in the GROUP BY?
It's alright to write a code like:
SELECT customerId, count(orderId) FROM orders
GROUP BY customerId, orderedOn
If you want to find out the number of orders done by a customer datewise.
But you cannot do it the other way round:
SELECT customerId, orderedOn count(orderId) FROM orders
GROUP BY customerId
You can issue an aggregate function on the column that is not there in the group by. But you cannot give it in the select line without the aggregate function. As it will not make much sense. Like for the above query. You group by just customerId for order counts and you want the date also to be printed in the output??!! You don't involve the date factor in the group for counting then will it mean something to have a date in it?
I don't know about other DBMS' but DB2/z, for one, does this just fine. It's not required to have the column in the select portion but, of course, it does have to extract the data from the table in order to aggregate so you're probably not saving any time by leaving it off. You should only select the columns that you need, aggregation of the data is a separate task from that.
I'm pretty certain the SQL standard allows this (although that's only based on the knowledge that the mainframe DB2 product follows it pretty closely).