yes I tried lot but unable to find repeated value - mysql

Kindly answer if possible.
Query to select record of table repeatedly two or more times to each row.
Like:
ID1 Name1 Age1
ID1 Name1 Age1
ID1 Name1 Age1
ID2 Name2 Age2
ID2 Name2 Age2
ID2 Name2 Age2
.. .. An so on.

Combination of UNION ALL and ORDER BY will do.
(SELECT * FROM TABLE_NAME) UNION ALL
(SELECT * FROM TABLE_NAME) UNION ALL
(SELECT * FROM TABLE_NAME)
ORDER BY 1,2,3
The above example is for 3 repeated times. If you want more, just add more UNION statements.

Related

Using count with group without distinct elements

I have the following table:
select id1, id2 from myTable
id1 id2
1 100
2 100
3 100
3 400
4 200
I want to count how many times each case of id1 occurs, while retaining all cases of id2 in the second column such that I receive the following table:
id1 id2 count
1 100 1
2 100 1
3 100 2
3 400 2
4 200 1
The following query gives the correct count, but only shows distinct elements of id1, thus it doesn't retain all cases of id2
select id1, id2, count(id1) from myTable
group by id1
id1 id2 count
1 100 1
2 100 1
3 100 2
4 200 1
Instead I tried this query, to group by both columns, which retains all of the information, but does not give the count I desire:
select id1, id2, count(id1) from myTable
group by id1, id2
id1 id2 count
1 100 1
2 100 1
3 100 1
3 400 1
4 200 1
This feels like it should be extremely simple. Can this be done in a single query, or must I use a subquery? If so, what would the subquery look like?
Try this:
select id1, id2,
(select count(*) from mytable as t2 where t1.id1 = t2.id1) "count"
from mytable as t1
By using a subselect as a column result, you can build your desire structure in output.
Try this.
SELECT
a.*, b.count
FROM
test.id a,
(SELECT
id1, COUNT(id1) AS count
FROM
test.id
GROUP BY id1) b
WHERE
a.id1 = b.id1;
select id1, id2, count(id1) OVER (PARTITION BY id1) from myTable

Cannot figure a simple mysql query out

I feel very very stupid now because I have a problem and cannot seem to figure it out.
Very simple MySQL table with 2 columns :
ID1 | ID2
1 | 1
1 | 2
2 | 1
Don't know very good how to explain the conditions : I want to select the value 1 from the column ID1 because it has connections with the values 1 AND 2 from ID2.
It's somewhat the opposite of IN.
If I make
SELECT ID1 FROM X WHERE ID2 IN (1,2) I recieve both 1 and 2 because it is a reunion. I want an intersection, something like SELECT ID1 FROM X WHERE ID2 IN BOTH 1 AND 2.
I am fairly sure it has something to do with grouping.
1 solution is to make
SELECT * FROM
(SELECT ID1, GROUP_COCAT(ID2) y
FROM X
GROUP BY ID1)t
WHERE t.y = '1,2'
but this is NOT ok because I do not know the order ( 1,2 or 2,1 ) and I can have more values.
Hopefully this is clear enough, I am very tired.
SELECT t.*
FROM TEMP t
WHERE t.id2 IN (1, 2)
GROUP BY t.id1 HAVING COUNT(*) = 2
OR
SELECT t.*
FROM TEMP t
WHERE t.id2 IN (1, 2, 3, 4)
GROUP BY t.id1 HAVING COUNT(*) = 4

Counting unique numbers in a column MySQL

I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number

MySQL multiple values on own columns

I have a table like so;
id1 id2
1 1
2 1
1 2
3 2
1 3
4 3
1 4
5 4
I'd like to select it in a way that I'd get rows GROUPed by id2, but still preserving both values of id1 for the corresponding rows in the table.
So I'd get a result set like so;
id1 id1 id2
1 2 1
1 3 2
1 4 3
1 5 4
I've never been even half good in advanced database queries -- how would I go about achieving this?
If you have exactly 2 rows (with 2 values for id1) for every different value of id2, you can use this:
SELECT MIN(id1) AS id1_a
, MAX(id1) AS id1_b
, id2
FROM tableX
GROUP BY id2 ;
You could try using
SELECT id2, GROUP_CONCAT(id1) FROM your_table
GROUP BY id2
This way you have, for each id2 value, a column with all id1 values comma separated.
Take a look at GROUP_CONCAT syntax.
This might not be the perfect solution but in your case, it should work. This is a trick I used.
select id1, (sum(id1) - id1 ) as nID1, id2 from table_name group by id2
Hope it works.
Ujjwal

Query two columns into one?

Is there a way to select these two colums:
Name 1 | Name 2
John Paul
Paul Ringo
Ringo George
So as to get both in one column with no repeated values:
Names
John
Paul
Ringo
George
SELECT name1 FROM theTable
UNION DISTINCT
SELECT name2 FROM theTable
It sounds like you can use:
SELECT Name1 FROM Table1
UNION SELECT Name2 FROM Table1
That will perform duplicate row removal by default, or you can make it explicit like this:
SELECT Name1 FROM Table1
UNION DISTINCT SELECT Name2 FROM Table1
See the docs for UNION for more information.
MySQL supports the UNION command. You just need to make sure the columns returned are the same.
E.g.
SELECT customerNumber id, contactLastname name
FROM customers
UNION
SELECT employeeNumber id,firstname name
FROM employees