using group by in subquery in sql - sql

how to get around this error :
Unable to use an aggregate or a subquery in an expression used in the
GROUP BY list of a GROUP BY clause.
here is my query :
select Id, name,dayA,monthA,yearA,
sum(x) as x,
(select SUM(x) group by month) as total,
from table_A
group by Id,name,monthA,dAyA,yearA, SUM(x)
in other words :
sample data :
id name dayA monthA yearA x
===========================
1 name1 2 3 2016 4
2 name2 2 3 2016 3
3 name1 2 3 2016 2
Expected result :
id name dayA monthA yearA x total
===================================
1 name1 2 3 2016 4 6
2 name2 2 3 2016 3 3
3 name1 2 3 2016 2 6
Thanks in advance

you're query has more problem.
(select SUM(x) group by month) as total, is it from the same table, not likely since column month is not mention inyour group by. When using sub query in a query, you must guaranteed that i will only return one record.
Based on your sample data and expected results...
create table table_A(
id int,
name varchar(25),
dayA int,
monthA int,
yearA int,
x int
)
insert into table_A
values (1,'name1',2,3,2016,4),
(2,'name2',2,3,2016,3),
(2,'name1',2,3,2016,2)
select ta.id, ta.name, ta.dayA, ta.monthA, ta.yearA, ta.x, total.Total from table_A as ta
left join
(select name, sum(x) as Total from table_A group by name) total on ta.name = total.name
group by
ta.id, ta.name, ta.dayA, ta.monthA, ta.yearA, ta.x, total.name, total.Total

May be this is what you want:
select table_A.*, TotalSums.total
from table_A
left join (select name, monthA, dayA, yearA, sum(x) as total from table_A group by name, monthA, dayA, yearA) as TotalSums
on table_A.name = TotalSums.name
and table_A.monthA = TotalSums.monthA
and table_A.dayA = TotalSums.dayA
and table_A.yearA = TotalSums.yearA
order by id

i think this is what you're looking for
select Id, main.name,dayA,main.monthA,main.yearA,
sum(x) as x,
,max(total.total) as total
from table_A as main
join (select SUM(x) total ,name ,monthA,yearA from table_A group by name,monthA,yearA) as total
on main.name = total.name
and main.monthA = total.monthA
and main.yearA = total.yearA
group by Id,main.name,monthA,dAyA,yearA

Related

How do i select all columns, plus the result of the sum

I have this select:
"Select * from table" that return:
Id
Value
1
1
1
1
2
10
2
10
My goal is create a sum from each Value group by id like this:
Id
Value
Sum
1
1
2
1
1
2
2
10
20
2
10
20
I Have tried ways like:
SELECT Id,Value, (SELECT SUM(Value) FROM Table V2 WHERE V2.Id= V.Id GROUP BY IDRNC ) FROM Table v;
But the is not grouping by id.
Id
Value
Sum
1
1
1
1
1
1
2
10
10
2
10
10
Aggregation aggregates rows, reducing the number of records in the output. In this case you want to apply the result of a computation to each of your records, task carried out by the corresponding window function.
SELECT table.*, SUM(Value) OVER(PARTITION BY Id) AS sum_
FROM table
Check the demo here.
Your attempt looks correct.
Can you try the below query :
It works for me :
SELECT Id, Value,
(SELECT SUM(Value) FROM Table V2 WHERE V2.Id= V.Id GROUP BY ID) as sum
FROM Table v;
You can do it using inner join to join with selection grouped by id :
select t.*, sum
from _table t
inner join (
select id, sum(Value) as sum
from _table
group by id
) as s on s.id = t.id
You can check it here
Your select is ok if you adjust it just a little:
SELECT Id,Value, (SELECT SUM(Value) FROM Table V2 WHERE V2.Id= V.Id GROUP BY IDRNC ) FROM Table v;
GROUP BY IDRNC is a mistake and should be GROUP BY ID
you should give an alias to a sum column ...
subquery selecting the sum does not have to have self table alias to be compared with outer query that has one (this is not a mistake - works either way)
Test:
WITH
a_table (ID, VALUE) AS
(
Select 1, 1 From Dual Union All
Select 1, 1 From Dual Union All
Select 2, 10 From Dual Union All
Select 2, 10 From Dual
)
SELECT ID, VALUE, (SELECT SUM(VALUE) FROM a_table WHERE ID = v.ID GROUP BY ID) "ID_SUM" FROM a_table v;
ID VALUE ID_SUM
---------- ---------- ----------
1 1 2
1 1 2
2 10 20
2 10 20

Select the non repeating/Distinct value in SQL

I'm trying to select the record based on the distinct id. When i go for 'DISTINCT' it picks the duplicate record and truncates the repeating record and gives me the one left out.
How can i SQL to pick to just that record which isn't repeated ?
INPUT
id
name
age
location
1
a
22
usa
1
a
23
usa
2
b
44
uk
3
e
33
eu
3
f
55
eu
8
k
49
usa
OUTPUT
id
name
age
location
2
b
44
uk
8
k
49
usa
ok , here is how you can do it :
select * from (
select * , count(*) over (partition by id) cn
from tablename
) t
where cn = 1
Try this:
SELECT *
FROM [Input]
WHERE ID IN (
SELECT ID FROM [Input] GROUP BY ID HAVING COUNT(ID) = 1
)
This should achieve the output you're after:
SELECT *
FROM yourtable
WHERE id IN (
SELECT id
FROM yourtable
GROUP BY id
HAVING COUNT(*) = 1)
You can use SQL Common Transaction Expression (CTE) AS FOLLOWS
declare #mytable as table(id int ,name nvarchar(100),age int,location nvarchar(50))
insert into #mytable values
(1,'a',22,'usa'),(1,'a',23,'usa'),(2,'b',44,'uk'),(3,'e',33,'eu'),(3,'f',55,'Tunisia'),('8','k',49,'Palestine')
with
cte1 as(select * from #mytable),
cte2 as (select id, count(1) N from #mytable group by id),
cte3 as (select TA.id,TA.name,TA.age,TA.location from cte1 TA inner join cte2 TB on TA.id=TB.id where TB.N=1)
select * from cte3

Group by and filter on 2 Distinct values in SQL

I have table T1
ID Size
A 1
A 2
A 3
B 3
B 4
C 2
C 4
I want to group by ID and filter the smallest size for each ID
Desired outcome:
A 1
B 3
C 2
I tried doing something like this:
SELECT ID, Size
FROM T1
WHERE ID IN (SELECT DISTINCT ID FROM T1)
You want a basic GROUP BY query:
SELECT ID, MIN(Size) AS Size
FROM T1
GROUP BY ID;
SELECT T1.id, MIN(T1.SIZE) AS MinimumSize FROM T1 GROUP BY T1.ID
may be you can find your solution.

select count of each distinct value in a column

I'm trying to SELECT a result set which contains two columns:
The DISTINCT values of a column.
The number of times that that value appears in the column in the table.
This statement (sort of) illustrates what I'm trying to do:
SELECT DISTINCT ID AS X,
(SELECT COUNT(*) FROM #t WHERE ID = X)
FROM #t
ORDER BY X;
The desired result set would look like:
| ID | COUNT
|------|------
| 0 | 12
| 1 | 16
| 2 | 4
SELECT ID AS X,
COUNT(*) AS "COUNT"
FROM #t
GROUP BY ID
ORDER BY X
SELECT ID AS X,
COUNT(1) AS "COUNT"
FROM #t
GROUP BY ID
ORDER BY ID
Use Group By
Your query (Correlated subqueries) should work too. As per GordonLinoff, Replace X with ID in your query and it should work.
SELECT DISTINCT ID AS X,
(SELECT COUNT(*) FROM #t WHERE ID = X)
FROM #t
ORDER BY ID ;
This is just a simple agregation using GROUP BY:
SELECT ID, COUNT(*)
FROM #t
GROUP BY ID
ORDER BY ID;

left join without duplicate values using MIN()

I have a table_1:
id custno
1 1
2 2
3 3
and a table_2:
id custno qty descr
1 1 10 a
2 1 7 b
3 2 4 c
4 3 7 d
5 1 5 e
6 1 5 f
When I run this query to show the minimum order quantities from every customer:
SELECT DISTINCT table_1.custno,table_2.qty,table_2.descr
FROM table_1
LEFT OUTER JOIN table_2
ON table_1.custno = table_2.custno AND qty = (SELECT MIN(qty) FROM table_2
WHERE table_2.custno = table_1.custno )
Then I get this result:
custno qty descr
1 5 e
1 5 f
2 4 c
3 7 d
Customer 1 appears twice each time with the same minimum qty (& a different description) but I only want to see customer 1 appear once. I don't care if that is the record with 'e' as a description or 'f' as a description.
First of all... I'm not sure why you need to include table_1 in the queries to begin with:
select custno, min(qty) as min_qty
from table_2
group by custno;
But just in case there is other information that you need that wasn't included in the question:
select table_1.custno, ifnull(min(qty),0) as min_qty
from table_1
left outer join table_2
on table_1.custno = table_2.custno
group by table_1.custno;
"Generic" SQL way:
SELECT table_1.custno,table_2.qty,table_2.descr
FROM table_1, table_2
WHERE table_2.id = (SELECT TOP 1 id
FROM table_2
WHERE custno = table_1.custno
ORDER BY qty )
SQL 2008 way (probably faster):
SELECT custno, qty, descr
FROM
(SELECT
custno,
qty,
descr,
ROW_NUMBER() OVER (PARTITION BY custno ORDER BY qty) RowNum
FROM table_2
) A
WHERE RowNum = 1
If you use SQL-Server you could use ROW_NUMBER and a CTE:
WITH CTE AS
(
SELECT table_1.custno,table_2.qty,table_2.descr,
RN = ROW_NUMBER() OVER ( PARTITION BY table_1.custno
Order By table_2.qty ASC)
FROM table_1
LEFT OUTER JOIN table_2
ON table_1.custno = table_2.custno
)
SELECT custno, qty,descr
FROM CTE
WHERE RN = 1
Demolink