Subselect OrderBy first row - oracle
I'm doing the below query but on the last subquery (mileage) I'm getting the following error due to the ORDER BY: "ORA-00907: missing right parenthesis", if I remove the ORDER BY it works well.
SELECT /* DATE OF THE ROUTE */
{GPS}.[DateTime],
/* ROUTE DESCRIPTION */
{Route}.[Description],
/* NAME OF THE DRIVER */
{Driver}.[Name],
/* VEHICLE LICENSE PLATE */
{Vehicle}.[Registration],
/* QUANTITY OF STOPS */
(SELECT COUNT({RouteStop}.[RouteId])
FROM {RouteStop}
WHERE {RouteStop}.[RouteId] = {GPS}.[RouteId]) AS StopCount,
/* AMOUNT OF FUEL */
(SELECT SUM(FUEL.[Value])
FROM {GPS} FUEL
WHERE {GPS}.[RouteId] = FUEL.[RouteId]
AND FUEL.[EventTypeId] = 23) FuelAmount, /* Event Fuel */
/* ROUTE STARTDATETIME */
{GPS}.[DateTime] AS ROUTESTARTDATETIME,
/* ROUTE ENDDATETIME */
(SELECT ROUTEENDDATETIME.[DateTime]
FROM {GPS} ROUTEENDDATETIME
WHERE {GPS}.[RouteId] = ROUTEENDDATETIME.[RouteId]
AND ROUTEENDDATETIME.[EventTypeId] = 5 /* Event Route Completed */
AND ROWNUM = 1) AS ROUTEEND,
/* INITIAL MILEAGE */
(SELECT MILEAGEBEGIN.[Value]
FROM {GPS} MILEAGEBEGIN
WHERE {GPS}.[RouteId] = MILEAGEBEGIN.[RouteId]
AND MILEAGEBEGIN.[EventTypeId] = 21 /* Event Mileage */
AND ROWNUM = 1
ORDER BY MILEAGEBEGIN.[DateTime]
) AS INITIALMILEAGE
FROM {GPS}
INNER JOIN {Route}
ON {GPS}.[RouteId] = {Route}.[Id]
INNER JOIN {Driver}
ON {GPS}.[DriverId] = {Driver}.[Id]
INNER JOIN {Availability}
ON {Driver}.[Id] = {Availability}.[DriverId]
INNER JOIN {Vehicle}
ON {Availability}.[VehicleId] = {Vehicle}.[Id]
WHERE {GPS}.[EventTypeId] = 3 /* Event RouteStarted */
I tried in the following way but I get this error: "ORA-00936: missing expresion".
SELECT /* DATE OF THE ROUTE */
{GPS}.[DateTime],
/* INITIAL MILEAGE */
SELECT TEST,'more test' FROM (SELECT MILEAGEBEGIN.[Value] AS TEST
FROM {GPS} MILEAGEBEGIN
WHERE {GPS}.[RouteId] = MILEAGEBEGIN.[RouteId]
AND MILEAGEBEGIN.[EventTypeId] = 21 /* Event Mileage */
ORDER BY MILEAGEBEGIN.[DateTime] ASC
)
WHERE ROWNUM = 1 AS INITIALMILEAGE
FROM {GPS}
WHERE {GPS}.[EventTypeId] = 3 /* Event RouteStarted */
Remove the brackets,
SELECT /* DATE OF THE ROUTE */
GPS.DateTime,
/* ROUTE DESCRIPTION */
Route.Description,
/* NAME OF THE DRIVER */
Driver.Name,
/* VEHICLE LICENSE PLATE */
Vehicle.Registration,
/* QUANTITY OF STOPS */
(SELECT COUNT(RouteStop.RouteId)
FROM RouteStop
WHERE RouteStop.RouteId = GPS.RouteId) AS StopCount,
/* AMOUNT OF FUEL */
(SELECT SUM(FUEL.Value)
FROM GPS FUEL
WHERE GPS.RouteId = FUEL.RouteId
AND FUEL.EventTypeId = 23) FuelAmount, /* Event Fuel */
/* ROUTE STARTDATETIME */
GPS.DateTime AS ROUTESTARTDATETIME,
/* ROUTE ENDDATETIME */
(SELECT ROUTEENDDATETIME.DateTime
FROM GPS ROUTEENDDATETIME
WHERE GPS.RouteId = ROUTEENDDATETIME.RouteId
AND ROUTEENDDATETIME.EventTypeId = 5 /* Event Route Completed */
AND ROWNUM = 1) AS ROUTEEND,
/* INITIAL MILEAGE */
(SELECT MILEAGEBEGIN.Value
FROM GPS MILEAGEBEGIN
WHERE GPS.RouteId = MILEAGEBEGIN.RouteId
AND MILEAGEBEGIN.EventTypeId = 21 /* Event Mileage */
AND ROWNUM = 1
ORDER BY MILEAGEBEGIN.DateTime
) AS INITIALMILEAGE
FROM GPS
INNER JOIN Route
ON GPS.RouteId = Route.Id
INNER JOIN Driver
ON GPS.DriverId = Driver.Id
INNER JOIN Availability
ON Driver.Id = Availability.DriverId
INNER JOIN Vehicle
ON Availability.VehicleId = Vehicle.Id
WHERE GPS.EventTypeId = 3 /* Event RouteStarted */
The Other query:
SELECT /* DATE OF THE ROUTE */
GPS.DateTime,
/* INITIAL MILEAGE */
( SELECT TEST FROM (SELECT MILEAGEBEGIN.Value AS TEST
FROM GPS MILEAGEBEGIN
WHERE GPS.RouteId = MILEAGEBEGIN.RouteId
AND MILEAGEBEGIN.EventTypeId = 21 /* Event Mileage */
ORDER BY MILEAGEBEGIN.DateTime ASC
)
WHERE ROWNUM = 1) AS INITIALMILEAGE
FROM GPS
WHERE GPS.EventTypeId = 3 /* Event RouteStarted */
Correct query
SELECT /* ROUTEID */
ROUTES.[RouteId] AS ROUTEID,
/* ROUTE STARTDATETIME */
ROUTES.[DateTime] AS ROUTESTARTDATETIME,
/* ROUTE DESCRIPTION */
{Route}.[Description],
/* NAME OF THE DRIVER */
{Driver}.[Name],
/* VEHICLE LICENSE PLATE */
{Vehicle}.[Registration],
/* QUANTITY OF STOPS */
(SELECT COUNT({RouteStop}.[RouteId])
FROM {RouteStop}
WHERE {RouteStop}.[RouteId] = ROUTEID) AS STOPCOUNT,
/* AMOUNT OF FUEL */
(SELECT SUM(FUEL.[Value])
FROM {GPS} FUEL
WHERE ROUTES.[AvailabilityId] = FUEL.[AvailabilityId]
AND TRUNC(ROUTES.[DateTime]) = TRUNC(FUEL.[DateTime])
AND FUEL.[EventTypeId] = #FuelEventTypeId) AS FUELAMOUNT,
/* ROUTE ENDDATETIME */
(SELECT ROUTEENDDATETIME.[DateTime]
FROM {GPS} ROUTEENDDATETIME
WHERE ROUTEID = ROUTEENDDATETIME.[RouteId]
AND ROUTEENDDATETIME.[EventTypeId] = #RouteCompletedEventTypeId
AND ROWNUM = 1) AS ROUTEEND,
/* INITIAL MILEAGE */
(SELECT INITIALMILEAGE
FROM (SELECT MILEAGEBEGIN.[Value] AS INITIALMILEAGE
FROM {GPS} MILEAGEBEGIN
WHERE ROUTES.[AvailabilityId] = MILEAGEBEGIN.[AvailabilityId]
AND TRUNC(ROUTES.[DateTime]) = TRUNC(MILEAGEBEGIN.[DateTime])
AND MILEAGEBEGIN.[EventTypeId] = #MileageEventTypeId
ORDER BY MILEAGEBEGIN.[DateTime] ASC
)
WHERE ROWNUM = 1),
/* FINAL MILEAGE */
(SELECT FINALMILEAGE
FROM (SELECT MILEAGEEND.[Value] AS FINALMILEAGE
FROM {GPS} MILEAGEEND
WHERE ROUTES.[AvailabilityId] = MILEAGEEND.[AvailabilityId]
AND TRUNC(ROUTES.[DateTime]) = TRUNC(MILEAGEEND.[DateTime])
AND MILEAGEEND.[EventTypeId] = #MileageEventTypeId
ORDER BY MILEAGEEND.[DateTime] DESC
)
WHERE ROWNUM = 1)
FROM {GPS} ROUTES
INNER JOIN {Route}
ON ROUTES.[RouteId] = {Route}.[Id]
INNER JOIN {Availability}
ON ROUTES.[AvailabilityID] = {Availability}.[Id]
INNER JOIN {Driver}
ON {Availability}.[DriverId] = {Driver}.[Id]
INNER JOIN {Vehicle}
ON {Availability}.[VehicleId] = {Vehicle}.[Id]
WHERE ROUTES.[EventTypeId] = #RouteStartedEventTypeId
Related
procedure failurecompilation
I have an existing procedure which creates a table adwstg.switchhold_notificatn_stg Suddenly it is throwing error in creating the table. I didn't change anything the statement. First I thought the error is with ''No'' . So find & replace '' with ' But it is throwing error at line: nvl(ba300_z51.sent_650_01, 'No') sent_650_01, error(27,29): PLS-00103: Encountered the symbol "NO" when expecting one of the following: * & = - + ; < / > at in is mod remainder not rem <> or != or ~= >= <= <> and or like like2 like4 likec between || multiset member submultiset*** Below is the query : create table adwstg.switchhold_notificatn_stg nologging parallel (degree 8) compress as select distinct bad3700.createdon, bad3700.uc_pod_ext, bpc.cacont_acc, bad3700.notificatn, bad3700.nfcat_code, nvl(ba300_z51.sent_650_01, ''No'') sent_650_01, decode(ba300_z51.sent_650_01,''No'',''--'',''Yes'',to_char(ba300_z51.ucswmsgdat,''yyyymmdd''),''--'') date_sent_650_01, nvl(to_char(ba300_z51.ucswmsgtim,''hh24:mi:ss''),''--'') time_sent_650_01, nvl(ba300_z52.received_650_02, ''No'') received_650_02, decode(ba300_z52.received_650_02,''No'',''--'',''Yes'',to_char(ba300_z52.ucswmsgdat,''yyyymmdd''),''--'') date_received_650_02, nvl(to_char(ba300_z52.ucswmsgtim,''hh24:mi:ss''),''--'') time_received_650_02, nvl(ba300_z20.received_814_20, ''No'') received_814_20, decode(ba300_z20.received_814_20,''No'',''--'',''Yes'',to_char(ba300_z20.ucswmsgdat,''yyyymmdd''),''--'') date_received_814_20, nvl(to_char(ba300_z20.ucswmsgtim,''hh24:mi:ss''),''--'') time_received_814_20, case when trim(bad3700.nfcat_code) = ''SH01'' and zet.ext_ui is not null then ''ADDED'' when trim(bad3700.nfcat_code) = ''SH01'' and zet.ext_ui is null then ''NOT PROCESSED'' when trim(bad3700.nfcat_code) in (''SH02'',''SH03'') and zet.ext_ui is null then ''REMOVED'' when trim(bad3700.nfcat_code) in (''SH02'',''SH03'') and zet.ext_ui is not null then ''NOT PROCESSED'' else ''NOT PROCESSED'' end work_order_check from (select distinct * from (select trunc(createdon) createdon, trim(notificatn) notificatn, trim(uc_pod_ext) uc_pod_ext, trim(not_type) not_type, trim(nfcat_code) nfcat_code, row_number () over (partition by trim(uc_pod_ext),trunc(createdon) order by trim(notificatn) desc) rnum from birpt.bic_azfc_ds3700 where upper(trim(not_type)) = ''SH'' and trim(bic_zdiscstat) = ''E0010'') where rnum = 1 ) bad3700 left outer join ( select distinct ucinstalla, uc_pod_ext, datefrom, dateto from birpt.bi0_qucinstalla where objvers = ''A'' ) bqi on (trim(bad3700.uc_pod_ext) = trim(bqi.uc_pod_ext) and trunc(bad3700.createdon) between bqi.datefrom and bqi.dateto) left outer join ( select distinct cacont_acc, ucinstalla, ucmovein_d, ucmoveoutd from birpt.bi0_puccontract where objvers = ''A'' ) bpc on (trim(bqi.ucinstalla) = trim(bpc.ucinstalla) and trunc(bad3700.createdon) between bpc.ucmovein_d and bpc.ucmoveoutd) left outer join (select distinct * from (select trim(ucswtpodex) ucswtpodex, trunc(ucswmsgdat) ucswmsgdat, ucswmsgtim, case --650_01 CHECK when trim(uc_mdcat) = ''Z51'' then ''Yes'' else ''No'' end sent_650_01, row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum from birpt.bic_azudeds0300 where trim(uc_mdcat) = ''Z51'') where rnum = 1) ba300_z51 on (trim(bad3700.uc_pod_ext) = trim(ba300_z51.ucswtpodex) and trunc(bad3700.createdon)= trunc(ba300_z51.ucswmsgdat)) left outer join (select distinct * from (select trim(ucswtpodex) ucswtpodex, trunc(ucswmsgdat) ucswmsgdat, ucswmsgtim, case --650_02 CHECK when trim(uc_mdcat) = ''Z52'' then ''Yes'' else ''No'' end received_650_02, row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum from birpt.bic_azudeds0300 where trim(uc_mdcat) = ''Z52'') where rnum = 1) ba300_z52 on (trim(bad3700.uc_pod_ext) = trim(ba300_z52.ucswtpodex) and trunc(bad3700.createdon)= trunc(ba300_z52.ucswmsgdat)) left outer join (select distinct * from (select trim(ucswtpodex) ucswtpodex, trunc(ucswmsgdat) ucswmsgdat, ucswmsgtim, case --814_20 CHECK when trim(uc_mdcat) = ''Z20'' then ''Yes'' else ''No'' end received_814_20, row_number () over (partition by trim(ucswtpodex), trunc(ucswmsgdat) order by trunc(ucswmsgdat) desc, ucswmsgtim desc) rnum from birpt.bic_azudeds0300 where trim(uc_mdcat) = ''Z20'') where rnum = 1) ba300_z20 on (trim(bad3700.uc_pod_ext) = trim(ba300_z20.ucswtpodex) and trunc(bad3700.createdon)= trunc(ba300_z20.ucswmsgdat)) left outer join (select distinct ext_ui from isurpt.zcs_esiid_tamper ) zet on (trim(bad3700.uc_pod_ext) = trim(zet.ext_ui));
If you remove all doubled single quotes (perform replace function in any text editor), your code will be valid (as far as syntax is concerned). I don't have your tables to test it, and there are too many of them with too many columns to create a test case by myself. The question is: why did you double them in the first place?
Multiple whereHas calls are not being aggregated together
I've got multiple calls to whereHas() on an instance of \Illuminate\Database\Query\Builder ($cars): $cars->whereHas("finance", function (Eloquent\Builder $query) { $query->where('term'...) } $cars->whereHas("finance", function (Eloquent\Builder $query) { $query->where('payment'...) } Is there some way to aggregate the where(s) together without needing to do all the where calls within the containing whereHas? The SQL query being executed: SELECT id FROM `cars` WHERE EXISTS (SELECT `finance`.`payment` as payment FROM `finance` INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id` WHERE `car_finance`.`car_id` = `cars`.`id` AND `payment` >= 50) AND EXISTS (SELECT * FROM `finance` INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id` WHERE `car_finance`.`car_id` = `cars`.`id` AND `payment` <= 200) AND EXISTS (SELECT * FROM `finance` INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id` WHERE `car_finance`.`car_id` = `cars`.`id` AND `term` = 48) AND EXISTS (SELECT * FROM `finance` INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id` WHERE `car_finance`.`car_id` = `cars`.`id` AND `deposit` = 1000) AND `active` = 1 The SQL query that I would like to be executed: SELECT * FROM cars WHERE EXISTS (SELECT * FROM `finance` INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id` WHERE `car_finance`.`car_id` = `cars`.`id` AND deposit = 1000 AND term = 48 AND payment >= 50 AND payment <= 200) AND active = 1
Your question it's not clear but I think you want this: $cars->whereHas("finance", function ($query) { $query->where('deposit', 1000)->where('term', 48) ->whereBetween('payment', 50, 200); })->where('active', 1)->get();
Add row num not working in oracle
I want to add rownum in my below oracle query but it is giving me error as ORA-30484: missing window specification for this function Here is my query SELECT ROW_NUMBER () AS sr_no, pn.lease_num, hz.party_name, flt.location_code, flt.office flat_no, NULL action, la.no_of_days, NULL remarks, flt.location_id flat_id, pn.lease_id FROM xxcus.xxacl_pn_leases_all la, pn_leases_all pn, (SELECT * FROM pn_locations_all flat WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) bld, (SELECT * FROM pn_locations_all flat WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) flr, (SELECT * FROM pn_locations_all flat WHERE SYSDATE BETWEEN flat.active_start_date AND flat.active_end_date) flt, pn_properties_all prop, hz_parties hz, apps.hz_cust_accounts sc1 WHERE la.lease_id = pn.lease_id AND pn.location_id = flt.location_id AND flt.parent_location_id = flr.location_id AND flr.parent_location_id = bld.location_id AND bld.property_id = prop.property_id AND pn.customer_id = sc1.cust_account_id AND sc1.party_id = hz.party_id AND la.type_of_booking = 50 AND prop.property_id = '1' AND bld.location_id = '1309' kindly help what is wrong I am using ORACLE
See documentation for ROW_NUMBER. You need to write something like: SELECT ROW_NUMBER() OVER (PARTITION BY la.type_of_booking ORDER BY la.lease_id) FROM xxcus.xxacl_pn_leases_all la or SELECT ROW_NUMBER() OVER (ORDER BY la.lease_id) FROM xxcus.xxacl_pn_leases_all la
ORA-01861: literal does not match format string in package function
I have the following select with the returned result of one row: SELECT * FROM (SELECT (SELECT dokdatum FROM dokumente WHERE dokid = '00100002LNWCAJ') AS tckdatum, aktzeitdatum aktdatum, (SELECT tckdokzeit FROM tickets WHERE tckid = '00100000000ICQ') tckzeit, (hh * 60 + mm) * 60 AS aktzeit, firmaid FROM (SELECT TO_DATE (aktdatumuhrzeitunitup, 'DD.MM.YYYY' ) aktzeitdatum, a.firmaid, TO_NUMBER (TO_CHAR (aktdatumuhrzeitunitup, 'HH24') ) hh, TO_NUMBER (TO_CHAR (aktdatumuhrzeitunitup, 'MI')) mm FROM aktivitaeten a, aktivitaetenarten aa WHERE a.aktartid = aa.aktartid(+) /* downtime terminated */ AND aktunitup = 1 AND tckid = '00100000000ICQ' AND (aktartstatistik IS NULL OR aktartstatistik = 1) AND a.aktdatumuhrzeitunitup IS NOT NULL) ORDER BY ((aktdatum - tckdatum) * 24 * 60 * 60 + (aktzeit - tckzeit) ) DESC) WHERE ROWNUM < 2; Result is: 11.03.2016 || 11.03.2016 || 41334 || 41940 || 001 This statement is executable without errors. But when I try to use it in a package function with FUNCTION xy ... IS CURSOR c_cdt_aktdatumuhrzeitunitup IS --the above select r_cdt_aktdatumuhrzeitunitup c_cdt_aktdatumuhrzeitunitup%ROWTYPE; BEGIN OPEN c_cdt_aktdatumuhrzeitunitup; --Exception is thrown with this statement FETCH c_cdt_aktdatumuhrzeitunitup INTO r_cdt_aktdatumuhrzeitunitup; Then I receive the ORA-01861: literal does not match format string error on the FETCH...INTO line Does anyone know why?
Laravel, why do I have so many queries?
Controller $attendees = Attendee::with('User')->get(); return View::make('admin.attendees.index', compact('attendees')); Attendee model public function user() | if( !( $user->hasRole('admin') || $user->hasRole('programmer') )) { | return Redirect::to('/'); return $this->belongsTo('User'); | } View #foreach($attendees as $attendee) <td>{{link_to_route('admin.users.show', $attendee->user->username, $attendee->user->id)}}</td> #endforeach 223 queries select * from `users` where `users`.`id` = '4' limit 1600μs select `roles`.*, `assigned_roles`.`user_id` as `pivot_user_id`, `assigned_roles`.`role_id` as `pivot_role_id` from `roles` inner join `assigned_roles` on `roles`.`id` = `assigned_roles`.`role_id` where `assigned_roles`.`user_id` = '4'630μs select * from `attendees`1.24ms select * from `users` where `users`.`id` in ('5', '1', '3', '8', '9', '10')780μs select * from `users` where `users`.`id` = '5' limit 1680μs select * from `users` where `users`.`id` = '5' limit 1650μs select * from `users` where `users`.`id` = '5' limit 1680μs select * from `users` where `users`.`id` = '5' limit 1590μs select * from `users` where `users`.`id` = '1' limit 1 <continues like so for each user id> I am using phpdebugbar to show the queries. Migration Schema::table('attendees', function(Blueprint $table) { $table->foreign('user_id')->references('id')->on('users') ->onDelete('cascade') ->onUpdate('no action'); Am I doing something wrong that is causing the query to be run over and over again?
The eager load should be the name of the relationship function, not the relationship's model, and it's apparently case sensitive: $attendees = Attendee::with('user')->get();