How to group by with union in laravel - laravel

I have two table that I want to union and after I union them I want to groupBy the one column that I used in union
Here is what I tried:
$issuance = DB::table('issuance as i')
->select('i.issue_to as stud_id', 'i.created_on');
$stud= DB::table('transfer as t')
->select('t.transfer_to as stud_id', 't.created_on')
->union($issuance)
->select('stud_id', 'created_on', DB::raw('COUNT(*) as total_asset'))
->groupBy('stud_id')
->orderBy('created_on', 'DESC')->get();
This is the MySQL query in what I tried
"(select `stud_id`, `created_on`, COUNT(*) as total_asset from `transfer` as
`t` group by `stud_id`) union (select `i`.`issued_to` as `stud_id`, `i`.`created_on` from
`issuance` as `i`) order by `created_on` desc"
What I really want in MySQL is like this:
select stud_id, count(*) from ((select `t`.`transfered_to` as `stud_id`,
`t`.`created_on` from `transfer` as `t`) union (select `i`.`issued_to` as
`stud_id`, `i`.`created_on`, COUNT(*) as asset from `issuance` as `i`)) as t
group by stud_id order by `created_on` desc
Thank you for the help

//try this example ,it will helps you
$query1 = DB::table('table1')->where(....);
$query2 = DB::table('table2')->where(....);
$data = DB::select(DB::raw('id, MAX(created_at) as max_created_at')>union($query1)->union($query2)->orderBy('max_created_at')->groupBy('id')->get();

Related

How To Use WITH RECURSIVE in Laravel

I have a query but i don't know how to run in Laravel
WITH RECURSIVE category_path (id, name, lvl) AS(SELECT id, name, 0 lvl FROM category WHERE parent =1 UNION ALL SELECT c.id, c.name,cp.lvl + 1 FROM category_path AS cp JOIN category AS c ON cp.id = c.parent) SELECT * FROM category_path GROUP BY lvl ORDER BY lvl DESC limit 1
You can use DB::select('your query here');
here is official link to doc laravel
I Think You want to use Raw Query You Can try this:
DB::select(DB::raw('your query here'));

How to use Union query with Laravel Eloquent?

This MySQL query Has union and Normalized I don't Know how to write laravel Query.
SELECT DISTINCT val
FROM
(
SELECT 'date' date_type, date val FROM user_presentation
 UNION
SELECT 'accept_date', accept_date FROM user_presentation
UNION
SELECT 'question_date', question_date FROM user_presentation
UNION
SELECT 'success_date', success_date FROM user_presentation
) normalized;
You may chain a series of unions in your Laravel code:
$first = DB::table('user_presentation')
->select('date');
$second = DB::table('user_presentation')
->select('accept_date');
$third = DB::table('user_presentation')
->select('question_date');
$fourth = DB::table('user_presentation')
->select('success_date AS val')
->union($first)
->union($second)
->union($third)
->get();
Note the above code actually corresponds to this query:
SELECT date AS val FROM user_presentation UNION
SELECT accept_date FROM user_presentation UNION
SELECT question_date FROM user_presentation UNION
SELECT success_date FROM user_presentation;
Also, the DISTINCT subquery you have should not be necessary, as the union query itself should remove all duplicate date values.
$userP = UserPresentation::get()
->only('date', 'accept_date', 'question_date', 'success_date')
->unique();
The 100% Laravel Way, using Collection, this one will select all four dates together, and Laravel Collection will find the unique rows from these selections.

How to join on a select result with eloquent in Laravel?

I want to join on a result of other select like this :
SELECT *
FROM TABLE1
JOIN (
SELECT cat_id FROM TABLE2 where brand_id = 2 GROUP BY TABLE2.cat_id) AS b ON TABLE1.id = b.cat_id
is there any way to do this with eloquent?
As it is mentioned here, using DB:raw() will solve your problem.
DB::table('table1')->join(DB::raw("(SELECT
cat_id
FROM table2
WHERE brand_id = 2
GROUP BY table2.cat_id
) as b"),function($join){
$join->on("b.cat_id","=","table1.id");
})->get();
\DB::table('table1')->join('table2' , function($join){
$join->on('table1.id', '=', 'table2.cat_id');
})->select(['table2.cat_id' , 'table1.*'])
->where('table2.brand_id' , '=' , '2')
->groupBy('table2.cat_id');
Depends on whether brand_id is in table1 or table2
You can also use model approach for it.
TABLE1::join('table2' , function($join){
$join->on('table1.id', '=', 'table2.cat_id');
})->select(['table2.cat_id' , 'table1.*'])
->where('table2.brand_id' , '=' , '2')
->groupBy('table2.cat_id');

DQL subquery - is already defined

I'm trying to reuse the definition name "comments" - table Comments (AppBundle:Comments entity) in the 5th DQL subquery, but I get the error: "Error: 'comments' is already defined."
Here is the DQL query:
SELECT employer.name AS employer_name, employer.position AS employer_position, employer.id AS employer_id,
(SELECT company.company_name FROM Application\Sonata\UserBundle\Entity\User company WHERE company.id = employer.company) AS company_name,
(SELECT city.name FROM AppBundle:City city WHERE city.id = employer.city) AS city_name,
(SELECT MAX(comments.id) FROM AppBundle:Comments comments WHERE comments.employer = employer_id) AS comment_id,
(SELECT commenttype.name FROM AppBundle:CommentTypes commenttype WHERE commenttype.id = comment_id) AS comment_name,
(SELECT COUNT(comments.id) FROM AppBundle:Comments comments WHERE comments.employer = employer_id) AS comment_count
FROM AppBundle:Employer employer
How can I reuse the definition without redefining it?
I tried also with this:
SELECT employer.name AS employer_name, employer.position AS employer_position, employer.id AS employer_id,
(SELECT company.company_name FROM Application\Sonata\UserBundle\Entity\User company WHERE company.id = employer.company) AS company_name,
(SELECT city.name FROM AppBundle:City city WHERE city.id = employer.city) AS city_name,
(SELECT MAX(comments.id) FROM AppBundle:Comments comments WHERE comments.employer = employer_id) AS comment_id,
(SELECT commenttype.name FROM AppBundle:CommentTypes commenttype WHERE commenttype.id = comment_id) AS comment_name,
(SELECT COUNT(comments.id) FROM comments WHERE comments.employer = employer_id) AS comment_count
FROM AppBundle:Employer employer
, but now I get "Class 'comments' is not defined." error.
Seems a duplicate alias in the first query.
Try this:
SELECT employer.name AS employer_name, employer.position AS employer_position, employer.id AS employer_id,
(SELECT company.company_name FROM Application\Sonata\UserBundle\Entity\User company WHERE company.id = employer.company) AS company_name,
(SELECT city.name FROM AppBundle:City city WHERE city.id = employer.city) AS city_name,
(SELECT MAX(comments.id) FROM AppBundle:Comments comments WHERE comments.employer = employer_id) AS comment_id,
(SELECT commenttype.name FROM AppBundle:CommentTypes commenttype WHERE commenttype.id = comment_id) AS comment_name,
(SELECT COUNT(aComments.id) FROM AppBundle:Comments aComments WHERE aComments.employer = employer_id) AS comment_count
FROM AppBundle:Employer employer
Hope this help

Group by not working in Oracle -

When I run the below query it works fine
select TO_CHAR(mydate ,'YYYYMMDD') , count(*) from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
But when I try to order by it fails
select TO_CHAR(mydate ,'YYYYMMDD') , count(*) from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
order by mydate desc
Similarly when I try to do any arithmatic on the date I get the same error.
select TO_CHAR(mydate +10/24 ,'YYYYMMDD') , count(*) from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
order by mydate desc
Can someone advise what is the best way to order by and add the 10 hours?
Try this:
select TO_CHAR(mydate ,'YYYYMMDD') from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
order by mydate desc
ie, you need to put all the columns of your select in the group by to make it working.
EDIT:-
select TO_CHAR(mydate ,'YYYYMMDD') , count(someID) from table
where columnA = '123'
group by TO_CHAR(mydate,'YYYYMMDD')
order by mydate desc
having count(someID)>1

Resources