Reputation: 197
I have a query in MySQL.
Full Query
SELECT
tc.expense AS expense,
tc.tour_sub_code,
tc.login_id
FROM
tc_wallet tc
WHERE tc.login_id = 'vinod.kumbala'
AND tc.expense = 'Daily Allowance'
AND tc.delete_flag = 'F'
AND tc.status != 'reject'
Result
Expense Tour_sub_code login_id
DAILY ALLOWANCE MOS-EUROPE100119 vinod.kumbala
DAILY ALLOWANCE Test vinod.kumbala
Initially I am fetching data from tc_wallet
table.
Now my requirement is that I need to find the total count of attendances for a particular tour_sub_code
. Attendances count can be found from attendance_master
table for that particular tour_sub_code
.
So I included a nested select query which is
Nested select
(SELECT
COUNT(*)
FROM
(SELECT
*
FROM
`attendance_master`
WHERE `delete_flag` = 'F'
AND login_id = 'vinod.kumbala'
AND `tour_sub_code` = tc.`tour_sub_code`
GROUP BY `device_date`) t1) AS newNoOfdays
Full Query
SELECT
tc.expense AS expense,
tc.tour_sub_code,
tc.login_id,
(SELECT
COUNT(*)
FROM
(SELECT
*
FROM
`attendance_master`
WHERE `delete_flag` = 'F'
AND login_id = 'vinod.kumbala'
AND `tour_sub_code` = tc.`tour_sub_code`
GROUP BY `device_date`) t1) AS newNoOfdays
FROM
tc_wallet tc
WHERE tc.login_id = 'vinod.kumbala'
AND tc.expense = 'Daily Allowance'
AND tc.delete_flag = 'F'
AND tc.status != 'reject'
Now this query gives me error as
Unknown column 'tc.tour_sub_code' in 'where clause'
Expected Result
Expense Tour_sub_code login_id Count
DAILY ALLOWANCE MOS-EUROPE100119 vinod.kumbala 20
DAILY ALLOWANCE Test vinod.kumbala 44
Can I know where I am going wrong with this ?
Also is there any other way i get result like by using JOINS ?
Upvotes: 3
Views: 5909
Reputation: 31802
In the innermost subquery you are referencing a column from the outer most query. This is not permitted for subqueries in the FROM clause (derived tables). However - You don't need that subquery. What you need is COUNT(DISTINCT device_date)
Rewrite
(SELECT
COUNT(*)
FROM
(SELECT
*
FROM
`attendance_master`
WHERE `delete_flag` = 'F'
AND login_id = 'vinod.kumbala'
AND `tour_sub_code` = tc.`tour_sub_code`
GROUP BY `device_date`) t1) AS newNoOfdays
to
(SELECT
COUNT(DISTINCT device_date)
FROM `attendance_master`
WHERE `delete_flag` = 'F'
AND login_id = 'vinod.kumbala'
AND `tour_sub_code` = tc.`tour_sub_code`
) AS newNoOfdays
You can also rewrite the full query to a LEFT JOIN query:
SELECT
tc.expense AS expense,
tc.tour_sub_code,
tc.login_id,
COUNT(DISTINCT device_date) AS newNoOfdays
FROM tc_wallet tc
LEFT JOIN attendance_master am
ON am.tour_sub_code = tc.tour_sub_code
AND am.delete_flag = 'F'
AND am.login_id = 'vinod.kumbala'
WHERE tc.login_id = 'vinod.kumbala'
AND tc.expense = 'Daily Allowance'
AND tc.delete_flag = 'F'
AND tc.status != 'reject'
Upvotes: 1
Reputation: 41
Can you try this:
select tc.expense AS expense,
tc.tour_sub_code,
tc.login_id,
newNoOfdays.countVal
from tc_wallet tc
inner join
( select tour_sub_code, count('A') 'countval'
from attendance_master ascs (nolock)
group by tour_sub_code) as newNoOfdays
on newNoOfdays.tour_sub_code = tc.tour_sub_code
where tc.login_id = 'vinod.kumbala'
AND tc.expense = 'Daily Allowance'
AND tc.delete_flag = 'F'
AND tc.status != 'reject'
Upvotes: 0
Reputation: 812
Try This ---
SELECT
tc.expense AS expense,
tc.tour_sub_code,
tc.login_id,
(SELECT
COUNT(am.*)
FROM `attendance_master` as am
WHERE am.delete_flag = 'F'
AND am.login_id = 'vinod.kumbala'
AND am.tour_sub_code = tc.tour_sub_code
GROUP BY am.device_date) AS count
FROM
tc_wallet tc
WHERE tc.login_id = 'vinod.kumbala'
AND tc.expense = 'Daily Allowance'
AND tc.delete_flag = 'F'
AND tc.status != 'reject'
Upvotes: 0
Reputation: 1371
Try this:
SELECT
tc.expense AS expense,
tc.tour_sub_code,
tc.login_id,
count(*) as count
FROM
tc_wallet tc
INNER JOIN
attendance_master am
ON
tc.tour_sub_code = am.tour_sub_code AND
tc.login_id = am.login_id
WHERE tc.login_id = 'vinod.kumbala'
AND tc.expense = 'Daily Allowance'
AND tc.delete_flag = 'F'
AND tc.status != 'reject'
GROUP BY
tc.expense AS expense,
tc.tour_sub_code,
tc.login_id
Upvotes: 0