I have a table ABR with three columns: two lookup values, A and B and a result Result. I want the result to be returned given A and B
But, for some values of A, column B can be a wildcard. In that case I want the most specific row.
I can't seem to figure out the MySQL statement though - I found one, but it was 7 lines long and had 5 = and two IF's, there must be a better way.
A B Result
1 * First
2 * Second
2 1 Third
2 2 Fourth
3 * Fifth
Example wanted results:
A=1, B=5 -> First
A=1, B=0 -> First
A=2, B=2 -> Fourth
A=2, B=4 -> Second
A=3, B=7 -> Fifth
Do notice that the column values can change later: we can add extra rows for A and B, or remove values. Hardcoding stuff in the SQL is not acceptable.
SELECT Result
FROM ABR
WHERE A = #LookupA
AND (B = #LookupB OR B = '*')
ORDER BY B DESC LIMIT 1;
Related
I am trying to query a table with millions of rows using PHP not SQL.
** to Select column B value from table where column A=1 & column C=1 if said value is not present wherever column A=2 in a MYSQL table **
In the example table the requested B value is 5.
I want to get a value from column B with a column A value of 1 and column C value of 1 if this column B value is not also present where column A value is 2.
Thanks.
Table
+-+-+-+
|A|B|C|
+-+-+-+
|1|3|0|
+-+-+-+
|1|6|0|
+-+-+-+
|1|5|1|
+-+-+-+
|2|3|1|
+-+-+-+
|2|4|1|
+-+-+-+
|3|3|0|
+-+-+-+
|1|3|1|
+-+-+-+
Select B where A=1, C=1 and wherever A is not equal to 2.
* 1st and 2nd row A=1 but C=0 so this is not answer,
* 3rd row, both A and C are equal to 1 and B= 5 , since there is not B=5 where A=2 this is the answer.
* 4th, 5th and 6th row are not right answer because A is not equal to 1
* 7th row is not an answer because B=3 is also present when A=2
What you need to do is add to your where clause to exclude the values of B that are the result of a subquery:
SELECT `B` FROM `my_table` WHERE `A`=1 AND `C`=1 AND `B` NOT IN (SELECT `B` FROM `my_table` WHERE `A`=2);
This just gets a list of the cases where B is found with A=2, and then excludes them from the main query.
new to SQL.
I have the following set of data
A X Y Z
1 Wind 1 1
2 Wind 2 1
3 Hail 1 1
4 Flood 1 1
4 Rain 1 1
4 Fire 1 1
I would like to select all distinct 'A' fields where for all rows that contain A have flood and rain.
So in this example, the query would return only the number 4 since for the set of all rows that contain A = 4 we have Flood and Rain.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
Please let me know if you need further clarification.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
You can use aggregation, and filter with a having clause:
select a
from mytable t
where x in ('Flood', 'Rain') -- either one or the other
having count(*) = 2 -- both match
If tuples (a, x) tuples are not unique, then you want having count(distinct x) = 2 instead.
You Shooud use count(distinct X) group by A and having
count(distinct...) avoid situation where you have two time the same value for X
select A
from my_table
WHERE x in ('Flood', 'Rain')
group A
having count(distinct X) = 2
Example:
If my data set is
A B C
1 2 3
5 6 7
4 5 6
I could have "1", "5" and "4" show up by typing =query(A:C, select A).
I could have "1" and "4" show up by typing =query(A:C, select A where B < 6).
Lets say I wanted to query only entries that appeared after a certain row. In this case, row 3 is 4, 5, 6. So if I want only results that are row 3 or below, I could add a fourth column D somewhere, fill column D with =row(), and then have only **** show up by typing
=query(A:C, select A where D >= 3).
But I don't want to have to add a fourth column somewhere and fill it with the =row() formula. The query should be able to do this on its own.
Query parametres
try:
=QUERY(A:C,"select * offset 2",0)
offset parameter is zero base:
0 -- start from row 1
1 -- start from row 2
2 -- start from row 3
so on
You may find more usuful query tips here. Use special words: offset, limit, skipping. For example, to select only odd rows use:
=QUERY(A:C,"select * skipping 2",0)
Filter function
To have full control of rows you select, use this construction:
=filter(A:C,isodd(row(A:C))) -- only odd rows
=filter(A:C,row(A:C)=3) -- only 3-d row
=filter(A:C,row(A:C)>=3) -- all rows >= 3-d row
=query(filter(A:C,row(A:C)>=3),"select *") use filter + query
So, I had an issue like this, and here is what I did. I made a virtual column.
=query(A:C, select A where D >= 3) would then be something like
=query({A:C, ROW(A:C)}, "Select Col1 where Col4 >=3")
You have to make an array to query a column you do not want in your sheet. In doing so you can no longer use Column letters, so then A, B, C then becomes Col1, Col2, Col3 and so on.
Trying to wrap my head around how to do this query - I want to return a list of client records and need to exclude clients if they had only a specific value and no other values.
For example
c# value
1 X
1 Y
2 X
3 Y
I want all the records for clients 1 and 3, since they had a value other than X. I do not want client 2, because that client had ONLY X.
I for example want returned in this case:
1 X
1 Y
3 Y
Of course, I could have lots of other records with other client id's and values, but I want to eliminate only the ones that have a single "X" value and no other values.
Maybe using a sub-query?
Try this:
SELECT client, value FROM myTable where `client` in
(select distinct(client) from myTable where value !='X');
Returns:
Client Value
1 X
1 Y
3 Y
Something like this
SELECT ABB2.*
FROM
mytable AS ABB2
JOIN
(SELECT c
FROM mytable
WHERE value <> "X"
GROUP BY c) AS ABB1 ON ABB1.c = ABB2.c
GROUP BY ABB2.c, ABB2.value
It's faster than using a WHERE clause to identify the sub query results (as in Mike's answer)
Hi I have a table with name test. it got 7 columns id , a , b , c , d , e , f. All this columns contains either 1 or 0. Now i want make a query where i can choose only those columns whose value is 1.
Something like this:
select (condition) from test where id = 5;
because i have a hotel table with 50 columns out of which 11 columns contains either 1 or 0 representing the facilities of the hotel. I want to make a query which just tells what are the facilities of the hotel.
Any help would be great.
select id, (a*64)+(b*32)+(c*16)+(d*8)+(e*4)+(f*2)+(g*1)
from test
this number you can reverse it to convert to a 7 digit binary code.
examples:
18 = 0010010 , 1000000 = 64
using sql you can select rows, NOT columns
If it is that what you want you can bulid your query like this
select id, a, b, c, d -- columns to select
from test -- table
where (a = 1 or b=1 or c = 1 or d = 1) -- these are the conditions