group by sql oracle - sql

I have a table with multiple fields, among which are: num1 (INTEGER), name(STRING) and datatime(TIMESTAMP). I would like to get the sample with name, sum(num1) and count(num1) grouped by month. I'm trying to do this:
SELECT name, sum(num1), count(num1) from mytable group by to_char(datatime, 'YYYY-MM')
But it doesn't work. Help me, please, and sorry for my English:)

Sum not sun
Group By not grouped by
all non-aggregates from select must be in the group by so add name.
.
SELECT name, sum(num1), count(num1)
FROM mytable
GROUP BY to_char(datatime, 'YYYY-MM'), name
Generally if you're going to group your data by yyyy-mm you would generally want to see that in the select... but maybe you have your reasons... otherwise how do you tell multiple values of name apart? What's in ()'s are not in your results so... which John is which?
John 123 23 (1990-01)
John 234 44 (1990-02)
John 323 22 (1990-03)

Related

SQL Query to get the Average age from multiple 'date' values

I'm needing to get a SQL Query which returns the average age of multiple data inputs in my table
Households
User
Dates
1
2002-01-01
2
2004-06-10
I want to grab both User 1 and 2 date of births and return the average age of them.
Managed to get the age from the date of births using
SELECT *, DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), Date)), '%Y') + 0 AS age
FROM Households;
I just can't get the rest of it working to then average the ages out.
Assuming that you are running MySQL, as the syntax of your SQL code suggests.
For starts, I would recommend simplifying the age computation. MySQL provides timestampdiff(), which we can use like so:
select user_id, user_date,
timestampdiff(year, user_date, current_date) age
from houshold
user_id
user_date
age
1
2002-01-01
20
2
2004-06-10
18
3
2004-11-10
17
To compute the average age over all rows of the table, we can use aggregate function avg():
select avg(timestampdiff(year, user_date, current_date)) avg_age
from houshold
avg_age
18.3333
Here is a small demo based on your sample data. Note that I renamed the columns so they do not clash with meaningful SQL names.

SQL max without group by

I would like to get one row with the maximum date. I cannot use group by as I need to retrieve all data in that row.
I have this:
ID Date Country
1 05/05/2019 US
2 05/06/2019 UK
I want to get this:
ID Date Country
2 05/06/2019 UK
I've tried the below but it didn't work for me
select TOP 1 ID, Date, country
from table
order by Date desc
I don't believe you. Here is a db<>fiddle that shows three different interpretations of the date in your sample data:
as a string
as mm/dd/yyyy
as dd/mm/yyyy
All three of them produce the same result.
I suspect that your actual data is more complicated and you have oversimplified the example for the question. Further, my suspicion is that the date column is stored as a string rather than a date.
As a string, you might have some hidden characters that affect the sorting (such as leading spaces).
If this is the case, fix the data type and your code will work.
This depends on what DB system you are using.
In Microsoft SQL server, you can use row_number() function:
select top 1 *
from facts
order by ROW_NUMBER() over (order by dateKey)
Can you try this?
select Top 1 ID,Date, country from table where date = max(date)
First set the DATE or DATETIME Datatype in your [Date] column
then try this code:
SELECT TOP 1 ID, [Date] , country FROM TableName ORDER BY Date DESC
SELECT ID,Date,Country from TableName Where Date = MAX(Date) AND Rownum <= 1

SQL EXTRACT(YEAR FROM MYDATE) not a GROUP BY expression

I have table MYTABLE with columns mydate and quantity of VARCHAR2 type.
|mydate| |quantity|
10/15/2010 15
01/20/2010 20
05/16/2005 30
04/29/2005 50
03/30/2008 5
I want to get:
|year| |quantity|
2010 35
2005 80
2008 5
I try:
SELECT
to_char(mydate,'yyyy') YEAR,
SUM(to_number(quantity))
FROM MYTABLE
GROUP BY
to_char(mydate,'yyyy');
But I get an error
ORA-00979: not a GROUP BY expression
What did I do wrong?
You must put all columns of the SELECT in the GROUP BY or use functions on them which compress the results to a single value (like MIN, MAX or SUM).
A simple example to understand why this happens: Imagine you have a database like this:
FOO BAR
0 A
0 B
and you run SELECT * FROM table GROUP BY foo. This means the database must return a single row as result with the first column 0 to fulfill the GROUP BY but there are now two values of bar to chose from. Which result would you expect - A or B? Or should the database return more than one row, violating the contract of GROUP BY?
Try this
select extract(year from mydate),sum(to_number(quant)) from mytable
group by extract(year from mydate);
SQLFiddle Example

How to get duplicate values in all rows filtering by one column

Here is what my table looks like.
Person Date Entry
Person1 05-20-14 142
Person2 05-20-14 443
Person1 05-21-14 248
Person1 05-21-14 142
I need two things.
First the number of times a Person made an entry for the first time.
I tried doing it with these queries. But the problem is I need this information per day.
That is if I query for 05/21, I need to see output
"Person1 1"
142 wont be included because it already exists.
In my query, I am filtering by date already, so I am not sure how to go out and search in the rest of the dates values. Here is what I have.
SELECT PERSON, Count(distinct Entry)
from [table]
where date >= 05/21/2014
and date < 05/22/2014
group by person
order by person.
This gives me
Person1 2
Both 248 and 142 are considered here. How do I look for 142 was an entry already made in previous dates. I am not very good at nested queries.
Thanks for looking.
Will this solve your problem or give you an idea how inner query should be?
SELECT PERSON, Count(distinct Entry)
from [table]
where date >= 05/21/2014
and date < 05/22/2014
and Entry not in (select distinct entry from [table] where date <> 05/21/2014)
group by person
order by person.
in the above query i have just added an inner query to get the distinct entry from other dates
select distinct entry from [table] where date <> 05/21/2014
and i have added the where condition that the current result should not consider those entries by
and Entry not in (select distinct entry from [table] where date <> 05/21/2014)
hope this helps you.
For the first query, it sounds like you need something like this:
SELECT Person
FROM sample
GROUP BY date, person
HAVING date = '05-21-2014'
See http://sqlfiddle.com/#!3/4653d/1
This might also help:
SELECT Person, date
FROM sample
GROUP BY date, person
ORDER BY date
Hopefully that helps, let me know if I am misunderstanding something..

Group by two columns is possible?

I have this table:
ID Price Time
0 20,00 20/10/10
1 20,00 20/10/10
2 20,00 12/12/10
3 14,00 23/01/12
4 87,00 30/07/14
4 20,00 30/07/14
I use this syntax sql to get the list of all prices in a way that does not get repeated values:
SELECT * FROM myTable WHERE id in (select min(id) from %# group by Price)
This code return me the values (20,14,87,20)
But in this case I would implement another check, that will not only sort by price but also by date, example: That syntax is getting the list by price, if I find a way to check by date, the code will return me the values (20,20,14,87,20)
He repeats 20 two times but if we see in the table we have three numbers 20 (two with the date 20/10/10 and one with the date 12/12/10) and is exactly what I'm wanting to get!
Somebody could help me?
To group by multiple columns, just put a comma in between the list.
SELECT price FROM myTable group by price, time order by time
The group by looks at all distinct combinations of the listed columns values, and discards duplicates. You can also use aggregate functions like sum or max to pull in additional columns to the results.
The following should work as long as all you need is the price/time combination. If you need to include the ID, things get more complicated:
SELECT `Price` FROM items
GROUP BY `Price`, `Time`
ORDER BY `Time`;
Here's a fiddle with the result in action: http://sqlfiddle.com/#!2/40821/1