group by based on two columns with same but vice versa values - mysql

What I Have:
I have a table where I have two basic columns on which the query is supposed to work
c1|c2
1 2
2 1
1 3
3 1
2 4
Now I want the result after the execution of the query is this
c1|c2
2 1
3 1
Notice
(3,1) was the last occurence of 1 and 3 no matter in which order they are as along as it is 1 and 3 and same is (2,1)
How can I achieve this where one value of the two columns is static for example in this case (1) is static and I want only those rows which has (1) in any of the two columns in their last occurrence.. Can any one help ?

Related

Сoncatenation of rows into one table

I have a table with 2 columns, one of them is categories:
Category
AvgSum
1
10
2
100
3
1000
4
10000
5
100000
And I need to unite the second and the third row, also the fourth and fifth, and in the received lines perform the operation AVG
So the table has to be like this:
Category
AvgSum
1
10
2
550
3
60000
Any thoughts?
SQL tables represent unordered sets. You can use a case expression to define the groups:
select (case when category in (2, 3) then 2
when category in (4, 5) then 3
else category
end) as new_category,
avg(avgsum) as avgsum
from t
group by new_category;

Generate numbers based on a table column mysql

I have a table with the following rows:
ID Description Number
1 Test 1 4
2 Test 2 3
3 Test 3 5
4 Test 5 6
How do I create my query so that if I want ID 3, it generates the following based on the Number column:
Count
1
2
3
4
5
Thanks. :)
It looks like your rows are already uniquley identified. You need to query the row with id 3, and then preform an operation with php to count out to the end of the number set. 5 in this case. You could use arrays, and just loop throjgh the array for each number as well.

Resequence column numbers in MySQL

I have a table that has a column with numbers in a sequence, but for some numbers they can be in groups with several of the same number. Then when the user deletes out a group, there can be a gap in the numbers.
What I want to do is re-sequence that column so it starts at 1, then proceeds up to the last column. So for example, the table column named Item could look like this:
1
1
2
3
5
5
7
8
8
and I want it to convert to this:
1
1
2
3
4
4
5
6
6
Is there a way to do this in MySQL?
I wouldn't encourage to do this, but you can re-number your values after each update
set #cnt = 0;
update test t set t.number=#cnt:=#cnt+1;
Update: this will increment the number field by one

select multiple rows on nested condition in mysql database

select multiple rows on nested condition and nested limit in mysql database
Actually i have one table like:
id name levels
1 Red 1
2 Red Light 1
3 Green 2
4 Green Light 2
5 Blue 3
6 Blue Light 3
7 Blue Dark 3
I want fetch any rows according to levels with any limits Like:
From Level-1 = 1 row
From Level-2 = 2 row
From Level-3 = 2 row
total rows are 5 according to levels.
Any one know about single query is possible?
or
three foreach loop will be implement?

Is this pattern reconstitution or what is the name for this problem?

I've following problem and don't know the terminology to describe it and hence search for possible solutions.
I have a pivot table (matrix), eg each row and column have a named header. there is a defined set for rows and columns. Now let's assume that 10 rows are "combined" meaning each column is summed up to create a new "pattern".
What I would like is a way to determine alternative row combinations that lead to the same or similar "combined" pattern.
1 1 1
5 5 5
"Combined"
6 6 6
alternate row combination:
2 2 2
4 4 4
Suggestions? How is this problem called?
http://en.wikipedia.org/wiki/System_of_linear_equations#Matrix_equation
I just have to transpose above matrix to get Matrix A
[code]
1 5
1 5
1 5
[/code]
combined matrix is a vector b:
[code]
6
6
6
[/code]
and x would be just a vector full of 1.