SQL: GROUP BY column B for groups in column a - sql

I am looking for a SQL query that will sum the values in column C GROUP BY column B, but not over the entire table, only within the ranges where the same values are present in column A.
Current table:
Column A
Column B
Column C
A
B
10
A
B
5
A
B
8
A
C
1
A
D
7
B
B
10
B
B
5
Required table:
Column A
Column B
Column C
A
B
23
A
C
1
A
D
7
B
B
15
SELECT A, B, SUM(C)
FROM ...
WHERE ...
GROUP BY B
This obviously doesn't work, because I don't use column A with an aggregate function, nor in the GROUP BY statement. How do I get to the desired solution? Thanks for the help.

It seems that you want to group by the first two columns and sum the third:
SELECT A, B, SUM(C) AS SUM_C,
100.0 * SUM(C) / SUM(SUM(C)) OVER (PARTITION BY A) AS PCT
FROM yourTable
GROUP BY A, B;

You can do nested grouping. The first column in the order gets to group the result first and the following columns get grouped within the group of the previous group.
SELECT count(*) FROM table GROUP BY A, B;

Related

SQL query - Cumulatively concatenate strings in consecutive rows

I'm a data analyst, so I write SQL queries to retrieve data from a database. I'm not sure what kind of SQL exactly, just assume the most standard (also not things like 'DECLARE #tbl', and no create functions etc.)
Here is my problem.
Given the following table:
name
number
letter
A
1
a
A
2
b
A
3
c
A
4
d
B
1
a
B
2
b
B
3
c
B
4
d
I want the following result: (concatenate letter cumulatively, order by number))
name
number
letter
result
A
1
a
a
A
2
b
a,b
A
3
c
a,b,c
A
4
d
a,b,c,d
B
1
a
a
B
2
b
a,b
B
3
c
a,b,c
B
4
d
a,b,c,d
Any help is highly appreciated. Thanks very much.
This answers the original version of the question which was tagged MySQL.
MySQL doesn't support group_concat() as a window function. So a subquery may be your best alternative:
select t.*,
(select group_concat(t2.letter order by t2.number)
from t t2
where t2.name = t.name and t2.number <= t.number
) as letters
from t;

Append one table to another table

I have following situation:
Table 1:
A B C
2012 4 2
2012 3 1
Table 2:
A B C
2013 3 2
2013 3 1
My result should look like this:
Table X
A B C
2012 4 2
2012 3 1
2013 3 2
2013 3 1
So I just want to append Table 2 to Table 1. They have the same columns
If you want a new table, then use create table as or the equivalent in your database:
create table x as
select a, b, c
from table1
union all
select a, b, c
from table2;
If you want to "append" the values onto the first table, use insert:
insert into table1 (a, b, c)
select a, b, c
from table2;
Note that tables in SQL represent unordered sets. You have not specified any ordering, so once the rows are in a single table, the original source (and ordering) is not preserved -- unless you include this information in separate columns.
The UNION should allow you to merge the tables
SELECT a,b,c from table1
UNION
SELECT a,b,c from table2

SQL - how to find records with specific type in sql table

I have a sql table with records having types A ,B and C. There are some records with only type B and C . How can I find the records with only type B and C using sql query ?
Emp id type
1 A
1 B
1 C
2 B
2 C
3 A
3 C
4 A
4 B
so my query should return me employee id 2 as it doesn't have type A.
select empId
from your_table
group by empId
having sum(case when type not in ('B','C') then 1 else 0 end) = 0
You want to use a self join or a nested subselect:
select * from employees a
where type in ('B','C')
and not exists
(select 1 from employees b
where a.id = b.id
and b.type = 'A')
This will return all employee records that have types B or C, but not A.

SQLite select most common

The table is look like:
A B
----------
John a
John a
Peter a
Mary b
Ann b
Ann b
I want the result is group by B and select the most common word from A:
A B
----------
John a
Ann b
First I calculate the number of B,A combinations, then per B I calculate the maximum number (MaxN), and finally in the outer query the count is calculated again, but only the lines are shown which have maxN=COUNT(*):
SELECT t.B, t.A, COUNT(*) as N
FROM Table t LEFT JOIN
(SELECT B, MAX(N) AS MaxN
FROM (
SELECT B, A, count(*) as N
FROM Table
GROUP BY B, A) as c
GROUP BY B) as mx on t.B=mx.B
GROUP BY t.B, t.A
HAVING mx.MaxN=COUNT(*);

Select row values that are in repeated rows that have a colum with at least 3 different values

I have a table like this
Ma Mi
-----
A b
A c
A c
A d
B b
B a
B a
B a
B a
C a
C b
I want to make a select that outputs only the "Ma" values that have at least 3 distinct "Mi" values, i've tryed group by and distinct unsuccesfully since i want to group first "Mi" and then count "Ma" with distinct "Mi".
So the intermediate step would be :
Ma Mi
-----
A b
A c
A d
B b
B a
C a
C b
So then i can count the rows per each Ma
In this case, the result would be
Ma num
------
A 3
B 2
C 2
And I could select only A because is the unique equal or higher to 3.
RESULT:
Ma num
------
A 3
Thank you in advanced !
L.
To check the results of a GROUP BY operation, you must use HAVING:
SELECT Ma,
COUNT(DISTINCT Mi) AS Num
FROM ATableLikeThis
GROUP BY Ma
HAVING COUNT(DISTINCT Mi) >= 3
Kindly find query as per your requirement:
Select Distinct MyTable.Ma,
(Select Count(Distinct MyTableAlias.Mi) From MyTable As MyTableAlias Where MyTableAlias.Ma = MyTable.Ma) As Num from MyTable WHere (Select Count(Distinct MyTableAlias.Mi) From MyTable As MyTableAlias Where MyTableAlias.Ma = MyTable.Ma) >= 3