how to use sql query in Laravel query - laravel-8

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();

Related

Convert SQL query into activerecord

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();

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 to do a sub-query in SQL

My table looks something like this:
I want to retrieve all the PractitionerIdFK if they have SpecialityIdFK = 1 AND SpecialityIdFK= 2. I tried the following but it doesn't seem to work.
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK IN (
SELECT PractitionerSpecialities.SpecialityIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK = 1
AND PractitionerSpecialities.SpecialityIdFK = 2
)
You can use GROUP BY and HAVING:
SELECT ps.PractitionerIdFK
FROM PractitionerSpecialities ps
WHERE ps.SpecialityIdFK IN (1, 2)
GROUP BY ps.PractitionerIdFK
HAVING COUNT(*) = 2; -- the size of the comparison list
This assumes that there are no duplicates in PractitionerSpecialities. If that is a possibility, then use HAVING COUNT(DISTINCT ps.SpecialityIdFK) = 2.
It can be achieved by using IN and BETWEEN operator in SQL .
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK in (1,2)
-- You can BETWEEN Clause as well ..
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK BETWEEN 1 AND 2
In Sub query use OR operator instead of AND .

simple subquery not working DB2

hey guys this is a very simple sql query that is not giving me the correct result.
subquery:
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
this subquery successfully returns a correct value 627809
simple query:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (627809)
this query executes properly and returns 4 rows.(4 addresses for a member)
but if I try to combine these queries in 1 query as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
then the query returns 0 rows. why is this happening?
Thanks
Your query looks OK, the only I can think is maybe you mistake the value on the result.
can you try this:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
and this
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
)
Since your Order (presumably) can only carry a single Member_ID -- can you please try your full query without the "IN", rather try an equal join as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID = (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)

LINQ: How do I perform an inline select statement in a LINQ statement?

I have the following SQL:
SELECT
*,
(SELECT COUNT(*) FROM WebOrder_URLHistory WHERE WebOrder_URLHistory.LeadID = Leads.LeadID) AS Sales
FROM
Leads
I want to put it (the select count statement) in this LINQ statement so I can get the sales count in my linq statement:
Dim TheLeads = (From L In DB.Leads Where L.IsDeleted = False Select L).ToList()
Is this possible to do in LINQ?
You can write Select L, L.UrlHistory.Count()