1

I have two tables A, B and my query show it:(join A and B)

select A.i,A.j,B.x,B.y,B.z from A inner join B on A.id=B.id_B order by A.i,A.j

.

+-----+-----+-----+-----+-----+
| A.i | A.j | B.x | B.y | B.z |
+=====+=====+=====+=====+=====+
|  bk | bg  |  1  | inf1| bar | 
+-----+-----+-----+-----+-----+
|  bk | bg  |  2  | inf2| bar | 
+-----+-----+-----+-----+-----+
|  bk | bg  |  3  | inf3|  y  | 
+-----+-----+-----+-----+-----+
|  ro | fn  |  5  | enf1| bar | 
+-----+-----+-----+-----+-----+
|  ro | fn  |  3  | enf2| bar | 
+-----+-----+-----+-----+-----+
|  st | st  |  3  | onf1| bar | 
+-----+-----+-----+-----+-----+

Now I want to know is how many times

A.i

is repeated, like that:

    +-----+-----+-----+-----+-----+
    | A.i | A.j | B.x | B.y | B.z |RepeatColumn
    +=====+=====+=====+=====+=====+---
    |  bk | bg  |  1  | inf1| bar | 3 |(bk is repeated 3 times)
    +-----+-----+-----+-----+-----+---
    |  bk | bg  |  2  | inf2| bar | 3 |(bk is repeated 3 times)
    +-----+-----+-----+-----+-----+---
    |  bk | bg  |  3  | inf3|  y  | 3 |(bk is repeated 3 times)
    +-----+-----+-----+-----+-----+---
    |  ro | fn  |  5  | enf1| bar | 2 |(ro is repeated 2 times)
    +-----+-----+-----+-----+-----+---
    |  ro | fn  |  3  | enf2| bar | 2 |(ro is repeated 2 times)
    +-----+-----+-----+-----+-----+---
    |  st | st  |  3  | onf1| bar | 1 |(st is repeated once)
    +-----+-----+-----+-----+-----+---

How can i do that query, anyone have any idea?

1

5 Answers 5

1

You need subquery:

select A.i,A.j,
       B.x,B.y,B.z,
       (select count(*) from b where b.id = a.id) RepeatColumn
from A 
inner join B 
order by A.i,A.j;

However, i didn't find any relation between tables (A, B) in o/p. So, i just used on fly.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, it was an example and thanks it works for me. I' m using 3 tables with that query, thats why i forgot to write the relation in the example
0

Select count(A.I) from tablename;

Comments

0

Try using subquery

SELECT 
A.i,
A.j,
B.x,
B.y,
B.z,
(SELECT COUNT(1) FROM A as WHERE i = A.i) AS 'RepeatColumn'
FROM A 
INNER JOIN B 
ORDER BY A.i,A.j

Comments

0

edit: Unfortunately (for you) it looks like MySQL doesn't support simple window functions.

Like this:

COUNT(1) OVER
  ( PARTITION BY A.i
  ) AS cnt

It depends on whether your use case is more complicated than what you've shown if it's worth cobbling a group_concat workaround, or simply using a subquery to get the counts.

2 Comments

He/she is using mysql not SQL Server.
@Aureate My mistake. I thought that all major DBMS supported this type of function.
0
 select A.i,
 A.j, 
 B.x, 
 B.y,
 B.z, 
 count(A.i) as repeated_column 
 from A inner join B
 order by A.i, A.j

1 Comment

While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.