What is equivalent query in yii2 - yii2

As search filtering is not working when use findbysql in yii2 searchmodel, so I want to write an equivalent query of "SELECT * FROM challan WHERE id IN (SELECT MAX(id) FROM challan GROUP BY sid)" in Yii2

$query->andWhere(new Expression('id IN (SELECT MAX(id) FROM challan GROUP BY sid)'));

I have found answer after spending many hours and here it is
Challan::find()->Where(['challan.id' => Challan::find()->select(['MAX(id)'])->groupBy('sid')]);

Related

How to use MAX function in yii2 query?

I have a query:
SELECT hs.*
FROM hire_screening hs
INNER JOIN
(SELECT resume_id, MAX(created_date) AS MaxDateTime
FROM hire_screening
GROUP BY resume_id) hire_screening
ON hs.resume_id = hire_screening.resume_id
AND hs.created_date = hire_screening.MaxDateTime
This is my table:
I need the answer like this:
I tried this:
$query = HireScreening::find()-
->select(["hire_screening.screening_id","hs.resume_id",
"MAX(hs.created_date) AS
MaxDateTime","hs.screening_by","hsl.screening_level as
hr_level","hss.screening_status as
hr_status","hr.candidate_name","hsm.screening_mode as
hr_mode","hire_screening.created_date","hs.screening_date"])
->innerJoin('hire_screening as hs','hs.resume_id =
hire_screening.resume_id')
->leftJoin('hire_screening_level as
hsl','hire_screening.screening_level = hsl.id')
->leftJoin('hire_screening_mode as
hsm','hire_screening.screening_mode = hsm.id')
->leftJoin('hire_screening_status as
hss','hire_screening.screening_status = hss.id')
->leftJoin('hire_resume as
hr','hire_screening.resume_id=hr.resume_id')
//->where(['hire_screening.created_date = MaxDateTime'])
->groupBy(['resume_id']);
//->having(['hire_screening.created_date' =>
'hs.MaxDateTime']);
$query->orderBy(['created_date' => SORT_DESC]);
But it didn't shows the answer. I need the distinct resume_id's with latest created date. The sql query shows the correct answer.I want to write this query in my search model. Please help me to convert this query into yii2.
If there is a table MyTable, then you can use simplier query grouping the table by resume_id:
SELECT
screening_id,
resume_id,
screening_by,
screening_date,
screening_level,
screening_mode,
screening_status,
reject_reason,
remarks,
created_by,
MAX(created_date) AS created_date
FROM MyTable
GROUP BY resume_id;
Using simplify query helps to avoid an errors. Also you could create a view or a stored procedure using the query and call this from your PHP code.

how to count how many person use the same id

I have this one table which is table staff. The column is id_staff, no_staff and id_posting. I would like to count how many staff using the same id_posting. Can anyone help me with the query?thanks.
Do you have tried to make the query yourself?
This is quite basic
select count(id_staff), id_posting from staff where id_posting = ? group by id_posting
Please try this
SELECT id_posting ,COUNT( `id_staff` ) as cnt_staff
FROM table_staff
GROUP BY id_posting
select COUNT(*) AS ID_COUNT from YOUR_TABLE group by THE_ID_WHICH_IS_REPEATED
http://www.w3schools.com/sql/sql_func_count.asp
check the tutorial for more info.

Use NEWID() without losing distinct?

I am trying to create a new data extract from a (badly designed) sql database. The customer requires that I add a distinctidentifier which I am attempting to do using the NEWID() function. Unfortunately this leads to multiple duplicate records being returned.
After a bit of research I have found that the NEWID() function does indeed 'undo' the use of the distinct keyword, but I cannot work out why or how to overcome this.
An example of the query I am trying to write is as follows:
select distinct
NEWID() as UUID
,Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)
If I miss out the NEWID() line in the select block, I get 569 records returned, which is correct, but if I include that line then I get in excess of 30,000 which are all duplicates of the original 569 but with different IDs. Can anyone suggest a way around this problem?
Thanks in advance
Use a sub query would be the easiest way to do it.
SELECT NEWID() as UUID
, * -- this is everything from below
FROM (
select distinct
Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)) as mySub
select NEWID() as UUID
,ISRN
,Internal_Patient_No
,Date_of_Birth
,histo_report
,Investigation_Result_Date
from (
select distinct
,Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)) t
You can use a sub-query to get around the issue, something like.....
SELECT NEWID() as UUID
,*
FROM (
select distinct
Histo_Results_File.ISRN
,Histo_Results_File.Internal_Patient_No
,Histo_Results_File.Date_of_Birth
,Histo_Result_freetext.histo_report
,Histo_Report.Date_Report_Updated as [Investigation_Result_Date]
from apex.Histo_Results_File
inner join apex.Histo_Report
on (Histo_Report.Histo_Results_File = Histo_Results_File.ID)
) t

Codeigniter - convert mysql query to CodeIgniter's active record query

I'm trying to convert this SQL query into a codeigniter query
SELECT
uploads.EMAIL
FROM
uploads
JOIN (
SELECT EMAIL, COUNT(*) as num FROM uploads GROUP BY EMAIL
) c ON uploads.EMAIL = c.EMAIL
ORDER BY
c.num DESC,
EMAIL ASC
Thanks for the help
kind regards
I am not sure why you can't figure this out yourself using the active record documentation, but:
$this->db->select('uploads.EMAIL');
$this->db->from('uploads');
$this->db->join('(SELECT EMAIL, COUNT(*) as num FROM uploads GROUP BY EMAIL) c','uploads.EMAIL = c.EMAIL','',FALSE);
$this->db->order_by('c.num desc, uploads.EMAIL asc');
and then
$query = $this->db->get();
FYI, passing FALSE as the fourth parameter to the db->join() method will cause it not to escape the statement, so you should be careful if you're going to take external variables. This is, until CodeIgniter 3, the only way to do subqueries with active record without extending the active record class to add them.

Nested MySQL query?

I have this MySQL statement which works fine:
SELECT claim_items.item_id, claim_items.quantity*items_rate.rate as per_item
FROM claim_items, items_rate
WHERE claim_items.claim_id = 1 AND claim_items.item_id = items_rate.items_id
However what I want to do is to get the sum of the per_item field generated in the MySQL statement. Is it possible to do a nested statement to do that? I know I could create a view and then do it, but would prefer to do it in one statement.
Thanks for your help, much appreciated!
select t.id, SUM(t.per_item)
from
(
SELECT claim_items.item_id as id, claim_items.quantity*items_rate.rate as per_item
FROM claim_items, items_rate
WHERE claim_items.claim_id = 1 AND claim_items.item_id = items_rate.items_id
) t
group by t.id
Hope this should help.
If you just want the sum over all rows you can do it like that (using explicit joins for better readability):
SELECT SUM( claim_items.quantity * items_rate.rate )
FROM claim_items
JOIN items_rate ON ( items_rate.items_id = claim_items.item_id )
WHERE claim_items.claim_id = 1