sql query to extract rows - mysql

table:
id user_id guessed_id result
1219 27 4 Y
1357 20 4 Y
2 3 5 N
9 20 5 N
1392 20 11 Y
1618 27 11 N
2471 20 25 Y
I would like to build a query that fetches only the guessed_ids that have exactly 2 result with the value Y.
In the above rows the query should return:
4
since the rows containing the guessed_id 4 have both Y in the column result.
guessed_id 5 should not be returned since it contains 2 N
11 should not be returned since it has a N
25 should not be returned since it has only one Y
I am using mysql DBMS.

A GROUP BY/HAVING will do what you want, just count the number of Y's and return the guessed_ids where the count is 2;
SELECT guessed_id
FROM myTable
WHERE result='Y'
GROUP BY guessed_id
HAVING COUNT(*)=2
An SQLfiddle to test with.
I'm not sure from your example whether any N results are allowed for a hit, I assumed they're not relevant as long as there are exactly 2 rows with a Y.

Related

MySQL: Get count for each range

There is mysql Ver 8.0.18 value_table as:
value count
1 3
11 1
12 2
22 5
31 1
34 3
35 1
40 3
46 7
What is query to get a total count for each dozen (1-10 - first dozen,11-20 - second , etc..)
as:
1 3
2 3
3 5
4 8
5 7
Query should be flexible, so when some records added to value_table , for example
51 2
62 3
so, it is not necessary to change a query by adding new range (51-60 - 6-th dozen, etc.)
I think you just want division and aggregation:
select min(value), sum(count)
from t
group by floor(value / 10);
To be honest, I'm not sure if the first column should be min(value) or floor(value / 10) + 1.

select all rows when its values smaller than specify number?

i have a table :
a b c
1 10 1001
7 6 54
56 2000 31
1200 5 9
4 10 20
2 65 20
how can i select rows with column's value of this row smaller than 1000. i want to get this
a b c
7 6 54
4 10 20
2 65 20
mysql query still get all value :
SELECT a,b,c FROM test
where a <'1000' or b<'1000' or c<'1000'
It sounds like you would like to pull a row where there is NO column greater than 1000 in that row, if that is correct then you need to us AND instead of OR.
SELECT a,b,c FROM test
where a <'1000' AND b<'1000' AND c<'1000'
Hope that helps!

Referencing values in a different column with matching name in MySQL

Getting stumped. How would I create a new column items_delta. I want items_delta to be a value that uses the column name, items, and last date. I need to find the delta between two different dates for the same name.
Ex: For name = A, the latest date is 12/14, and the items = 150. For name = A, I want the difference in items now compared to the items from the most previous date only. So essentially for name = A, I want (150 - 120)/120 = 0.25 which is the difference from the two latest dates.
Test Table
id name items last_date
1 A 150 12/14/15
2 B 100 12/14/15
3 C 50 12/14/15
4 A 120 12/13/15
5 B 110 12/13/15
6 B 90 12/12/15
7 A 110 12/12/15
8 A 100 12/11/15
9 A 150 12/10/15
10 C 35 12/10/15
What I want:
Test Table 2
id name items last_date items_delta
1 A 150 12/14/15 0.25
2 B 100 12/14/15 -0.09
3 C 50 12/14/15 0.43
Relatively new to mySQL, please be gentle.

Cannot print out the latest results of table

I have the following table:
NAMES:
Fname | stime | etime | Ver | Rslt
x 4 5 1.01 Pass
x 8 10 1.01 Fail
x 6 7 1.02 Pass
y 4 8 1.01 Fail
y 9 10 1.01 Fail
y 11 12 1.01 Pass
y 10 14 1.02 Fail
m 1 2 1.01 Fail
m 4 6 1.01 Fail
The result I am trying to output is:
x 8 10 1.01 Fail
x 6 7 1.02 Pass
y 11 12 1.01 Pass
y 10 14 1.02 Fail
m 4 6 1.01 Fail
What the result means:
Fnames are an example of tests that are run. Each test was run on different platforms of software (The version numbers) Some tests were run on the same platform twice: It passed the first time and failed the second time or vice versa. My required output is basically the latest result of each case for each version. So basically the results above are all unique by their combination of Fname and Ver(sion), and they are selected by the latest etime from the unique group.
The query I have so far is:
select Fname,stime,max(etime),ver,Rslt from NAMES group by Fname,Rslt;
This however, does not give me the required output.
The output I get is (wrong):
x 4 10 1.01 Fail
x 6 7 1.02 Pass
y 4 12 1.01 Pass
y 10 14 1.02 Fail
m 1 6 1.01 Fail
Basically it takes the max time, but it does not really print the correct data out, it prints the max time, but it prints the initial time of the whole unique group of data, instead of the initial time of that particular test (record).
I have tried so long to fix this, but I seem to be going no where. I have a feeling there is a join somewhere in here, but I tried that too, no luck.
Any help is appreciated,
Thank you.
Use a subquery to get the max ETime by FName and Ver, then join your main table to it:
SELECT
NAMES.FName,
NAMES.STime,
NAMES.ETime,
NAMES.Ver,
NAMES.Rslt
FROM NAMES
INNER JOIN (
SELECT FName, Ver, MAX(ETime) AS MaxETime
FROM NAMES
GROUP BY FName, Ver
) T ON NAMES.FName = T.FName AND NAMES.Ver = T.Ver AND NAMES.ETime = T.MaxETime
You could first find which is the latests=max(etime) for each case for each version ?
select Fname,Ver,max(etime) from NAMES group by Fname,Ver;
From there you would display the whole thing via joining it again?
select *
from
NAMES
inner join
(select Fname,Ver,max(etime) as etime from NAMES group by Fname,Ver ) sub1
using (Fname,Ver,etime)
order by fname,Ver;

How to find matrix element with sql query?

I have array and table that I referenced some elements in array. Like my array
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
And I have area like Start point s=9,X=2,Y=2,Row Count R=6
then I have boxes 9,10,11,15,16,17,21,22,23
Now I am trying to write some sql that check if 16 number in this area.I created some logic like if ((s<16<s+X) || (s+6<16<s+x+6) || (s+12<16<s+x+12) ) but should I write it in one sql query? I am using mySql.
This doesn't have anything to do with SQL, I don't think, but something like the following condition is probably what you want. Since your example has the same value of X and Y, and "Row Count" sounds more like "number of rows" than "number of items in a row," I may have gotten rows and columns backwards from what you want.
set #s=9, #x=2, #y=5, #R=6, #testval=16;
(#testval-1)/#R between (#s-1)/#R and (#s-1)/#R - #y - 1
and (#testval-1)%#R between (#s-1)%#R and (#s-1)%#R - #x - 1