@Sinnbeck Sir in connection with my previous query
the last query is not working correctly so I tried to build it in a sub-query by removing the model name and using the DB instance as follows:
public function meetingSankey() {
$returnArray = array();
$result = DB::query()->fromSub(
DB::table('typeform_contacts')
->selectRaw("'Meetings Offered' AS Source, 'Meetings Chosen' AS Destination, COUNT(1) AS value, DATE(created_at) AS DATE")
->where('client_signed', 1)
->groupByRaw('DATE(created_at)')
->union(
DB::table('lawyer_opportunity_meetings')
->selectRaw("'Meetings Chosen' AS Source, '4A OR 4B' AS Destination, COUNT(1) AS Value, DATE(created_at) AS DATE")
->where('contact_type', 'typeform')
->groupByRaw('DATE(created_at)')
)
->union(
DB::table('dripfeed_targets')
->selectRaw("'4A OR 4B' AS Source, '4B Get L times' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE")
->whereIn('feed_id', [360, 361])
->groupByRaw('DATE(date_added)')
)
->union(
DB::table('dripfeed_targets')
->selectRaw("'4B Get L times' AS Source, '4C - Meeting Set' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE")
->whereIn('feed_id', [361])
->groupByRaw('DATE(date_added)')
)
->union(
DB::table('dripfeed_targets')
->selectRaw("'4C - Meeting Set' AS Source, '4D Follow up' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE")
->whereIn('feed_id', [362])
->groupByRaw('DATE(date_added)')
)
->union(
DB::table('dripfeed_targets')
->selectRaw("'4D Follow up' AS Source, '' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE")
->whereIn('feed_id', [363])
->groupByRaw('DATE(date_added)')
), 'temp')
->whereBetween('DATE', [Carbon::create(2023,1,1,0)->toDateTimeString(), Carbon::now()->toDateTimeString()])->get();
$returnArray['data'] = $result;
$returnArray['status'] = "success";
return response()->json($returnArray, 200);
}
Now when I am running this code it shows the following error although the column client_signed exist in the table typeform_contacts:
Illuminate\Database\QueryException: SQLSTATE[42703]: Undefined column: 7 ERROR: column "client_signed" does not exist
LINE 1: ...reated_at) AS DATE from "typeform_contacts" where "client_si...
^ (SQL: select * from ((select 'Meetings Offered' AS Source, 'Meetings Chosen' AS Destination, COUNT(1) AS value, DATE(created_at) AS DATE from "typeform_contacts" where "client_signed" = 1 group by DATE(created_at)) union (select 'Meetings Chosen' AS Source, '4A OR 4B' AS Destination, COUNT(1) AS Value, DATE(created_at) AS DATE from "lawyer_opportunity_meetings" where "contact_type" = typeform group by DATE(created_at)) union (select '4A OR 4B' AS Source, '4B Get L times' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE from "dripfeed_targets" where "feed_id" in (360, 361) group by DATE(date_added)) union (select '4B Get L times' AS Source, '4C - Meeting Set' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE from "dripfeed_targets" where "feed_id" in (361) group by DATE(date_added)) union (select '4C - Meeting Set' AS Source, '4D Follow up' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE from "dripfeed_targets" where "feed_id" in (362) group by DATE(date_added)) union (select '4D Follow up' AS Source, '' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE from "dripfeed_targets" where "feed_id" in (363) group by DATE(date_added))) as "temp" where "DATE" between 2023-01-01 00:00:00 and 2023-01-31 18:26:32) in file /opt/lampp/htdocs/multitenant/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671
When I am separately executing the query which is getting built:
select * from (select 'Meetings Offered' AS Source, 'Meetings Chosen' AS Destination, COUNT(1) AS value, DATE(created_at) AS DATE from "typeform_contacts" where "client_signed" = 1 group by DATE(created_at)
union (select 'Meetings Chosen' AS Source, '4A OR 4B' AS Destination, COUNT(1) AS Value, DATE(created_at) AS DATE from "lawyer_opportunity_meetings" where "contact_type" = typeform group by DATE(created_at))
union (select '4A OR 4B' AS Source, '4B Get L times' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE from "dripfeed_targets" where "feed_id" in (360, 361) group by DATE(date_added))
union (select '4B Get L times' AS Source, '4C - Meeting Set' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE from "dripfeed_targets" where "feed_id" in (361) group by DATE(date_added))
union (select '4C - Meeting Set' AS Source, '4D Follow up' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE from "dripfeed_targets" where "feed_id" in (362) group by DATE(date_added))
union (select '4D Follow up' AS Source, '' AS Destination, COUNT(1) AS Value, DATE(date_added) AS DATE from "dripfeed_targets" where "feed_id" in (363) group by DATE(date_added)) as "temp" )
where "DATE" between '2023-01-01 00:00:00' and '2023-01-31 18:12:57';
It throws an error as shown below:
ERROR: syntax error at or near "as"
LINE 6: ...ere "feed_id" in (363) group by DATE(date_added)) as "temp" ...
Kindly suggest if possible where am I going wrong.