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
Related
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
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.
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
Is it possible to use group by for two columns where they can be group either way?
Example:
MyTable
Column1 | Column2
1 2
2 1
1 3
3 1
3 2
4 5
Result:
Column1 | Column2
1 2
1 3
3 2
4 5
As you can see, it groups the 2 columns even though interchanged. but should still get the rows that doesn't have a partner.
select
least(Column1, Column2),
greatest(Column1, Column2)
from
Table1
group by
least(Column1, Column2),
greatest(Column1, Column2)
see it working live in an sqlfiddle
read more about the functions here
SELECT * FROM table WHERE id != 4;
SELECT * FROM table WHERE NOT id = 4;
SELECT * FROM table WHERE id <> 4;
I've got this all working but I also have to choose another field (or more fields) to decide what rows are returned.
How can I get this working?
If you want to 'deselect' columns where both conditions are true (ID1 is 4 and ID2 is 7), use something like:
select * from TBL where ID1 <> 4 or ID2 <> 7;
ID1 ID2 selected
--- --- --------
4 7 no
4 1 yes
1 7 yes
1 1 yes
If you want to 'deselect' columns where either condition is true (ID1 is 4 or ID2 is 7), use something like:
select * from TBL where ID1 <> 4 and ID2 <> 7;
ID1 ID2 selected
--- --- --------
4 7 no
4 1 no
1 7 no
1 1 yes
This can be extended to more conditions simply by adding them to the end of the where clause (and changing both/either to all/any in the text).
select * from albums where idAlbum!= 4 and idAlbum!= 8 I just solve my problem. Thanks for the help guys!