select case when in MYSQL - mysql

I have 2 tables
First tabel name is "consumer"
id_consumer
name
1
Roy
2
Dori
3
Rico
Second tabel name is "consumer_address"
id_consumer
address
status
1
Street Avenue
1
1
Park Hill
0
2
Highwalk Street
1
2
Albion Place
0
Condition
name from tabel "consumer"
address from "consumer_address" , but i want to get only 1 address when consumer_address.status = 1
When Consumer not have data in tabel "consumer_address", field is NULL
The Final Tabel Like this
id_consumer
name
address
status
1
Roy
Street Avenue
1
2
Dori
Highwalk Street
1
3
Rico
NULL
NULL
i have query, but its not work
this is my query
SELECT
id_consumer,
name,
CASE WHEN (`consumer_address`.`status` = 1) THEN `consumer_address`.`address` ELSE NULL END as "Address",
CASE WHEN (`consumer_address`.`status` = 1) THEN `consumer_address`.`status` ELSE NULL END as "Status"
FROM consumer
JOIN consumer_address ON consumer_address.id_consumer = consumer.id_consumer
Thanks

Very simple solution:
SELECT
`id_consumer`,
`name`,
`consumer_address`.`address`,
`consumer_address`.`status`
FROM consumer
LEFT JOIN consumer_address ON
`consumer_address`.`id_consumer` = `consumer`.`id_consumer` AND
`consumer_address`.`status` = 1

Instead of using CASE WHEN just include the status in the JOIN.
Additionally, to keep consumer 3, you need a LEFT JOIN.
SELECT
id_consumer,
name,
`consumer_address`.`address`,
`consumer_address`.`status`
FROM
consumer
LEFT JOIN
consumer_address
ON consumer_address.id_consumer = consumer.id_consumer
AND consumer_address.status = 1

Related

Codeigniter - left join tables

i have a table for my sql like this :
result_podium
id_result_podium
id_race
id_rider
position
point
and
result_dnf
id_result_dnf
id_race
id_rider
So what im trying to do is to join those two table and shown it as one, and here is the result and what i have tried so far :
Attempt 1 :
$this->db->select_sum('result_podium.point');
$this->db->select('riders.name AS rider_name, teams.name AS team_name');
$this->db->from('result_podium');
$this->db->join('riders','riders.id_rider = result_podium.id_rider');
$this->db->join('result_dnf','result_dnf.id_rider = riders.id_rider', 'left');
$this->db->join('teams','teams.id_team = riders.id_team');
$this->db->where('riders.is_deleted','0');
$this->db->order_by("SUM(result_podium.point) DESC");
$this->db->group_by(array('result_podium.id_rider','result_dnf.id_rider'));
$query = $this->db->get();
return $query->result();
And the result is something like this :
No
rider
point
1
Nick
10
2
A
5
3
D
5
4
CC
4
5
B
4
it managed to get data from table result_podium but not managed to join data from table result_dnf, and here is my another attempt :
Attempt 2 :
$this->db->select_sum('result_podium.point');
$this->db->select('result_dnf.*, riders.name AS rider_name, teams.name AS team_name');
$this->db->from('result_dnf');
$this->db->join('riders','riders.id_rider = result_dnf.id_rider');
$this->db->join('teams','teams.id_team = riders.id_team');
$this->db->join('result_podium','result_podium.id_rider = riders.id_rider', 'left');
$this->db->where('riders.is_deleted','0');
$this->db->order_by("SUM(result_podium.point) DESC");
$this->db->group_by(array('result_dnf.id_rider','result_podium.id_rider'));
and here is the result :
No
rider
point
1
A
5
2
D
5
3
B
4
4
CC
4
5
Bri
6
Brum
the point data forBri and Brum, is null because they came from result_dnf, but i did not managed to get Nick data from result_podium.
And here what i was expecting for the data to shown in my view :
No
rider
point
1
Nick
10
2
A
5
3
D
5
4
CC
4
5
B
4
5
Bri
6
Brum
is there a way to join those two table?, anyhelp is really appreciated thank you!.

SQLAlchemy query construction

I'm working with a Key model backed by Postgres that is a generic table to hold API keys:
class Key(Model):
__tablename__ = "keys"
id = Column(Integer, primarykey=True)
user_id = Column(Integer, ForeignKey("users.id"))
brokerage_id = Column(Integer, ForeignKey("brokerages.id"))
account_id = Column(Integer, ForeignKey("accounts.id"))
key = Column(String(128))
value = Column(String(128))
In the below example, user 2 has three keys. All three are associated with brokerage 2 and account 2. This is represented by IDs 4 through 6. For this site, the user has an authentication token plus two query IDs.
id user_id brokerage_id account_id key value
--------------------------------------------------------------------
4 2 2 2 token 999999999999
5 2 2 2 query_id 888888
6 2 2 2 query_id 777777
7 1 2 3 token 444444444444
I am trying to construct a query so that my result will be modeled as such:
[(user_id, brokerage_id, account_id, token, [query_id_1, query_id_2, ...]), ...]
So for the above example, it would look like this
[(2, 2, 2, 999999999999, [888888, 777777]), (1, 2, 3, 444444444444, [])]
I've got the following queries which select the token and the query_ids
tokens = db.session.query(
Key.user_id, Key.brokerage_id, Key.account_id, Key.value
).filter(Key.key=='token').all()
query_ids = db.session.query(
Key.user_id, Key.brokerage_id, Key.account_id, Key.value
).filter(Key.key=='query_id').all()
I've tried using subquery in various ways but cannot quite get the output to resemble what I need. How can I construct a query to return results in a way that align to my list of tuples, above?
Result
Adding the final working query here thanks to #rfkortekaas
from sqlalchemy.orm import aliased
from sqlalchemy import func, and_
from project.models import Key
from project.extensions import db
key_token = aliased(Key)
q = db.session.query(
key_token.user_id,
key_token.brokerage_id,
key_token.account_id,
key_token.value.label('token'),
func.array_agg(Key.value).label('query_ids')
).join(
Key,
and_(
key_token.user_id == Key.user_id,
key_token.brokerage_id == Key.brokerage_id,
key_token.account_id == Key.account_id,
Key.key == 'query_id'
)
).filter(
key_token.key == 'token'
).group_by(
key_token.user_id,
key_token.brokerage_id,
key_token.account_id,
key_token.value
)
results = q.all()
You can use the array_agg function from PostgreSQL to create an array of the results:
from sqlalchemy.orm import aliased
key_token = aliased(Key)
stmt = select(key_token.user_id,
key_token.brokerage_id,
key_token.account_id,
key_token.value.label('token'),
func.array_agg(Key.value).label('query_ids')
).join(Key,
and_(key_token.user_id == Key.user_id,
key_token.brokerage_id == Key.brokerage_id,
key_token.account_id == Key.account_id,
Key.key == 'query_id'))\
.where(key_token.key == 'token')\
.group_by(key_token.user_id,
key_token.brokerage_id,
key_token.account_id,
key_token.value)
keys = session.execute(stmt).all()
for row in keys:
print(row)
Results in:
user_id
brokerage_id
account_id
token
query_ids
1
2
3
'44'
['4']
2
2
1
'33"
['6']
2
2
2
'99"
['8', '7]
For the following dataset:
user_id
brokerage_id
account_id
key
value
2
2
2
token
'99'
2
2
1
token
'33'
2
2
1
query_id
'6'
2
2
2
query_id
'8'
2
2
2
query_id
'7'
1
2
3
token
'44'
1
2
3
query_id
'4'

sql - get parentID with or without child in self refrenced table

Here is my table's (events) content. eventID is "primary key" and parentID is "foreign key" with references to events(eventsID)
self referenced table :
eventID eventName parentID appFK
1 evt1 null 2
2 evt2 1 1
3 evt3 1 1
4 evt4 null 3
5 evt5 8 3
6 evt6 null 4
7 evt7 null 1
8 evt8 null 1
and another table content (applications) like this :
appID appName
1 app1
2 app2
3 app3
4 app4
I'd like to fetch all eventIDs which are parents or not with a given appID. If a child has the given appID, i'd like to get his parentID and not himself. So the result is going to be like this with appID = 1 :
eventID eventName ParentID appFK
1 evt1 null 2 // parent event who has children with appID = 1
7 evt7 null 1 // event with no child and appID = 1
8 evt8 null 1 // parent event with appID = 1 and has a child
I tried lot of examples and read a lot of solutions here but i didn't find a problem like this. Can you help me to write the right SQL ?
thx.
Try this:
SELECT DISTINCT COALESCE(e2.eventID, e1.eventID),
COALESCE(e2.eventName, e1.eventName),
COALESCE(e2.appFK, e1.appFK)
FROM events AS e1
LEFT JOIN events AS e2 ON e1.parentID = e2.eventID AND e1.appFK = 1
WHERE (e1.appFK = 1 AND e1.parentID IS NULL) OR (e2.eventID IS NOT NULL)
The LEFT JOIN fetches parent records (e1.parentID = e2.eventID) of a child having appID = 1 (e1.appFK = 1).
The WHERE clause selects root records having appID = 1 and root records that are related to a child having appID = 1 (e2.eventID IS NOT NULL).
Demo here

MYSQL: Select specific column value depending on condition

I have table with columns
id doctor_name charges_cash charges_cashless
1 1 300 600
2 2 200 400
Now I am trying to run this query:
SELECT ipd.patient_name, r.room_name, doctor.doctor_name,
CASE p.tpa_name
WHEN NULL
THEN i.charges_cash
ELSE i.charges_cashless
END AS 'charges'
FROM `daily_ward_entry` d, ipd_charges i, ipd_patient_entry ipd, room_charges r,
patient_detail p, doctor
WHERE d.doctor_visit_name = i.doctor
AND r.id = d.room_name
AND d.patient_name = ipd.id
AND d.doctor_visit_name = doctor.id
I am getting the result for charges as 400 whereas p.tpa_name being null, I expect it to be 200,
I am out of any clue, what I am doing wrong here?
The result set is like this
patient_name room_name doctor_name charges
Sapna Agrawal MG-1 Dr. Dungri 400
Thanks.
You need the IS operator when comparing to NULL
CASE WHEN p.tpa_name IS NULL
THEN i.charges_cash
ELSE i.charges_cashless
END AS 'charges'

mysql,select query ,and or clause problem

i have a table named item with four attribute name,code,class,value
now i want to group them in following way:
group a: name='A',code=11,class='high',value between( (5300 and 5310),(7100 and 7200),(8210 and 8290))
group b: name='b',code=11,class='high',value between( (1300 and 1310),(2100 and 2200),(3210 and 3290))
how can i do it?
You might want to try something like this:
SELECT
CASE
WHEN code = 11 AND
class = 'high' AND
(code BETWEEN 5300 AND 5310 OR
code BETWEEN 7100 AND 7200 OR
code BETWEEN 8210 AND 8290)
THEN 'A'
WHEN code = 11 AND
class = 'high' AND
(code BETWEEN 1300 AND 1310 OR
code BETWEEN 2100 AND 2200 OR
code BETWEEN 3210 AND 3290)
THEN 'B'
ELSE Unknown
END AS name,
*
FROM your_table
ORDER BY name
You might wish to change ORDER BY to GROUP BY and you should be aware that BETWEEN includes both endpoints.
First group
select * from item
where name LIKE 'A'
and code LIKE '11'
and class LIKE 'high'
and (value BETWEEN 5300 AND 5310 OR value BETWEEN 7100 AND 7200 OR value BETWEEN 8210 AND 8290)
the same idea for group b