MySQL select query on multiple table does not display the comm data - mysql

here is my current query and it access four tables. Everything else displays the correct data except for this part:
IF(tbl_shift.commission IS NULL,'0.00', (tbl_shift.commission*tbl_payment.subtotal)) AS comm,
this doesn't seem to access the data from the table.
Here is the full query:
SELECT tbl_payment.dateTime AS purchasedate, tbl_payment.invoiceNo AS invoice,
tbl_payment.subtotal AS total, tbl_payment.staffid AS employee,
tbl_users.fname AS firstname, tbl_users.lname AS lastname,
IF(tbl_shift.commission IS NULL,'0.00', (tbl_shift.commission*tbl_payment.subtotal)) AS comm,
(SELECT COUNT(1) AS qty FROM tbl_purchases
WHERE tbl_payment.invoiceNo=tbl_purchases.invoiceNo) AS qty
FROM `tbl_payment`
LEFT JOIN tbl_shift ON tbl_payment.staffid = tbl_shift.employeeId
AND tbl_payment.dateTime BETWEEN tbl_shift.startShift
AND tbl_shift.endShift
INNER JOIN tbl_users ON tbl_payment.staffid = tbl_users.id
WHERE tbl_payment.staffid = ".$staff."
AND dateTime BETWEEN '".$from."' AND '".$to."'
ORDER BY tbl_payment.dateTime DESC

if this succeeds
IF(tbl_shift.commission IS NULL,'0.00', (tbl_shift.commission*tbl_payment.subtotal)) AS comm,
Wouldn't it mean that you would have both string values and numbers in the same column if both are true for any rows?
Perhaps try this and see what happens:
IF(ISNULL(tbl_shift.commission)=1,0,(tbl_shift.commission*tbl_payment.subtotal)) AS comm,

Related

same column name but the data is from other table

I'm having a problem on a query because I cannot get how to do this.
These are my tables:
You might want to have a look at standard MySQL documentation for JOIN
select timeout,
timein,
taggedby.employee_name tagged_by,
untaggedby.employee_name untagged_by
from table_breaktime tb
JOIN table_employee taggedby on taggedby.emp_no = tb.tagged_by
JOIN table_employee untaggedbyon untaggedby.emp_no = tb.untagged_by
We are joining the employee table twice, on the columns tagged_by and untagged_by from the table table_breaktime. To fetch the employee names individually
Here's alternate solution:
select time_out, time_in,
(select employee_name from table_employee where emp_no = tb.tagged_by) tagged_by,
(select employee_name from table_employee where emp_no = tb.untagged_by) untagged_by,
from table_breaktime tb

get updated date field row after inner joining table

I have a situation where a row is inserted whenever a schedule is created with call_back_date value
la_id is the same for each row, i want get a most recent row that is max(call_back_date)
I have written this query which fetch all the entries for those schedule after inner joining three tables:
SELECT
la_details.application_no,
la_details.id la_det,
la_details.client_id client_id,
la_details.la_name la_name,
la_details.sex sex,
la_details.advisor_name advisor_name,
la_details.client_phone client_phone,
la_details.client_mobile client_mibile,
la_details.level level,
la_details.state state,
la_details.pin_code pin_code,
trans_schedules.id schedule_id,
trans_schedules.*,
comments.descriptions
FROM
la_details
INNER JOIN trans_schedules
ON
trans_schedules.la_id=la_details.id
INNER JOIN comments
ON
comments.comment_id=trans_schedules.comment_id
WHERE
(trans_schedules.comment_id<>'1'
OR
trans_schedules.list_followup = '1'
OR
trans_schedules.counter_flag='0')
AND
la_details.case_closed='0'
ORDER BY
trans_schedules.call_back_date
) a1 INNER JOIN
(
SELECT la_id,MAX(call_back_date) AS call_back_date from
trans_schedules group by la_id
) b1 on a1.la_det = b1.la_id and a1.call_back_date = b1.call_back_date
My question is how could i use a subquery on previous query to get max call_back_date row
Thanks in advance for help!!!!
For MySQL:
Select LAST_INSERT_ID();
It will give you last inserted identity record

Working on three tables, my grouping by it's not working

I have to list the difference between the number of tickets sold at the different
stations of one line between time1 and time2. I have three table: time1, time 2 and place.
create table fares_jan18 (
station varchar(100),
ff int
);
create table fares_feb1 (
station varchar(100),
ff int
);
create table stations (
name varchar(100),
line varchar(50)
);
I'm using this query:
SELECT
fares_jan18.station AS name,
SUM(fares_feb1.ff - fares_jan18.ff) AS diff_feb1_jan18
FROM
fares_jan18
JOIN fares_feb1 ON fares_jan18.station = fares_feb1.station
JOIN stations ON fares_jan18.station = stations.name
WHERE
stations.line ="Broadway"
GROUP BY
name
ORDER BY
name;
it does give me the correct table, but when ever there is more that one record, I get the duplicate the sum of the difference. For instance the result for a station with two values should be 33254, and it' giving me 66508. I have removed the group by from my query, it seems that I have for records instead of two.
I've read all I could on StackOverflow but I can't get my head around what I'm doing wrong? Thanks!!
Here is an image of my table:
You're creating a cartesian product of all the rows with the same stations in the two fares tables, and then summing the differences from each pair. You need to calculate the totals in each table separately in subqueries, and then subtract them.
SELECT s.name, feb1.total - jan18.total AS diff_feb1_jan18
FROM stations AS s
JOIN (SELECT station, SUM(ff) AS total
FROM fares_feb1
GROUP BY station) AS feb1
ON feb1.station = s.station
JOIN (SELECT station, SUM(ff) AS total
FROM fares_jan18
GROUP BY station) AS jan18
ON jan18.station = s.station
WHERE s.line = "Broadway"
ORDER BY s.name
You need to pre-aggregate each separately or you are getting a Cartesian result... Since the query is for a single Broadway, a single record will result in each station when working the join from each date data source
SELECT
justF18.name,
justFeb1.SumFF - justF18.SumFF as FF_Diff
from
( SELECT f18.station AS name,
SUM(f18.ff) AS Sumff
FROM
fares_jan18 f18
JOIN stations s ON f18.station = s.name
AND s.line = "Broadway"
group by
f18.station ) justF18
JOIN
( SELECT feb1.station AS name,
SUM(feb1.ff) AS Sumff
FROM
fares_feb1 feb1
JOIN stations s ON feb1.station = s.name
AND s.line = "Broadway"
group by
feb1.station ) justFeb1
ON justF18.name = justFeb1.name
order by
JustFeb18.name

mySQL Query Count Distinct and Percentage

I'm having trouble creating a query that will report on training completions by users that are grouped by district within a region. For one training course the report needs to show for each district
Number of users assigned course
Number of users completed course
Percentage of users completed course
The output report should look like this (without the periods).:
District.........Assigned.......Completed....%
Arkansas..........20..............15...............75%
Illinois...............80..............80...............100%
Iowa.................10...............8.................80%
Michigan..........30..............20................66%
Here's the SQL query I have tried using
Select mytable.district as District,
(Select Count(Distinct mytable.user_id)
From mytable
Where mytable.district = District AND
mytable.training_title = 'My Course') As 'Assigned',
(Select Count(Distinct mytable.user_id)
From mytable
Where mytable.training_status = "Completed" AND
mytable.district = District AND
mytable.training_title = 'My Course') as 'Completed',
Concat(Round(100 * (Select Count(Distinct mytable.user_id)
From mytable
Where mytable.training_status = "Completed" AND
mytable.district = District AND
mytable.training_title = 'My Course') / (Select Count(Distinct mytable.user_id)
From mytable
Where
mytable.district = District AND
mytable.training_title = 'My Course'),0 ),"%") as '%'
From mytable
Where mytable.region = 'Midwest'
Group by District
It doesn't work at all like this. However if I substitute one of the district values (such as Arkansas) in for 'District' in the WHERE clause I can get the correct values for that district. However i need to find all of the districts for each region and calculate the values for district. This is only one of the regions I need to create the query for.
Two key issues:
All of the data exists in one table in my database. I have consolidated and imported it from another database.
The data contains duplicates Therefore I must use Distinct to eliminate the duplicates from the results.
Suggestions? Thanks in advance for your assistance!!!
As far as I can see, you could just sum things up as you want them and GROUP BY district. The subquery will take care of the duplicate rows.
SELECT
district,
COUNT(*) Assigned,
COUNT(CASE WHEN training_status='Completed' THEN 1 END) Completed,
CONCAT(COUNT(CASE WHEN training_status='Completed' THEN 1 END) /
COUNT(*) * 100, '%') `%`
FROM (
SELECT DISTINCT * FROM mytable
WHERE mytable.region = 'Midwest'
AND mytable.training_title = 'My Course') z
GROUP BY district;
An SQLfiddle to test with.

Fetching a result twice with deferend name

I have 1 table with tasks named opentask:columns: id,title,description,expires,creator_id,creator_name, executer_id, executer_name, priority_id, status_id1 table with users named user:
with columns: user_id, username
What I want is to create a query where there will be all columns from opentask table where the executer_id will be equal to the user_id of user table AND the creator_id will be equal to the user_id again. This creates a confusion because the first equality excludes the second.So I need somehow to create a query where I will include the usernames for the executer with something like where "opentask.executer_id=user_user_id" and at the same time I will include the username again (as a differend name?) for the creator with something like "where opentask.executer_id=user_user_id"So I try this, which of course I know that is missing something, can you help?
SELECT DISTINCT id, title, description, expires, creator_id, executer_id, oc_opentask.priority_id, oc_opentask.status_id, priority_name, status_name, user_id, username, (SELECT username FROM oc_opentask, oc_user WHERE oc_opentask.creator_id=oc_user.user_id) AS username2 FROM oc_opentask, oc_opentask_priority, oc_user, oc_opentask_status WHERE oc_opentask.priority_id=oc_opentask_priority.priority_id AND oc_opentask.executer_id=oc_user.user_id AND oc_opentask.status_id=oc_opentask_status.status_id ORDER BY oc_opentask.expires DESC
ReJOIN the table oc_user twice with different aliases creators and executers like so:
SELECT DISTINCT
t.*,
creators.user_name 'Creator name',
executers.username 'Executor name',
tp.priority_name,
s.status_name
FROM oc_opentask t
INNER JOIN oc_opentask_priority tp ON t.priority_id = tp.priority_id
INNER JOIN oc_user executers ON t.executer_id = executes.user_id
INNER JOIN oc_user creators ON t.creator_id = creators.user_id
INNER JOIN oc_opentask_status s ON t.status_id = s.status_id
ORDER BY t.expires DESC