missing expression in Select SQL - sql

I'm new to SQL and sub queries, When I run the sub query by itself I get the correct data output. But when I run the full query I get the error message
SELECT * Error at line 3: ORA-00936: missing expression
Here's my code:
SELECT
MAX(
SELECT
SUM(
ALLOCATION.HourlyRate
*
ACTION.HrsWorked
)
FROM ALLOCATION
INNER JOIN ACTION
ON ((ACTION.ActId = ALLOCATION.ActId) AND (ACTION.EmpId = ALLOCATION.EmpId))
GROUP BY (ALLOCATION.ActId)
)
FROM ALLOCATION
GROUP BY (ALLOCATION.ActId)
SOLUTION:
SELECT MAX(sum_total_pay)
FROM
(
SELECT SUM(ALLOCATION.HourlyRate * ACTION.HrsWorked) AS sum_total_pay
FROM ALLOCATION
INNER JOIN ACTION
ON ((ACTION.ActId = ALLOCATION.ActId) AND (ACTION.EmpId = ALLOCATION.EmpId))
GROUP BY (ALLOCATION.ActId)
);

You needs two parentheses more :)
SELECT MAX ( ( SELECT SUM (ALLOCATION.HourlyRate * ACTION.HrsWorked)
FROM ALLOCATION INNER JOIN ACTION ON (ACTION.ActId = ALLOCATION.ActId) AND (ACTION.EmpId = ALLOCATION.EmpId)
GROUP BY (ALLOCATION.ActId)))
FROM ALLOCATION
GROUP BY ALLOCATION.ActId
The Select into MAX have to be in paranthesis.
The logic is not clear. This give you the total pay per activity:
SELECT ALLOCATION.ActId, SUM (ALLOCATION.HourlyRate * ACTION.HrsWorked) total_pay
FROM ALLOCATION b INNER JOIN ACTION ON ACTION.ActId = ALLOCATION.ActId AND ACTION.EmpId = ALLOCATION.EmpId
GROUP BY ALLOCATION.ActId
If you want to see, which activity has the biggest payment, then order by desc :
SELECT *
FROM ( SELECT ALLOCATION.ActId, SUM (ALLOCATION.HourlyRate * ACTION.HrsWorked) total_pay
FROM ALLOCATION b INNER JOIN ACTION ON ACTION.ActId = ALLOCATION.ActId AND ACTION.EmpId = ALLOCATION.EmpId
GROUP BY ALLOCATION.ActId)
ORDER BY total_pay DESC

Related

Select the latest client record from query with many joins

I have a query with many joins and I am trying to select the latest client record from it. This is the query I have but it is giving me all records of the client:
EDIT:
SELECT ca.CA_ID, ca.CLIENT_ID, ca.CA_STATUS_TYPE, ca.CREATED_BY, MAX(ca.CREATED_DATE), ca.UPDATED_BY, MAX(ca.UPDATED_DATE), ca.CA_REFERENCE_NUMBER,
cl.SOURCE_SYSTEM_CODE, catchment.EST_CATCHMENT_AREA, catchment.SERVICE_PROVIDER_NAME, sds.SDS_NAME, sds.SDS_ADDRESS_TEXT, u.CA_USER_TYPE "USER TYPE",
CASE WHEN ca.REFERRED_IND = 1 THEN 'REFERRED' ELSE 'NOT REFERRED' END REFER_TYPE
FROM CA ca
JOIN(
SELECT TOP 1
CLIENT_ID, MAX(CREATED_DATE) CREATED_DATE
FROM CA
GROUP BY
CLIENT_ID
ORDER BY
MAX(CREATED_DATE)
) maxclient
ON maxclient.CLIENT_ID = ca.CLIENT_ID
AND maxclient.CREATED_DATE = ca.CREATED_DATE
LEFT JOIN CLIENT cl
ON ca.CLIENT_ID = cl.CLIENT_ID
LEFT JOIN (
SELECT * FROM CATCHMENT_AREA catchment LEFT JOIN SERVICE_PROVIDER sp ON sp.SERVICE_PROVIDER_ID = catchment.SERVICE_PROVIDER_ID
) catchment ON ca.CATCHMENT_ID = catchment.CATCHMENT_ID
LEFT JOIN SERVICE_DELIVERY_SITE sds
ON ca.SERVICE_DELIVERY_SITE_ID = sds.SERVICE_DELIVERY_SITE_ID
LEFT JOIN USER u
ON ca.CREATED_BY = u.CA_USER_ID
WHERE (ca.CREATED_DATE) BETWEEN TO_DATE('2021-01-04', 'yyyy-mm-dd') AND TO_DATE('2021-01-08', 'yyyy-mm-dd')
Oracle don't have the top clause:
If you want the max created date from all est_ca_client_id you do:
select est_ca_client_id, max(created_date) created_date
from ca_cdr.est_ca
group by est_ca_client_id
But if you want the last est_ca_client_id from the created_date, assuming you don't have clients created in the same second you can do something like:
select est_ca_client_id
from ca_cdr.est_ca
where created_date = (select max(created_date)
from ca_cdr.est_ca)

ORACLE SQL: Slow query when using "join table on id = id" vs "where id = number"

I'm having performance problem in a querie when I use a subquery to set an ID = number, and then join that subquery in the main query to look for that ID, this method takes about 150 seconds. But if I delete the subquery and look for the ID = number directly in the main query, it takes 0,5 second.
Here some code as exemple:
This is the example of 150 seconds
In this I set the cto_in_codigo in the With clause.
WITH CONTRATOS AS (
SELECT CTO_IN_CODIGO FROM MGCAR.CAR_CONTRATO
WHERE CTO_IN_CODIGO = 14393
)
SELECT
PT.PAR_IN_CODIGO,
PTC.PARCOR_IN_INDICE
FROM (
SELECT
MAX(PT.HPAR_IN_CODIGO) OVER (PARTITION BY PT.PAR_IN_CODIGO, PT.CTO_IN_CODIGO) HPAR_IN_CODIGO_MAX,
PT.HPAR_IN_CODIGO,
PT.CTO_IN_CODIGO,
PT.PAR_IN_CODIGO
FROM
QUERIE.PARCELA_TOTAL PT
JOIN CONTRATOS CTO
ON CTO.CTO_IN_CODIGO = PT.CTO_IN_CODIGO
WHERE
PT.PAR_DT_REAJUSTE <= TO_DATE('31/12/2017', 'DD/MM/YYYY')
) PT
LEFT OUTER JOIN (
SELECT
MAX(PTC.PARCOR_IN_CODIGO) OVER (PARTITION BY PTC.PAR_IN_CODIGO, PTC.CTO_IN_CODIGO) PARCOR_IN_CODIGO_MAX,
PTC.PARCOR_IN_CODIGO,
PTC.CTO_IN_CODIGO,
PTC.PAR_IN_CODIGO,
PTC.HPAR_IN_CODIGO,
PTC.PARCOR_IN_INDICE
FROM
QUERIE.PARCELA_TOTAL_CORRECAO PTC
JOIN CONTRATOS CTO
ON CTO.CTO_IN_CODIGO = PTC.CTO_IN_CODIGO
) PTC
ON PTC.CTO_IN_CODIGO = PT.CTO_IN_CODIGO
AND PTC.PAR_IN_CODIGO = PT.PAR_IN_CODIGO
AND PTC.HPAR_IN_CODIGO = PT.HPAR_IN_CODIGO
AND PTC.PARCOR_IN_CODIGO = PTC.PARCOR_IN_CODIGO_MAX
WHERE
PT.HPAR_IN_CODIGO = PT.HPAR_IN_CODIGO_MAX
and this is the 0,5 sec.
in this I set the cto_in_codigo inside each query
SELECT
PT.PAR_IN_CODIGO,
PTC.PARCOR_IN_INDICE
FROM (
SELECT
MAX(PT.HPAR_IN_CODIGO) OVER (PARTITION BY PT.PAR_IN_CODIGO, PT.CTO_IN_CODIGO) HPAR_IN_CODIGO_MAX,
PT.HPAR_IN_CODIGO,
PT.CTO_IN_CODIGO,
PT.PAR_IN_CODIGO
FROM
QUERIE.PARCELA_TOTAL PT
WHERE
PT.PAR_DT_REAJUSTE <= TO_DATE('31/12/2017', 'dd/MM/yyyy')
AND PT.CTO_IN_CODIGO = 14393
) PT
LEFT OUTER JOIN (
SELECT
MAX(PTC.PARCOR_IN_CODIGO) OVER (PARTITION BY PTC.PAR_IN_CODIGO, PTC.CTO_IN_CODIGO) PARCOR_IN_CODIGO_MAX,
PTC.PARCOR_IN_CODIGO,
PTC.CTO_IN_CODIGO,
PTC.PAR_IN_CODIGO,
PTC.HPAR_IN_CODIGO,
PTC.PARCOR_IN_INDICE
FROM
QUERIE.PARCELA_TOTAL_CORRECAO PTC
WHERE
PTC.CTO_IN_CODIGO = 14393
) PTC
ON PTC.CTO_IN_CODIGO = PT.CTO_IN_CODIGO
AND PTC.PAR_IN_CODIGO = PT.PAR_IN_CODIGO
AND PTC.HPAR_IN_CODIGO = PT.HPAR_IN_CODIGO
AND PTC.PARCOR_IN_CODIGO = PTC.PARCOR_IN_CODIGO_MAX
WHERE
PT.HPAR_IN_CODIGO = PT.HPAR_IN_CODIGO_MAX
what is confusing to me is that the with clause returns just one row with the cto_in_codigo number, much like if I hard code then inside each query like the second code. What is could be causing this super delay?

Only month end values for each identifier

For the code below I am attempting to select only the month end values for all unique FundId's. The code below keeps giving me the error of
Msg 164, Level 15, State 1, Line 16
Each GROUP BY expression must contain at least one column that is not an outer reference.
How can I fix the where statement to pull all month end values for each fundid
SELECT TOP 10000 a.[PerformanceId]
,[InvestmentType]
,[EndDate]
,a.[CurrencyId]
,[AssetValue]
,c.FundId
FROM [StatusData_DMWkspaceDB].[dbo].[NetAssetsValidationFailure] a
LEFT JOIN MappingData_GAPortDB.dbo.PerformanceLevelMapping b
ON a.PerformanceId = b.PerformanceId
LEFT JOIN MappingData_GAPortDB.dbo.FundClassMatching c
ON b.SecId = c.SecId
WHERE a.EndDate IN (
SELECT MAX(a.EndDate)
From [StatusData_DMWkspaceDB].[dbo].[NetAssetsValidationFailure]
GROUP BY c.FundId, Month(a.EndDate), YEAR(a.EndDate))
This is your query:
SELECT TOP 10000 navf.[PerformanceId], [InvestmentType], [EndDate],
navf.[CurrencyId], [AssetValue], fcm.FundId
FROM [StatusData_DMWkspaceDB].[dbo].[NetAssetsValidationFailure] navf LEFT JOIN
MappingData_GAPortDB.dbo.PerformanceLevelMapping plm
ON navf.PerformanceId = plm.PerformanceId LEFT JOIN
MappingData_GAPortDB.dbo.FundClassMatching fcm
ON l.m.SecId = fcm.SecId
WHERE navf.EndDate IN (SELECT MAX(navf.EndDate)
From [StatusData_DMWkspaceDB].[dbo].[NetAssetsValidationFailure] navf
GROUP BY fcm.FundId, Month(navf.EndDate), YEAR(navf.EndDate)
);
Learn to use sensible table aliases, so the query is easier to write and to read.
In any case, your WHERE clause is only referencing outer tables in the GROUP BY. The message is quite clear.
I'm not even sure what you want to do, but I am guessing that this is a working version of what you want:
SELECT x.*
FROM (SELECT navf.[PerformanceId], [InvestmentType], [EndDate],
navf.[CurrencyId], [AssetValue], fcm.FundId,
ROW_NUMBER() OVER (PARTITION BY fcm.FundId, Month(navf.EndDate), YEAR(navf.EndDate)
ORDER BY navf.EndDate DESC
) as seqnum
FROM [StatusData_DMWkspaceDB].[dbo].[NetAssetsValidationFailure] navf LEFT JOIN
MappingData_GAPortDB.dbo.PerformanceLevelMapping plm
ON navf.PerformanceId = plm.PerformanceId LEFT JOIN
MappingData_GAPortDB.dbo.FundClassMatching fcm
ON l.m.SecId = fcm.SecId
) x
WHERE seqnum = 1;

Selecting Columns from another table inside a Select Query

It seems to be the query that I have created is a little bit messy.
Is there any other way to make it more readable?
Here's the query that I have created.
Select nullif((select count(*) from [casino_game].[dbo].[group_user] gu
where gu.group_id = (select pt.group_id from [casino_game].[dbo].[promo_trigger] pt
join [casino_game].[dbo].[promo_offer] po on po.trigger_id = pt.trigger_id
where pt.name = f.promo_name
group by pt.group_id)),0) as target_group, -- Number of customer uploaded to a free game program.
(cast(count(*) as decimal(11,2)) / nullif((select count(*) from [casino_game].[dbo].[group_user] gu
where gu.group_id = (select pt.group_id from [casino_game].[dbo].[promo_trigger] pt
join [casino_game].[dbo].[promo_offer] po on po.trigger_id = pt.trigger_id
where pt.name = f.promo_name
group by pt.group_id)), 0)) * 100 as claim_rate from [data].[dbo].[testgame]
This query is working, but I need some suggestion if there will be another way to make this working and more readable.
You can use OUTER APPLY to avoid using the same correlated sub-query twice
SELECT target_group,
( Cast(Count(*) AS DECIMAL(11, 2)) / NULLIF(target_group, 0) ) * 100 AS claim_rate
FROM [data].[dbo].[testgame]
OUTER apply (SELECT Count(*) AS target_group
FROM [casino_game].[dbo].[group_user] gu
WHERE gu.group_id = (SELECT pt.group_id
FROM
[casino_game].[dbo].[promo_trigger] pt
JOIN [casino_game].[dbo].[promo_offer]
po
ON po.trigger_id = pt.trigger_id
WHERE pt.NAME = f.promo_name
GROUP BY pt.group_id)) ou
I think the target table was group_user instead of testgame, and it must be a single row, no need for a join to many rows to testgame
select
COUNT(*) as target_group, -- Number of customer uploaded to a free game program.
-- It may cause the Divide by zero error if count(*) = 0
CASE WHEN COUNT(*) > 0 THEN (cast((SELECT COUNT(*) as value FROM [data].[dbo].[testgame]) as decimal(11,2)) / COUNT(*)) * 100 END as claim_rate
from
[casino_game].[dbo].[group_user] gu
inner join [casino_game].[dbo].[promo_trigger] pt on gu.group_id = pt.group_id
inner join [casino_game].[dbo].[promo_offer] po on po.trigger_id = pt.trigger_id

Column is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause

Ok here's my View (vw_LiftEquip)
SELECT dbo.tbl_equip_swl_unit.unit_id,
dbo.tbl_equip_swl_unit.unit_name,
dbo.tbl_equip_swl_unit.archived,
dbo.tbl_categories.category_id,
dbo.tbl_categories.categoryName,
dbo.tbl_categories.parentCategory,
dbo.tbl_categories.sub_category,
dbo.tbl_categories.desc_category,
dbo.tbl_categories.description,
dbo.tbl_categories.miscellaneous,
dbo.tbl_categories.category_archived,
dbo.tbl_equip_swl_unit.unit_name AS Expr1,
dbo.tbl_categories.categoryName AS Expr2,
dbo.tbl_categories.description AS Expr3,
dbo.tbl_equip_depts.dept_name,
dbo.tbl_equip_man.man_name,
dbo.tbl_Lifting_Gear.e_defects AS Expr7,
dbo.tbl_Lifting_Gear.e_defects_desc AS Expr8,
dbo.tbl_Lifting_Gear.e_defects_date AS Expr9,
dbo.tbl_equipment.equipment_id,
dbo.tbl_equipment.e_contract_no,
dbo.tbl_equipment.slID,
dbo.tbl_equipment.e_entered_by,
dbo.tbl_equipment.e_serial,
dbo.tbl_equipment.e_model,
dbo.tbl_equipment.e_description,
dbo.tbl_equipment.e_location_id,
dbo.tbl_equipment.e_owner_id,
dbo.tbl_equipment.e_department_id,
dbo.tbl_equipment.e_manafacture_id,
dbo.tbl_equipment.e_manDate1,
dbo.tbl_equipment.e_manDate2,
dbo.tbl_equipment.e_manDate3,
dbo.tbl_equipment.e_dimensions,
dbo.tbl_equipment.e_test_no,
dbo.tbl_equipment.e_firstDate1,
dbo.tbl_equipment.e_firstDate2,
dbo.tbl_equipment.e_firstDate3,
dbo.tbl_equipment.e_prevDate1,
dbo.tbl_equipment.e_prevDate2,
dbo.tbl_equipment.e_prevDate3,
dbo.tbl_equipment.e_insp_frequency,
dbo.tbl_equipment.e_swl,
dbo.tbl_equipment.e_swl_unit_id,
dbo.tbl_equipment.e_swl_notes,
dbo.tbl_equipment.e_cat_id,
dbo.tbl_equipment.e_sub_id,
dbo.tbl_equipment.e_parent_id,
dbo.tbl_equipment.e_last_inspector,
dbo.tbl_equipment.e_last_company,
dbo.tbl_equipment.e_deleted AS Expr11,
dbo.tbl_equipment.e_deleted_desc AS Expr12,
dbo.tbl_equipment.e_deleted_date AS Expr13,
dbo.tbl_equipment.e_deleted_insp AS Expr14,
dbo.tbl_Lifting_Gear.e_defects_action AS Expr15,
dbo.tbl_equipment.e_rig_location,
dbo.tbl_Lifting_Gear.e_add_type AS Expr17,
dbo.tbl_Lifting_Gear.con_id,
dbo.tbl_Lifting_Gear.lifting_date,
dbo.tbl_Lifting_Gear.lifting_ref_no,
dbo.tbl_Lifting_Gear.e_id,
dbo.tbl_Lifting_Gear.inspector_id,
dbo.tbl_Lifting_Gear.lift_testCert,
dbo.tbl_Lifting_Gear.lift_rig_location,
dbo.tbl_Lifting_Gear.inspected,
dbo.tbl_Lifting_Gear.lifting_through,
dbo.tbl_Lifting_Gear.liftingNDT,
dbo.tbl_Lifting_Gear.liftingTest,
dbo.tbl_Lifting_Gear.e_defects,
dbo.tbl_Lifting_Gear.e_defects_desc,
dbo.tbl_Lifting_Gear.e_defects_date,
dbo.tbl_Lifting_Gear.e_defects_action,
dbo.tbl_Lifting_Gear.lift_department_id,
dbo.tbl_Lifting_Gear.lifting_loc
FROM dbo.tbl_equipment
INNER JOIN dbo.tbl_equip_swl_unit
ON dbo.tbl_equipment.e_swl_unit_id = dbo.tbl_equip_swl_unit.unit_id
INNER JOIN dbo.tbl_categories
ON dbo.tbl_equipment.e_cat_id = dbo.tbl_categories.category_id
INNER JOIN dbo.tbl_equip_depts
ON dbo.tbl_equipment.e_department_id = dbo.tbl_equip_depts.dept_id
INNER JOIN dbo.tbl_equip_man
ON dbo.tbl_equipment.e_manafacture_id = dbo.tbl_equip_man.man_id
INNER JOIN dbo.vwSubCategory
ON dbo.tbl_equipment.e_sub_id = dbo.vwSubCategory.category_id
INNER JOIN dbo.vwDescCategory
ON dbo.tbl_equipment.e_cat_id = dbo.vwDescCategory.category_id
INNER JOIN dbo.tbl_Lifting_Gear
ON dbo.tbl_equipment.equipment_id = dbo.tbl_Lifting_Gear.e_id
And here's the select statement with subquery that I am using:
SELECT *
FROM vw_LiftEquip
WHERE lifting_loc = ? AND
con_id = ? AND
EXPR11 =
'N'(
SELECT MAX(lifting_date) AS maxLift
FROM vw_LiftEquip
WHERE e_id = equipment_id
)
ORDER BY lifting_ref_no,
category_id,
e_swl,
e_serial
I get the error :
Column "vw_LiftEquip.category_id" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
Can't see why its returning that error, this is admittedly the first time I've ran a subquery on such a complex view, and I am a bit lost, thanks in advance for any help. I have looked through the similar posts and can find no answers to this one, sorry if I am just being dumb.
You are missing AND between EXPR11 = 'N' and (SELECT MAX(...
Otherwise, it looks OK. MAX without GROUP BY is allowed if you have no other columns in the SELECT
Update: #hvd also noted that you have nothing to compare to MAX(lifting_date). See comment
Update 2,
SELECT *
FROM vw_LiftEquip v1
CROSS JOIN
(
SELECT MAX(lifting_date) AS maxLift
FROM vw_LiftEquip
WHERE e_id = equipment_id
) v2
WHERE v1.lifting_loc = ? AND
v1.con_id = ? AND
v1.EXPR11 = 'N'
ORDER BY v1.lifting_ref_no,
v1.category_id,
v1.e_swl,
v1.e_serial