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?
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 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;
I have a table which has month data in INT April=4, May=5 and so on. I want those records which have continuous data. My table is as follows. So if a record has discontinuous data, it should not be returned.
Continuous data means those records which having continuous four month. If records are there for 4,5,6,7 or 5,6,7,8 or 6,7,8,9 months then that record should come in result of a ID. If there records for a ID has 4,5,8,9 in month field this is discontinue data for me.
Initial query:
select ID, month from table1 where month in (4,5,6,7,8,9) group by month;
Initial Data:
PK ID month value
1 1 4 400
2 1 5 200
3 1 6 300
4 1 7 400
5 2 5 400
6 2 6 200
7 2 7 100
8 2 8 400
9 3 4 200
10 4 5 800
11 5 6 800
12 5 7 100
13 5 8 700
14 5 9 900
15 6 4 100
16 6 5 200
17 6 8 500
18 6 9 600
Result:
PK ID month value
1 1 4 400
2 1 5 200
3 1 6 300
4 1 7 400
5 2 5 400
6 2 6 200
7 2 7 100
8 2 8 400
11 5 6 800
12 5 7 100
13 5 8 700
14 5 9 900
My database is MySQL.
I have months from April(4) to Sept(9)
Try the below query this will work I hope!
SELECT ID, Month
FROM table1 WHERE month in (4,5,6,7,8,9)
GROUP BY ID, Month
HAVING count(ID)>3
kindly let me know this is working or not
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.