I have a table like this:
id c_id time value
1 4 1 12
2 4 2 5
3 4 3 6
4 4 4 48
5 4 5 1
6 4 6 121
7 5 1 121
8 5 2 321
9 5 3 2
10 5 4 1
11 5 5 54
12 5 6 4546
13 5 7 78
14 5 8 784
15 5 9 1
Now I want a table like this with a SELECT command:
time1 value1 time2 value2
1 12 1 121
2 5 2 321
3 6 3 2
4 48 4 1
5 1 5 54
6 121 6 4546
0 0 7 78
0 0 8 784
0 0 9 1
time1 and value1 is from the data with c_id=4,
time2 and value2 is from the data with c_id=5
Is it possible to create a SELECT command to do that?
I hope you can help
Yiu can use an inner join
select a.time as time1, a.value as value1, b.time as time2, b.value as value2
from my_table as a
inner join my_table as b on a.time = b.time
and a.c_id= 4
and b.c_id= 5;
Related
i have a table named test with the below structure like this
id mark join_id
1 5 1
2 4 1
3 9 1
4 5 2
5 7 2
6 12 2
i want to write a query that can get me the average of the marks from the beginning record to this record with the desired result as below
id mark join_id avg_of_previous_marks
1 5 1 5
2 4 1 4.5
3 9 1 6
4 5 2 5.75
5 7 2 6
6 12 2 7
i wrote this query but it doesn't seem to work correctly
SELECT test.id, test.mark, test.join_id, test_avg.avg_of_previous_marks FROM test
LEFT JOIN (SELECT id, join_id, AVG(mark) as avg_of_previous_marks FROM test GROUP BY
join_id) test_avg
ON test_avg.join_id = test.join_id AND test_avg.id <= test.id
and it gives this resault
id mark join_id avg_of_previous_marks
1 5 1 6
2 4 1 6
3 9 1 6
4 5 2 8
5 7 2 8
6 12 2 8
Its a simple running total that you need.
select id,mark,join_id, avg(mark) over (order by id) avg_of_previous_marks from test_avg ;
fiddle here
I've tried an sql-query like this:
SELECT A.id, FROM_UNIXTIME(A.timestamp) AS timestamp, A.test, B.test2, B.test3, C.test4
FROM table AS A
INNER JOIN table AS B
ON A.id = B.id - 3
INNER JOIN table AS C
ON A.id = C.id - 6
ORDER BY id DESC
To merge different rows from one table together.
But it is very slow. Is there an way to speed it up in MySQL itself. I want to create an view to select the data from tableau (tableau itself has no preprocessing).
With an postprocessing script it would be very easy (just save up to six old columns and read it back six rows later). Shouldn't such an easy task possible with SQL too?
Example:
Original Table
id
timestamp
test
test2
test3
test4
1
…
1
1
1
1
2
…
2
2
2
2
3
…
3
3
3
3
4
…
4
4
4
4
5
…
5
5
5
5
6
…
6
6
6
6
7
…
7
7
7
7
8
…
8
8
8
8
9
…
9
9
9
9
10
…
10
10
10
10
11
…
11
11
11
11
12
…
12
12
12
12
13
…
13
13
13
13
Needed result:
id
timestamp
test
test2
test3
test4
1
…
1
null
null
null
2
…
2
null
null
null
3
…
3
null
null
null
4
…
4
1
1
null
5
…
5
2
2
null
6
…
6
3
3
null
7
…
7
4
4
null
8
…
8
5
5
1
9
…
9
6
6
2
10
…
10
7
7
3
11
…
11
8
8
4
12
…
12
9
9
5
13
…
13
10
10
6
ID is an unique autoincrement value.
I have table structure like where Loanid is Foreign Key
TranID LOANID TRANSDATE
2 2 2013-05-01
13 2 2013-05-10
14 2 2013-05-15
6 5 2013-05-01
7 5 2013-06-10
8 5 2013-06-14
9 5 2013-07-01
10 5 2013-07-10
i need a query to calculate Days between like below .
TranID LOANID TRANSDATE DAYS_BETWEEN
2 2 2013-05-01 9
13 2 2013-05-10 5
14 2 2013-05-15 0
6 5 2013-05-01 41
7 5 2013-06-10 4
8 5 2013-06-14 17
9 5 2013-07-01 9
10 5 2013-07-10 0
Possibly a self join, using MIN to get the next date.
SELECT t1.tranid,
t1.loanid,
t1.transdate
DATEDIFF(IFNULL(MIN(t2.transdate), t1.transdate), t1.transdate) AS days
FROM some_table t1
LEFT OUTER JOIN some_table t2
ON t1.loanid = t2.loan_id
AND t1.transdate < t2.transdate
GROUP BY t1.tranid,
t1.loanid,
t1.transdate
tables i am using
product_parameter
id productid parameterid value
1 1 1 10
2 2 2 11
3 1 2 34
4 2 4 44
5 3 2 55
6 3 3 43
7 4 1 33
8 1 3 33
9 1 4 24
and so on i want to display in form
parameterid 1 2 3 4. . .
productid
1 43 34 33 24
2 null 11 null 44
3
4
.
.
and so on rows and columns are not fixed
tables i am using
product_parameter
Sorry wanted to correct the output format i needed after query
id productid parameterid value
1 1 1 10
2 2 2 11
3 1 2 34
4 2 4 44
5 3 2 55
6 3 3 43
7 4 1 33
8 1 3 33
9 1 4 24
and so on i want to display in form
parameterid 1 2 3 4. . .
productid
1 43 34 33 24
2 null 11 null 44
3
4
.
.
and so on... rows and columns values are not fixed
It would be much easier to create 2D array in application code.
In SQL you have to add a join for each column.
I have the following data:
id1,id2
1 3
1 8
1 10
1 11
2 3
2 10
2 11
3 2
3 18
3 20
4 3
4 8
5 3
5 10
5 11
5 40
5 45
5 50
6 1
6 59
6 70
I won't get all id1 with id2 = 3,10,11.
For example, id1=4 only with id2=3, should not return.
The results should be
id1
1
2
5
SELECT distinct(ID1) FROM TBTEST WHERE ID2 IN(3,10,11)
SQL code
SELECT ID1,COUNT(ID2) FROM TBTEST
WHERE ID2 IN(3,10,11)
GROUP BY ID1
HAVING COUNT(ID2)=3
Is this what you need?