using count and group by correctly - sql

I have a relation CandyC(id, email, age, name, candy_id)
I want to count the CandyC.ids associated with a CandyC.candy_id once.
Attempt:
SELECT email, age, name
FROM CandyC
GROUP BY id
HAVING COUNT(DISTINCT candy_id) = 1;
It gives me an error:
not a group by expression

The group by clause need to have all the non aggregated columns selected directly. Also, it's usually a good idea to use having after the group by as it's the standard way of writing this (even though Oracle supports it the other way too).
Does this do what you want:
select email, age, name
from candyc
group by id, email, age, name
having count(distinct candy_id) = 1
If not, you should provide sample data and expected results in your question to clarify.

I think you want something more like this:
SELECT candy_id, COUNT(*)
FROM CandyC
GROUP BY candy_id;
I don't know what the email/age/name columns have to do with the question:
I want to count the CandyC.ids associated with a CandyC.candy_id once.

Related

How to do text calculations using columns in SQL

I'd like to conditionally include the values from particular columns in a SQL query. To illustrate the question let me use the fictional example of returning selective data about users.
If we were to return all data we'd use a query of the form:
SELECT
name, email, gender
FROM
users
Assume all users have entered their gender. However some have set the value of gender_public to be false and I would like to reflect this in the data the query returns.
The type of thing I'd like to do is:
SELECT
name, email, if(gender_public, gender, 'N/A')
FROM
users
Is this possible? I'm working with Postgres
Using CASE
This is the obvious way to go, as suggested by #a_horse_with_no_name.
Assuming gender_public is of type BOOLEAN.
SELECT name, email, CASE WHEN gender_public THEN gender ELSE 'N/A' END AS gender
FROM users;
Using lateral join
This way of doing is especially useful if you need to use the value in more than one place and want to remain consistent.
SELECT name, email, T.gender
FROM users
CROSS JOIN LATERAL (
SELECT gender WHERE show_gender
UNION
SELECT 'N/A' WHERE NOT(show_gender)
) T(gender)

How to get the number of time a particular number appeared

I want count the number of times a single data occured in a column, how can I achieve that using mysqli. For instance I want to know the number of times Victor appeared in the column of name.
If you're using SQL server:
SELECT name, count(1)
from Tablename
where name like 'Victor'
group by name
This query will give you results like - eg Victor appeared 22 times:
Victor 22
Is this what you're looking for?
Please provide more information so its easier to assist.
Try window functions
SELECT ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Name DESC)
AS Total, Name from table
this one will give you count of records with name='Victor'
select count(name) as cnt from t where name='Victor'
this one will give you all names with counts
select name, count(1) as cnt
from t
group by name
order by name

Coalesce of multiple values group by date in postgres

I have a table as shown below:
I need to select columns based on identity_no and also, need to select non zero/non null column values grouped by src_date. so when I query on the identity_no, I want something like:
I need a postgresql statement or even a function is ok. I tried with coalesce. but i can't do group by on src_date. Here src_date is a string and not a date
Use group by and max()
select scr_date,max(upload) as upload, max(download) as download, identity_no, max(email) as email,
max(phone) as phone, min(id) as id
from tablename
group by scr_date,identity_no
You need to group your table by scr_date and other columns need to be aggregated with a method such as min or max. From your output, it seems that the id is the minimum of each group. Other columns don't containt multiple values for each group, therefore min and max would work the same. However you should consider if other columns had multiple values for each group, how would you want them to be aggregated.
select min(id) as id, max(upload) as upload, max(download) as download, max(email) as email, max(phone) as phone, scr_date
from tbl
group by scr_date

SQL - Your query does not include the specified expression as part of an aggregate function

I'm trying to display data from two tables in ms access using an SQL command. I want to display the data title, forename, surname, name, type and breed. And group it by surname and name. I created the SQL command below which works fine on phpmyadmin but I get and error on ms access which is displayed below the SQL command. Any help is appreciated.
SELECT Title, Forename, Surname, Name, Type, Breed
FROM owner, pet
GROUP BY Surname, Name;
Error
Your query does not include the specified expression 'Title' as part of an aggregate function.
Any field in a group by query must either be in the group by clause, or be in an agrigation function. When field is of String type, you can use MIN(), or MAX() functions, that will give you the desired results:
SELECT min(Title), min(Forename), Surname, Name, Min(Type), Min(Breed)
FROM owner, pet
GROUP BY Surname, Name;
When using the GROUP BY Statement, you can only include the grouped columns along with SUM, COUNT and things like that. These are the "aggregate functions".
In your case, Title is not one of the GROUP BY Parameters.
So, if these other columns are the same for each individual, go ahead and include them in the GROUP BY parameter list.

SQL Group By error ORA-00979

I'm trying to give this query:
select s_name, course from Student group by course;
But I get an error (ORA-00979 Not a GROUP BY EXPRESSION).
I want to list the names of all the students that are in the same course.
Is there another method of doing this? If not, what is the proper way to implement this query? I would appreciate if someone could give me the exact code required.
One variant (Oracle 11g):
select course, listagg(s_name, ', ') within group (order by s_name)
from student
group by course;
Oracle 10g (undocumented secret function wm_concat)
select course, wm_concat(s_name)
from student
group by course;
For what you want you shouldn't use GROUP BY.
The intention of GROUP BY is to summarise information per group.
Since you want detail within each course, you should rather use ORDER BY to ensure that your output is simply sorted with students in the same course listed together.
select s_name, course
from Student
order by course
For an example of what GROUP BY is intended for, try the following:
select course, COUNT(*) as NumStudents
from Student
group by course