How to modify my query for the below requirement? - mysql

This is the query where I can the order_seq_num as 2 but I want to get value from the column test_group_id from the t_patient_mhc_step_group_mapping for selected order_seq_num if the order_seq_num selected the row value 2 in t_patient_mhc_step_mapping.
SELECT
IF((labrep.lab_status_id = 2
&& labrep.mhc_status = 'MHC'),
MIN(stepmap.order_seq_num) + 1,
MIN(stepmap.order_seq_num)) AS step_order_seq_num
FROM
`t_patient_mhc_step_group_mapping` stepgroup
INNER JOIN
t_patient_mhc_step_mapping stepmap ON stepgroup.step_id = stepmap.step_id
AND stepmap.mhc_step_status = 1
INNER JOIN
`t_patient_mhc_mapping` mhcmap
INNER JOIN
`t_lab_test_report` labrep ON labrep.op_ip_appt_id = mhcmap.appt_id
WHERE
mhcmap.appt_id = 81

Related

Sum of columns using join

SELECT
td.EstimatedTotalAmount_Car,
AVG(tdca.EstimatedTotalAmount) AS policy_saving
FROM Company_Reporting td
INNER JOIN Company_Car_Archive tdca
ON tdca.VehClassSize = td.VehClassSize_Car
AND tdca.VendorName != td.VendorName_Car
AND tdca.USER_SEARCH_ID = td.SEARCH_ID_Car
WHERE td.Company_id = 12
AND td.CHECKOUT_STS = 1
AND td.Check_Type = 3
AND td.policy_violate_flag = 0
GROUP BY td.ID
Above SQL is returning the below output.
EstimatedTotalAmount_Car policy_saving
55.86 75.197856
55.86 75.197856
170.83 244.070000
143.66 132.120000
171.96 131.007272
222.66 226.162725
97.03 79.554001
56.61 74.213999
But I want the sum of both columns in the single record.
sum(EstimatedTotalAmount_Car): 974.47 sum(policy_saving):1037.523709
How can I achieve this?
You can do this in outer query like
select sum(EstimatedTotalAmount_Car),
sum(policy_saving)
from(
/** your query */
) t
Just wrap your current query and sum both columns:
SELECT
SUM(EstimatedTotalAmount_Car) AS grand_total,
SUM(policy_saving) AS sum_policy_saving
FROM
(
SELECT
td.EstimatedTotalAmount_Car,
AVG(tdca.EstimatedTotalAmount) AS policy_saving
FROM Company_Reporting td
INNER JOIN Company_Car_Archive tdca
ON tdca.VehClassSize = td.VehClassSize_Car AND
tdca.VendorName != td.VendorName_Car AND
tdca.USER_SEARCH_ID = td.SEARCH_ID_Car
WHERE
td.Company_id = 12 AND
td.CHECKOUT_STS = 1 AND
tgd.Check_Type = 3 AND
td.policy_violate_flag = 0
GROUP BY td.ID
) t;

Counting entry in a column MySQL and display counts

I would want to count entries in a column and display the count beside it.
However, I'm clueless on how can I do it.
Desired output:
arrangement_number tray_no rl_type flag(count of occurrence)
------------------ ------- ---- ----
2774818 381001 R 3
2774818 381001 R 3
2774818 381001 L 3
2778470 405128 R 1
2779702 265265 R 2
2779702 265265 R 2
I'm currently trying queries using #variables but I still cant get it right.
each row are unique and I need them not to be grouped.
Update: Expanded Table added source code
Note: I'm currently joining 5 tables now
Actual Query:
SELECT
log.arrangement_number,
header.tray_number,
detail.rl_type,
-- some more fields here
FROM
log
INNER JOIN
header ON log.arrangement_number = header.rxarrangement_number
AND log.production_place_code = header.production_place_code
INNER JOIN
detail ON log.arrangement_number = detail.rxarrangement_number
AND log.production_place_code = detail.production_place_code
INNER JOIN
deliveryperiod ON log.arrangement_number = deliveryperiod.arrangement_number
AND log.production_place_code = deliveryperiod.production_place_code
AND detail.rl_type = deliveryperiod.rl_type
INNER JOIN
calc ON calc.arrangement_number = log.arrangement_number
AND calc.production_place_code = log.production_place_code
AND deliveryperiod.rl_type = calc.rl_type
AND detail.rl_type = calc.rl_type
WHERE
header.status_code IN ('20' , '21')
AND log.process_code = '12'
AND deliveryperiod.process_code_current = '12'
AND deliveryperiod.sub_process_code_current IN ('100' , '105')
AND lot_number = '120131'
ORDER BY log.lot_number , log.sequence_number , deliveryperiod.rl_type DESC
SELECT t1.tray_no,
t2.flag
FROM yourTable
INNER JOIN
(
SELECT tray_no, COUNT(*) AS flag
FROM yourTable
GROUP BY tray_no
) t2
ON t1.tray_no = t2.tray_no
try this...
SELECT tray_no, COUNT(*) 'flag'
FROM table1
GROUP BY tray_no

Decide to do LEFT JOIN on which table based on CONDITION

I have two tables. I want to left join with one of the tables based on certain condition. I want to do something like this:
IF (Projects.paymentType==1)
LEFT JOIN ProjectBudgetFixedPrice ON (Projects.budget=ProjectBudgetFixedPrice.id)
ELSE
LEFT JOIN ProjectBudgetPerHourPrice ON (Projects.budget=ProjectBudgetPerHourPrice.id)
I have created the following, but it is not working.
SELECT * FROM Projects LEFT JOIN
(IF (Projects.paymentType==1,ProjectBudgetFixedPrice,ProjectBudgetPerHourPrice))
ON
(IF (Projects.paymentType==1,Projects.budget=ProjectBudgetFixedPrice.id,Projects.budget=ProjectBudgetFixedPrice.id))
WHERE (Projects.statusCode=3 OR Projects.statusCode=4) AND Projects.suspended='N' AND (Projects.paymentType=1 OR Projects.paymentType=2)
Any help or suggestions please?
You should be able to use a UNION.
Something like
SELECT
*
FROM
Projects
LEFT JOIN
ProjectBudgetFixedPrice
ON Projects.budget = ProjectBudgetFixedPrice.id
WHERE
Projects.paymentType = 1
AND ((Projects.statusCode = 3 OR Projects.statusCode = 4)
AND Projects.suspended = 'N'
AND (Projects.paymentType = 1 OR Projects.paymentType = 2))
UNION
SELECT
*
FROM
Projects
LEFT JOIN
ProjectBudgetPerHourPrice
ON Projects.budget = ProjectBudgetPerHourPrice.id
WHERE
Projects.paymentType <> 1
AND ((Projects.statusCode = 3 OR Projects.statusCode = 4)
AND Projects.suspended = 'N'
AND (Projects.paymentType = 1 OR Projects.paymentType = 2));
HTH

updating in a query with joins. Counter to be increased by number of rows

I have written a following query.
UPDATE
tbl_bookings tb
INNER JOIN
tbl_slots ts
ON ( tb.slot_id = ts.id )
SET tb.seat_freed = 1, ts.free_machines = ts.free_machines + 1
WHERE 1
AND tb.seat_freed = 0
AND tb.transactionComplete = 0
Here I am trying to free the seats by updating the seat_freed to 1 and increasing the free_machines counter by 1.
In case, there are more than 1 rows (say 3 rows) returned from tbl_bookings, I would want to increment the counter by .
Is there any way to do it, using the single. I can obviously do it by breaking it down into different queries, but single query is what I desire. :)
You could use a subquery with the exact same conditions to calculat the number of rows which will be affected by the update. I used DISTINCT for the count since i don't know how bookings and slots are related in your example.
UPDATE tbl_bookings tb
INNER JOIN tbl_slots ts ON ( tb.slot_id = ts.id )
INNER JOIN (SELECT count(DISTINCT b.id) seats_to_be_freed
FROM tbl_bookings b INNER JOIN tbl_slots s ON ( b.slot_id = s.id )
WHERE b.seat_freed=0 and b.transactionComplete=0) tmp
SET tb.seat_freed = 1, ts.free_machines = tmp.seats_to_be_freed
WHERE 1
AND tb.seat_freed = 0
AND tb.transactionComplete = 0

query is not working correctly

I have these tables:
single_user_has_university_has_course
single_user_users_id_user university_has_course_university_id_university university_has_course_course_id_course first_year_school last_year_school grade
1 1 1 2000 2001 15
And
university_has_course
university_id_university course_id_course
1 1 <<< I want to select this
15 1
1 3
15 3
The problem is that the query below return all rows where course_id_course = 1 but ignore theuniversity_id_university = 1`. Then, will return two rows.
query:
SELECT A.first_year_school, A.last_year_school, A.grade, U.university, C.course, T.type_course
FROM single_user_has_university_has_course A
INNER JOIN university_has_course Q
ON A.university_has_course_course_id_course = Q.course_id_course
INNER JOIN university U
ON Q.university_id_university = U.id_university
INNER JOIN course C
ON Q.course_id_course = C.id_course
INNER JOIN type_course T
ON C.type_course_id_type_course = T.id_type_course
AND A.single_user_users_id_user = ?
You are joining the last table to the previous joins on 2 constraints C.type_course_id_type_course = T.id_type_course AND A.single_user_users_id_user = ?. I think you want to write it as a WHERE clause. So replace your last line with:
WHERE A.single_user_users_id_user = ?
But you're not also trying to filter university_id_university = 1, so you may want to add it to the WHERE clause as well:
WHERE A.single_user_users_id_user = ? AND Q.university_id_university = ?