Selected columns that are not in group by - sql

I am a quetions about SQL. For example i have a table like this
Name Surname Price Adress
john smith 100 adress123
alex martin 200 adress2
john smith 300 adress123
And i want to group this records which name is same.And this records prices must be sum()
Generally i write query like this
SELECT SUM(PRICE),NAME AS TOTAL_PRICE FROM TABLE1 A GROUP BY A.NAME
But when i want to select other columns i should be group by like this
... group by A.NAME,A.SURNAME,A.ADRESS
I want select this columns without group by. I want to ask what is the best way selecting other columsn without using group by condition?
I am waiting this result
200 ALEX MARTIN adress2
210 JOHN SMITH adress123
but i don't want to group by surname and adress column

You can get an arbitrary value from the rest of the columns by using MIN() or MAX():
select sum(price) as total_price, name, max(surname) as surname,
max(address) as address
from table1 a
group by a.name;

Related

How to get values of one column without the aggregate column?

I have this table:
first_name
last_name
age
country
John
Doe
31
USA
Robert
Luna
22
USA
David
Robinson
22
UK
John
Reinhardt
25
UK
Betty
Doe
28
UAE
How can I get only the names of the oldest per country?
When I do this query
SELECT first_name,last_name, MAX(age)
FROM Customers
GROUP BY country
I get this result:
first_name
last_name
MAX(age)
Betty
Doe
31
John
Reinhardt
22
John
Doe
31
But I want to get only first name and last name without the aggregate function.
If window functions are an option, you can use ROW_NUMBER for this task.
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY country ORDER BY age DESC) AS rn
FROM tab
)
SELECT first_name, last_name, age, country
FROM cte
WHERE rn = 1
Check the demo here.
It sounds like you want to get the oldest age per country first,
SELECT Country, MAX(age) AS MAX_AGE_IN_COUNTRY
FROM Customers
GROUP BY Country
With that, you want to match that back to the original table (aka a join) to see which names they match up to.
So, something like this perhaps:
SELECT Customers.*
FROM Customers
INNER JOIN
(
SELECT Country, MAX(age) AS MAX_AGE_IN_COUNTRY
FROM Customers
GROUP BY Country
) AS max_per_country_query
ON Customers.Country = max_per_country_query.Country
AND Customers.Age = max_per_country_query.MAX_AGE_IN_COUNTRY
If your database supports it, I prefer using the CTE style of handling these subqueries because it's easier to read and debug.
WITH cte_max_per_country AS (
SELECT Country, MAX(age) AS MAX_AGE_IN_COUNTRY
FROM Customers
GROUP BY Country
)
SELECT Customers.*
FROM Customers C
INNER JOIN cte_max_per_country
ON C.Country = cte_max_per_country.Country
AND C.Age = cte_max_per_country.MAX_AGE_IN_COUNTRY

SQL Find/Count similar duplicates

I am trying to to do a count on all records that are similar.
Eg.
I have 2 records 1 is 1A Smith St and the other is 1 Smith St.
So this data needs to be returned/counted as 1.
Any assistance would be gratefully appreciated.
Thanks
Counting those where the (street, number) combination isn't unique.
SELECT COUNT(*) AS Total
FROM
(
SELECT Street, Number
FROM your_table
GROUP BY Street, Number
HAVING COUNT(*) > 1
) q
use
SELECT COUNT(1) AS NUMBER, Suffix, Street FROM TableName GROUP BY Suffix, Street

Select distinct lines by a field

I am making a select that returns me a table likes this
Name surname
Jhon a
Jhon b
Jhon c
Joe a
Joe b
Joe c
But what I need to get is just one occurrence of Jhon and one of Joe with one of the surnames.
I can only have one Jhon with one surname and one Joe with a surname..
I cannot make an order by because I need to select Name and surname.. Also if I use distinct I will have all Jhons and Joes..
Can you help me?
You can just use aggregation:
select name, max(surname) as surname
from table t
group by name;
You can also do something similar with analytic functions:
select t.name, t.surname
from (select t.*, row_number() over (partition by name order by name) as seqnum
from table t
) t
where seqnum = 1;
This is particularly useful if you want to get more than one column from the same row.

Sum of a column with distinct ID?

i have a table that has multiple IDs and need a query to return the sum of a column for each ID. Calls_table looks like this
EmployeeID TypeOfCall InvoiceAmount
John NC 50
john NC 100
Joe NC 76
Joe NC 50
i have it so i have to do it employee by emplyee now like
SELECT sum(InvoiceAmount/2) as "Total Calls"
from Calls
where TypeOfCall='NC' and EmployeeID='Derek';
but i would like it to be able to return all IDs in a list like this
Total Calls
Joe 100
John 68
I am sure i need to use the Distinct parameter but just cant figure out where
You need to use the group by keyword
Select EmployeeID, SUM(InvoiceAmount)
From Calls
Group by EmployeeID
You can even take it a bit further and group by type of call and EmployeeID like This:
Select EmployeeID, TypeOfCall, SUM(InvoiceAmount)
From Calls
Group by EmployeeID, TypeOfCall
Your selected fields need to be either aggregate functions (sum, count, avg, etc) or in the Group by when doing this.
SELECT EmployeeID, SUM(InvoiceAmount/2)
FROM Calls
WHERE TypeOfCall='NC'
GROUP BY EmployeeID

Need a hand with a simple query

I need a help with a query. I think is not so difficult.
I need to do a select with distinct and at the same time, do a count(*) of how many rows are returned by this distinct.
One example:
Table names>
Id Name
1 john
2 john
3 mary
I need a query thats return:
Name Total
john 2
mary 1
select name, count(*) from names group by name;
SELECT name, COUNT(*) FROM names GROUP BY name
SELECT name, count(*) as occurrences FROM names GROUP BY name