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
Related
i have a two table i want to know the number of person who are all assigned to project in each sector
CREATE TABLE first1( a int,projectname varchar(20));
INSERT INTO first1 VALUES
(1001,'crm'),
(1002,'iic'),
(1003,'abc'),
(1004,'sifty bank');
CREATE TABLE diff(b int,name varchar(20),p_id int );
INSERT INTO diff VALUES
(101,'priya',1001),
(102,'divya',1002),
(103,'sidhu',null),
(104,'shiva',null),
(105,'surya',1002);
Query:
select first1.projectname,count(*) from first1 left join diff on first1.a=diff.p_id group by
first1.projectname;
The output of this code is:
abc|1
crm|1
iic|2
sifty bank|1
The expected output is :
abc|0
crm|1
iic|2
sifty bank|0
The problem is count(*); it counts how many rows there are in each group - A project without any person assigned still counts as 1. Instead, you need to count() something from the left table, so null values are not taken into account:
select f.projectname, count(d.p_id) as cnt_diff
from first1 f
left join diff d on f.a = d.p_id
group by f.projectname;
Note that you can get the same result with a subquery:
select f.projectname,
(select count(*) from diff d where d.p_id = f.a) as cnt_diff
from first1 f
I used for a while Stack, you help me a lot ... this is my first question, I hope to be able to expose correctly.
I need to retrieve all value from a table and add one column with sum value from another table.
This what I mean:
q1=>
SELECT * FROM users WHERE role >30 AND role <50 AND available = 1 AND active = 1
to q1 add a new column from q2=>
SELECT SUM(budget) AS totalBudget FROM projects_assignments
Relation between tables are idUser
more over in projects_assignments I have some users of users' table and some user is in projects_assignments only one time.
example:
table1 = idUser, some values
table2 = idUser, budget, other es: values (1,10000,40),(1,5000,30),(2,5000,30)
My expected result is:
idUser,table1column1,table1column2,totalBudget
1,x,y,15000(10k+5k)
2,x,x,5000
3,x,y,0
...
n,x,y,0
Thanks a lot,
Matteo
select table1.column1,
table1.column2,
table1.column3,
sum(table2.budget) as totalBudget
from table1
inner join table2
on table1.idUser = table2.idUser
where...
group by table1.idUser
I have a table that looks like this:
For each COMPANY there are multiple NATURAL_PERSON_ID, every NATURAL_PERSON have a date in which an audit was performed FECHA_DE_REPORTE and as a company there is a date in which the first loan was give to that company.
What I want is to select for each NATURAL_PERSON all the FOLIO_CONSULTA whose FECHA_DE_REPORTE is less or equal to FIRST_LOAN (the date in which the first loan was given for that company) Then I need to find the MAX date among each group and keep al the information (the whole row) for the value that fulfills all these conditions, and all this for each NATURAL_PERSON
So for this example the result I expected is all the information of the second row since this is the MAX() of FECHA_DE_REPORTE by COMPANY AND NATURAL_PERSON.
I have tried:
SELECT NPC.COMPANY_ID
,NPC.NATURAL_PERSON_ID
,NPS.DIGITAL_SIGNATURE_ID
,CDC.FOLIO_CONSULTA
,CDC.FECHA_DE_REPORTE
,FIRST_LOAN.FIRST_LOAN
,MAX(CDC.FECHA_DE_REPORTE) MAX_FOLIO_CONSUTA
FROM KONFIO.NATURAL_PERSON_COMPANY NPC
LEFT JOIN KONFIO.NATURAL_PERSON_SIGNATURE NPS ON NPS.NATURAL_PERSON_ID = NPC.NATURAL_PERSON_ID
JOIN KONFIO.CDC_RESPONSE CDC ON CDC.DIGITAL_SIGNATURE_ID= NPS.DIGITAL_SIGNATURE_ID
JOIN
(
SELECT CAPP.COMPANY_ID
,MIN(LOAN.DOCUMENTATION_DATE) FIRST_LOAN
FROM KONFIO.COMPANY_APPLICATION CAPP
JOIN KONFIO.LOAN ON LOAN.APPLICATION_ID = CAPP.APPLICATION_ID
GROUP BY CAPP.COMPANY_ID) FIRST_LOAN ON FIRST_LOAN.COMPANY_ID = NPC.COMPANY_ID
WHERE CDC.FECHA_DE_REPORTE <= FIRST_LOAN.FIRST_LOAN
AND NPC.COMPANY_ID IN (1033)
GROUP BY NPC.COMPANY_ID, NPC.NATURAL_PERSON_ID
but it retrieves the first value that finds so the FOLIO_CONSULTA does not correspond to the FOLIO_CONSULTA of the MAX() FECHA_DE_REPORTE
Any help would be appreciated
You should join the subquery for MAX(FECHA_DE_REPORTE) on table CDC_RESPONSE
SELECT NPC.COMPANY_ID
,NPC.NATURAL_PERSON_ID
,NPS.DIGITAL_SIGNATURE_ID
,CDC.FOLIO_CONSULTA
,CDC.FECHA_DE_REPORTE
,FIRST_LOAN.FIRST_LOAN
,T.MAX_FOLIO_CONSUTA
FROM KONFIO.NATURAL_PERSON_COMPANY NPC
INNER JOIN (
SELECT DIGITAL_SIGNATURE_ID
, MAX(FECHA_DE_REPORTE) MAX_FOLIO_CONSUTA
FROM KONFIO.CDC_RESPONSE
GROUP BY DIGITAL_SIGNATURE_ID
) T ON T.DIGITAL_SIGNATURE_ID = NPS.DIGITAL_SIGNATURE_ID
AND T.MAX_FOLIO_CONSUTA = CDC.FECHA_DE_REPORTE
LEFT JOIN KONFIO.NATURAL_PERSON_SIGNATURE NPS ON NPS.NATURAL_PERSON_ID = NPC.NATURAL_PERSON_ID
JOIN KONFIO.CDC_RESPONSE CDC ON CDC.DIGITAL_SIGNATURE_ID= NPS.DIGITAL_SIGNATURE_ID
JOIN
(
SELECT CAPP.COMPANY_ID
,MIN(LOAN.DOCUMENTATION_DATE) FIRST_LOAN
FROM KONFIO.COMPANY_APPLICATION CAPP
JOIN KONFIO.LOAN ON LOAN.APPLICATION_ID = CAPP.APPLICATION_ID
GROUP BY CAPP.COMPANY_ID) FIRST_LOAN ON FIRST_LOAN.COMPANY_ID = NPC.COMPANY_ID
WHERE CDC.FECHA_DE_REPORTE <= FIRST_LOAN.FIRST_LOAN
AND NPC.COMPANY_ID IN (1033)
GROUP BY NPC.COMPANY_ID, NPC.NATURAL_PERSON_ID
...... missing part
I have this query:
SELECT `y78f2_students`.`firstname` , `y78f2_students`.`lastName` , `y78f2_students`.`student_id`,`y78f2_attendance`.`is_present`, `y78f2_attendance`.`note`, `y78f2_attendance`.`thedate`
FROM `y78f2_students`
INNER JOIN `y78f2_enrolls` ON `y78f2_enrolls`.`student_id` = `y78f2_students`.`student_id`
INNER JOIN `y78f2_attendance` ON `y78f2_attendance`.`student_id` = `y78f2_students`.`student_id`
WHERE `y78f2_enrolls`.`term` = 'Term 2 2016'
AND `y78f2_enrolls`.`crn_class` = 'Math1R1'
and `y78f2_attendance`.`thedate` = '2016-01-24'
ORDER BY thedate desc
This query returns only the rows where the date is '2016-01-24'. Currently that is just one row: http://i.imgur.com/jRTBqQ0.png
I need to show all rows where term = 'Term 2 2016' and crn_class` = 'Math1R1' and also where the date is not set as yet. In order words I want to show all students in the class and if the date is not set for these students yet, it will show null.
This is what I would like: http://i.imgur.com/jmsuSF5.png
So in summary I need to show rows where the clause is met and those where the date would be null or not exist yet.
How can I write this query?
Try moving the conditions related to the joined tables from the end of the query, up to the table's respective ON clause for each join. Also, if you would like to return records for which no row yet exists in the y78f2_attendance table, that table should be LEFT OUTER joined, not INNER joined.
SELECT `y78f2_students`.`firstname` , `y78f2_students`.`lastName`,
`y78f2_students`.`student_id`,`y78f2_attendance`.`is_present`,
`y78f2_attendance`.`note`, `y78f2_attendance`.`thedate`
FROM `y78f2_students`
INNER JOIN `y78f2_enrolls` ON
`y78f2_enrolls`.`student_id` = `y78f2_students`.`student_id`
AND `y78f2_enrolls`.`crn_class` = 'Math1R1'
LEFT OUTER JOIN `y78f2_attendance` ON
`y78f2_attendance`.`student_id` = `y78f2_students`.`student_id`
AND (`y78f2_attendance`.`thedate` IS NULL OR `y78f2_attendance`.`thedate` = '2016-01-24')
WHERE `y78f2_enrolls`.`term` = 'Term 2 2016'
ORDER BY thedate desc
i've 2 tables 1st named orders and 2nd named rewinding.
1st orders has one record
id=1, job_code=597, job_name=xyz
2nd rewinding has one or more than one record
id=5, job_code=597, weight=254,remarks=foo
id=6, job_code=600, weight=765,remarks=foo
id=7, job_code=597, weight=594,remarks=foo
id=8, job_code=597, weight=54,remarks=foo
Now I want to select all the rows and columns which has code=597 and only record from orders where job_code=597
If there are multiple rows in orders with the job_code and you only want one, then use limit in a subquery:
SELECT *
FROM (SELECT o.* FROM orders o WHERE o.job_code = 597 LIMIT 1
) as o597 JOIN
rewinding r
ON o597.job_code = r.job_code;
What about this?
SELECT *, orders.id orders_id, rewinding.id rewinding_id
FROM orders
JOIN rewinding ON orders.job_code = rewinding.job_code
WHERE orders.job_code = 597
Demo: http://sqlfiddle.com/#!2/b1ad6/4