Convert SQL query into activerecord - sql

I want to convert this SQL query into activerecord codeigniter, is there anyone can help?
SELECT t.idkas,t.tglkas, (
SELECT (SUM(kredit))-(SUM(debet))
FROM kaskecil t2
WHERE t2.idkas <= t.idkas
) AS total
FROM kaskecil t

$this->db->select("t.idkas, t.tglkas, ((SELECT SUM(kredit) - SUM(debet) FROM kaskecil t2 WHERE t2.idkas <= t.idkas) AS total)", FALSE);
$this->db->from("kaskecil t");
$query = $this->db->get();

Related

Where Equals Subquery Laravel

How can I convert the following SQL query to Laravel:
SELECT *
FROM SomeTable
WHERE some_column =
(
SELECT some_column
FROM SomeTable st1
where st1.some_column2 = SomeTable.some_column2
)
This query is just an example query. I know that Laravel has a whereIn function for subqueries. I am looking for something similar about equality of a column with a subquery.
DB::table('SomeTable')
->select('*')
->where('some_column','=',function($query) {
$query->from('SomeTable')
->select('some_column')
->where('SomeTable.some_column2','=',DB::raw('SomeTable.some_column2'));
})
->get();

How can i write this query with gorm?

i want to write this query with gorm
SELECT count(*)
FROM (select distinct on(t1.number) t1.number ,t1.id from "tbl_phone_books" as t1
WHERE (customer_id = 100)) as phone LIMIT 5 OFFSET 0
i wrote this code but it didn't work correctly:
Db.Debug().
Model(models.TblPhoneBook{}).
Select("distinct on(number) number,id).
Where("customer_id = ? , customerId).
Offset(offset).
Limit(5).
Count(&response)
db.Table("tbl_phonebooks").Select("distinct on(number) number, id").Where("customer_id = ?", 100).Group("number, id").Limit(5).Offset(0).Count(&count)

how to use sql query in Laravel query

SELECT
COUNT(*)
FROM
(
SELECT
tbl_order.order_id
FROM
tbl_order
where delete_status=1
group by patient_id,event_no
) AS DerivedTableAlias
I want this sql in laravel
Try out this query builder query:
/DB::table('tbl_order')
->select('patient_id','event_no')
->selectRaw('COUNT(tbl_order.order_id) AS count')
->where('delete_status', 1)
->groupBy('patient_id','event_no')
->get();

Convert self join in sql to DAX Query power bi

We have a scenario where we need to convert a particular query to power BI DAX query. Its kind of a self join with filter on multiple column.
SELECT IRF.file_number, IRF.date_updated, FileFrequency, ( SELECT TOP 1 new_value FROM #table1 A WHERE A.field_name = 'OperationsStatusCode' AND A.file_number = IRF.file_number AND A.date_updated <= IRF.date_updated ORDER BY A.date_updated DESC ) AS OpsStatus, ( SELECT TOP 1 new_value FROM #table1 A WHERE A.field_name = 'xyz' AND A.file_number = IRF.file_number AND A.date_updated <= IRF.date_updated ORDER BY A.date_updated DESC ) AS LabStatus INTO #Table2 FROM #table1;
We are trying to create a calculated column using DAX query but not able to get the desired result. Can ayone guide as o how ew can achieve this.
LabSecondCondition = CALCULATE( TOPN ( 1, VALUES(table1[new_value]) ), FiLTER( table1, table1[field_name] = "LaboratoryStatusCode" && table1[file_number] = table1[file_number] && table1[date_updated] <= table1[date_updated] ) )

Hql Querys for sql query

Can you give exact hql Query for this Sql query?
SELECT * FROM `ims_product`WHERE
quantity <= reorder_quantity AND
reorder_quantity IS NOT NULL AND
NOT(reorder_quantity=0);
Try using.
FROM `ims_product` WHERE `quantity` <= `reorder_quantity` AND `reorder_quantity` != 0;