I'm having the following error with this query in SQL server 2014 "Operand data type varchar is invalid for sum operator."
SUM (DISTINCT (studentsip.AdminNO)) AS NoOfStudentsAllocated
If you want to count the number of students (as suggested by the column name), then use COUNT(), not SUM():
COUNT(DISTINCT studentsip.AdminNO) AS NoOfStudentsAllocated
I have a certain amount of experience with SQL. I have never used SUM(DISTINCT). I wish the language did not allow the syntax.
I should note that if the DISTINCT is not needed, then you should not use it. DISTINCT almost always slows down queries.
Your field is of type varchar. To use it in sum() you need to convert() it to int:
sum(distinct(convert(int,studentsip.AdminNO))) as NoOfStudentsAllocated
If you want the number of students that have each value of AdminNo, you can use count and group by:
select AdminNO, count(1) as NoOfStudentsAllocated
from studentsip
group by AdminNO
order by AdminNO
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...
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..
I'm using oracle sql developer and I can't get this query to function. It's telling me its not a single group function. Please help.
SELECT LGBRAND.BRAND_NAME, LGPRODUCT.PROD_DESCRIPT,
COUNT (LGPRODUCT.PROD_DESCRIPT) AS "NUMPRODUCTS"
FROM LGBRAND, LGPRODUCT
ORDER BY LGBRAND.BRAND_NAME;
What I'm trying to accomplish is to get the total different products grouped by each brand name.
when using aggregate functions you need to use group by clause
All aggregate functions like avg, count,sum needs to be used along with a group by function. If you dont use a group by clause, you are performing the function on all the rows of the table.
SELECT LGBRAND.BRAND_NAME,
LGPRODUCT.PROD_DESCRIPT,
COUNT (LGPRODUCT.PROD_DESCRIPT) AS "NUMPRODUCTS"
FROM LGBRAND, LGPRODUCT,
GROUP BY LGBRAND.BRAND;
You need to use GROUP BY clause.
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.
I have a column of count and want to divide the column by max of this column to get the rate.
I tried
select t.count/max(t.count)
from table t
group by t.count
but failed.
I also tried the one without GROUP BY, still failed.
Order the count desc and pick the first one as dividend didn't work in my case. Consider I have different counts for product subcategory. For each product category, I want to divide the count of subcategory by the max of count in that category. I can't think of a way avoiding aggregate func.
If you want the MAX() per category you need a correlated subquery:
select t.count*1.0/(SELECT max(t.count)
FROM table a
WHERE t.category = a.category)
from table t
Or you need to PARTITION BY your MAX()
select t.count/(max(t.count) over (PARTITION BY category))
from table t
group by t.count
The following works in all dialects of SQL:
select t.count/(select max(t.count) from t)
from table t
group by t.count;
Note that some versions of SQL do integer division, so the result will be either 0 or 1. You can fix this by multiplying by 1.0 or casting to a float.
Most versions of SQL also support:
select t.count/(max(t.count) over ())
from table t
group by t.count;
The same caveat applies about integer division.
You might want to try using a subquery to derive the max value (including both in the same query might not work the way that you are expecting, since you are grouping on the same column that you are aggregating)
Select t.count / (select max(sub.count) from table sub)
from table t
group by t.count