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(*);
Related
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;
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
i am using MYSQL..
I have two tables:
TABLE 1 (TABLE NAME T1)
SL NAME
1 a
2 b
3 c
4 c
table 2 (table name T2)
SL NAME
1 a
2 c
3 c
4 c
Q1: how i count the total number of 'c' in both table?
Q2: which name is max occurrences in both table?
sl is primary key...
my query is:>
select count(*) from t1,t2
where t1.name=t2.name where t1.name='c';
but it showing 6
To count c in both tables you should use UNION, not JOIN.
Syntax:
SELECT ...
UNION [ALL | DISTINCT] SELECT ...
[UNION [ALL | DISTINCT] SELECT ...]
Doc:
http://dev.mysql.com/doc/refman/5.0/en/union.html
Edit:
I'll explain the query that you provided.
select count(*) from t1,t2 where t1.name=t2.name where t1.name='c';
First of all, you use WHERE clause twice which is a syntax error. Should be:
select count(*) from t1,t2 where t1.name=t2.name AND t1.name='c';
And this is the same that:
SELECT count(*) from t1
JOIN t2 ON t1.name=t2.name
WHERE t1.name='c';
You choose only rows with c value so these are the rows, that we will take under consideration:
TABLE 1 (TABLE NAME T1)
SL NAME
3 c
4 c
table 2 (table name T2)
SL NAME
2 c
3 c
4 c
Now, simple JOIN joins every row from table 1 to every row from table 2 (where condition is true of course)
So the result before counting is:
t1.SL t1.NAME t2.SL t2.NAME
3 c 2 c
4 c 3 c
3 c 4 c
4 c 2 c
3 c 3 c
4 c 4 c
This is 6 rows.
Answers for both of your questions.
SELECT name, count(*) as cnt
FROM(select t1.name from t1
union all
select name from t2) as tem
group by name
order by cnt DESC
This query will give you ranking of names ordered by occurrences.
To retrieve only c count, just add WHERE clause. To retrieve only the most occurring name set LIMIT clause to 1.
INSERT INTO #test
SELECT NAME FROM m_t1 WHERE NAME ='c'
UNION all
SELECT NAME FROM m_t2 WHERE NAME ='c'
SELECT count(*) FROM #test
I have a table like this
Table A:
Id Count
1 4
1 16
1 8
2 10
2 15
3 18
etc
Table B:
1 sample1.file
2 sample2.file
3 sample3.file
TABLE C:
Count fileNumber
16 1234
4 2345
15 3456
18 4567
and so on...
What I want is this
1 sample1.file 1234
2 sample2.file 3456
3 sample3.file 4567
To get the max value from table A I used
Select MAX (Count) from A where Id='1'
This works well but my problem is when combining data with another table.
When I join Table B and Table A, I need to get the MAX for all Ids and in my query I dont know what Id is.
This is my query
SELECT B.*,C.*
JOIN A on A.Id = B.ID
JOIN C on A.id = B.ID
WHERE (SELECT MAX(COUNT)
FROM A
WHERE Id = <what goes here????>)
To summarise, what I want is Values from Table B, FileNumber from Table c (where the count is Max for ID from table A).
UPDATE: COrrecting table C above. Looks like I need Table A.
I think this is the query you're looking for:
select b.*, c.filenumber from b
join (
select id, max(count) as count from a
group by id
) as NewA on b.id = NewA.id
join c on NewA.count = c.count
However, you should take into account that I don't get why for id=1 in tableA you choose the 16 to match against table C (which is the max) and for id=2 in tableA you choose the 10 to match against table C (which is the min). I assumed you meant the max in both cases.
Edit:
I see you've updated tableA data. The query results in this, given the previous data:
+----+---------------+------------+
| ID | FILENAME | FILENUMBER |
+----+---------------+------------+
| 1 | sample1.file | 1234 |
| 2 | sample2.file | 3456 |
| 3 | sample3.file | 4567 |
+----+---------------+------------+
Here is a working example
Using Mosty’s working example (renaming the keyword count to cnt for a column name), this is another approach:
with abc as (
select
a.id,
a.cnt,
rank() over (
partition by a.id
order by cnt desc
) as rk,
b.filename
from a join b on a.id = b.id
)
select
abc.id, abc.filename, c.filenumber
from abc join c
on c.cnt = abc.cnt
where rk = 1;
select
PreMax.ID,
B.FileName,
C2.FileNumber
from
( select C.id, max( C.count ) maxPerID
from TableC C
group by C.ID
order by C.ID ) PreMax
JOIN TableC C2
on PreMax.ID = C2.ID
AND PreMax.maxPerID = C2.Count
JOIN TableB B
on PreMax.ID = B.ID
i have 3 tables (A,B,C)
Table A -
ID Name
1 Sam
2 Manuel
3 Jane
Table B
ID Tab_A_ID Name
1 1 Meer
2 1 Kutti
3 2 Mikaro
Table C
ID Tab_B_ID Price
1 1 255.11
2 1 30.52
3 3 125.22
I need a query that shall pick up the top price for TableA-Name from TableC. So only 1 top price for 1 nae record.
e.g.-
Sam - 255.11
Manuel - 125.22
How can i get this?
To get the max price per entry in A:
SELECT a.Name,
MAX(c.price)
FROM a
INNER JOIN b
ON a.id = b.tab_a_id
INNER JOIN c
ON b.id = c.tab_b_id
GROUP BY a.id, a.name
To get the max price per entry A per entry B:
SELECT a.Name,
b.Name
MAX(c.price)
FROM a
INNER JOIN b
ON a.id = b.tab_a_id
INNER JOIN c
ON b.id = c.tab_b_id
GROUP BY a.id, b.id, a.name, b.name
Note that entries in A without corresponding entires in B or entries in B without corresponding entries in C will not appear in the result. Use LEFT JOIN if you want to include these in the result.