select one row and some row in second table - mysql

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

Related

query make a count to 1 which does not satistfy the condition

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

SQL Subtract from different tables

I'm subtracting values from diferent tables based on ID.
Its like an inventory where I've the tables for Itens In and Itens Out.
What I want to do is to substract the Itens In - Itens Out based on ID of the iten.
I manage to do that, but if an Iten only as an In movement, the query just shows an empty row, when it should show the In movement - Out moment that even if it doesnt exists should be considered as 0, showing in this case only the value of the IN movment.
Can someone help?
Each row in each table represents one item.
TABLE - in_used
id_item_____qnt
1 _________500
2 _________1000
TABLE - out_used
id_item_____qnt
1 _________200
OUTPUT EXPECTED
used_stock
id_item____qnt
1 ________300
2 ________1000 (there's no out movement so it should show only the IN one)
Select
in_used.qnt - out_used.qnt As used_Stock
From
in_used Inner Join
out_used On in_used.id_item = out_used.id_item
If id_item is unique in in_used and there is at most 1 row in out_used then all you need is a LEFT join and COALESCE():
select i.id_item,
i.qnt - coalesce(o.qnt, 0) used_Stock
from in_used i left join out_used o
on i.id_item = o.id_item
If there are more rows use also aggregation:
select i.id_item,
sum(i.qnt) - coalesce(sum(o.qnt), 0) used_Stock
from in_used i left join out_used o
on i.id_item = o.id_item
group by i.id_item
It sounds like you want something like this:
select item, sum(inc)
from ((select item, in_used as inc
from items_in
) union all
(select item, - out_used as inc
from items_out
)
) ii
group by item;
I'm not sure if each row in the two tables represents one item or if there is a counter. If there is a counter, then that should be used instead of 1/-1.
In this case we know that we have id_item in all cases for join results and none of them would be null:
SELECT I.id_item,
SUM(I.qnt) - SUM(O.qnt) AS used_Stock
FROM in_used AS I
JOIN out_used AS O ON I.id_item = O.id_item
GROUP BY id_item
But for general result, you can try below query:
SELECT (CASE
WHEN I.id_item IS NOT NULL THEN I.id_item
ELSE O.id_item
END) AS id_item,
SUM(I.qnt) - SUM(O.qnt) AS used_Stock
FROM in_used AS I
JOIN out_used AS O ON I.id_item = O.id_item
GROUP BY id_item
Hope this works for you!
If you have more then one record per id you can use sum() and must use FULL JOIN if there a no records on both tables or otherwise you can use LEFT JOIN
Select
id_item , coalesce(sum(in_used.qnt),0) - coalesce(sum(out_used.qnt),0) As used_Stock
From
in_used FULL Join
out_used On in_used.id_item = out_used.id_item
GROUP BY id_item

Retrieve all the values that are in the the row with the max value

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

How to select distinct rows when using joint table

I have two tables named chatMaster and chatMessages , i was trying to left join the two tables to get all data from master and latest message for each chat. I am using this query to join the two tables
SELECT c.id, c.p1, c.p2, m.toP, m.message, m.createdOn FROM chatMaster c
left join chatMessages m on c.id = m.id group by m.id ;
This query is for selecting only one row for each entry in chatMaster, but it is selecting the first message for every entry like,
1, 2018-11-24 00:40:08, HI!, 99e22056-ee7f-11e8-bc28-8acac6f59ef9, 1001
2, 2018-11-24 00:40:21, HI! There, 99e22056-ee7f-11e8-bc28-8acac6f59ef9, 1
3, 2018-11-24 01:33:12, HI!, e3345a17-ee7f-11e8-bc28-8acac6f59ef9, 2
this is the result of select * from chatMessages, There are two entries for chat id 99e22056-ee7f-11e8-bc28-8acac6f59ef9 and i want the send one which is send on 2018-11-24 00:40:21 but the result of my join query is
99e22056-ee7f-11e8-bc28-8acac6f59ef9, 1001, 1, 1001, HI!, 2018-11-24 00:40:08
e3345a17-ee7f-11e8-bc28-8acac6f59ef9, 2, 10001, 2, HI!, 2018-11-24 01:33:12
It is selecting the first message. How to select the latest message? What changes should i make to get the latest message from chatMessages instead of first message. please ask if you need more details
In a Derived Table, you can get the maximum values of createdOn (latest createdOn) for every chat-master id. You can then join this result-set to the main table, to get only the row corresponding to latest createdOn.
SELECT c.id, c.p1, c.p2, m.toP, m.message, m.createdOn
FROM chatMaster c
LEFT JOIN
( SELECT id, MAX(createdOn) AS latest_createdOn
FROM chatMessages
GROUP BY id
) AS dt
ON dt.id = c.id
LEFT JOIN chatMessages m
ON m.id = c.id AND
m.createdOn = dt.latest_createdOn

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