Join two queries to get particular table format - mysql

I have two tables X and Y
SELECT ser.entity_type,ser.param_name
FROM X as ser
where
ser.group_name like '%CRVJL%'
and ser.param_name like '%CBV%'
and ser.entity_type like '%SIGNON%'
SELECT reg.entity_type
FROM REGISTER_TB as reg
where
reg.group_name like '%CRVJL%' and reg.PROV_NAME like'%CBV%' and reg.PROV_NAME like'%-OD-%' and reg.ENTITY_TYPE like '%SIGNON%'
I get some values from table x lets say 4 rows,i have to check if they are present in y or not if not then as -1 and if present then +1
Table should look like this
entity type param_name status
SIGNON CBV6815_I-OD-I 1
SIGNON CBV7815_I-OD-I 1
SIGNON CBV8815_I-OD-I -1
SIGNON CBV9815_I-OD-I 1
we have equal columns
X Y
entity_type entity_type
param-value prov_name
I tried using left join union and right join but its not giving me output as i want.Can anybody help me?

Use this sort of query, you'll have to replace table/field name to match your schema
SELECT a.entity_type, a.param_name, IF(b.status IS NULL, -1, 1) AS 'status'
FROM
X AS a
LEFT OUTER JOIN Y AS b ON a.entity_type=b.entity_type AND a.group_name=b.group_name
WHERE
a.group_name LIKE '%CRVJL%'
AND a.param_name LIKE '%CBV%'
AND a.entity_type LIKE '%SIGNON%'
AND b.prov_name LIKE '%CBV-OD-%'

Related

specify location name if point intersects it MySQL

I am using MySQL 8.0
I have two tables: location_table, rent_table
location_tables looks like:
location_name polygon
A BLOB
B BLOB
...
polygon is POLYGON datatype.
rent_table looks like:
user_id rent_time rent_location
1 x BLOB
2 x BLOB
...
where rent_location is POINT data type
for each row in rent_table I want to create a column that indicate which location_name it belongs to. If user_id rent_location intersects location_name = A new column would have A
It would look something like this:
user_id rent_time rent_location location_name
1 x BLOB A
2 x BLOB B
...
Thanks in advance!
What I've tried:
I can do one by one by using
select *
, st_intersects(A, Point(ST_X(work_location), ST_Y(work_location))) as location_A
, st_intersects(B, Point(ST_X(work_location), ST_Y(work_location))) as location_B
, st_intersects(C, Point(ST_X(work_location), ST_Y(work_location))) as location_C
from rent_table;
This works when I set A,B,C variable beforehand but I want to get location polygon directly from location_table.
I could use subquery like below:
select *
, st_intersects((select polygon from location_table where location_name = 'A'), Point(ST_X(work_location), ST_Y(work_location))) as location_A
from rent_table;
however I have millions of rows in rent_table therefore I do not want subquery in select statement to run for each of million rows.
You can do:
select
r.*,
l.location_name
from rent r
left join location l on ST_CONTAINS(l.polygon, r.rent_location)

SQL query statement Self Join?

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

How to join columns of same table under different query conditon

I am explaining here I have table name TB which contains three columns and data like this
Cl1. Cl2 Cl3
0. X. A
0. Y. B
0. Z. C
1. X. a
1. X. b
1. X. c
1. Z. d
And I output like this
Cl1. Cl2. Cl3. No
0. X. A. 3
0. Y. B. 0
0. Z. C. 1
Here no column shows repetition of Cl2. Value when Cl1.=1 with respective value of Cl2. When Cl1. = 0 means for Cl1. = 1 here we can see for Cl.=1 and Cl2.=X it's value is repeated twice for Cl1.=1 similar when no match found for Cl1.=1 and Cl2.=Y no gives 0
I hope i have described my problem
I did many attempts but I didn't get any solution. My problem goes out of mind.
My few attempts are like
Select Cl1. ,Cl2. ,Cl3. ,IF(Cl2.=(Select Cl2. From TB where Cl1.=1),Cl2. ,0) as no.
From TB
where Cl2.=0
I also try join type statement for the same table but it also didn't help me.
Please guys help me. I am new on stack overflow if I did any mistake in describing my question then sorry
Try this, #Rajesh
SELECT
t1.cl1,
t1.cl2,
t1.cl3,
IFNULL(cnt, 0) as No
FROM
(
SELECT cl1, cl2, cl3 FROM tb WHERE cl1=0
) AS t1
LEFT OUTER JOIN
(
SELECT cl2, COUNT(cl2) AS cnt FROM tb WHERE cl1 <> 0 GROUP BY cl2
) AS t2
ON t1.cl2=t2.cl2

SQL unwanted results in NOT query

This looks like it should be really easy question, but I've been looking for an answer for the past two days and can't find it. Please help!
I have two tables along the lines of
texts.text_id, texts.other_stuff...
pairs.pair_id, pairs.textA, pairs.textB
The second table defines pairs of entries from the first table.
What I need is the reverse of an ordinary LEFT JOIN query like:
SELECT texts.text_id
FROM texts
LEFT JOIN text_pairs
ON texts.text_id = text_pairs.textA
WHERE text_pairs.textB = 123
ORDER BY texts.text_id
How do I get exclusively the texts that are not paired with A given textB? I've tried
WHERE text_pairs.textB != 123 OR WHERE text_pairs.textB IS NULL
However, this returns all the pairs where textB is not 123. So, in a situation like
textA TextB
1 3
1 4
2 4
if I ask for textB != 3, the query returns 1 and 2. I need something that will just give me 1.
The comparison on the second table goes in the ON clause. Then you add a condition to see if there is no match:
SELECT t.text_id
FROM texts t LEFT JOIN
text_pairs tp
ON t.text_id = tp.textA AND tp.textB = 123
WHERE tp.textB IS NULL
ORDER BY t.text_id ;
This logic is often expressed using NOT EXISTS or NOT IN:
select t.*
from texts t
where not exists (select 1
from text_pairs tp
where t.text_id = tp.textA AND tp.textB = 123
);

MySQL - return records for clients unless they have a specific value and only that value

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)