ORA-00936:missing expression -- oracle - sql

Although I find some related question in Stackoverflow:
ORA-00936: missing expression oracle
ORA-00936: missing expression Oracle Apex
But those is not fit my question:
(SELECT t_1.oi1name OI1NAME
FROM ( select oi1.name oi1name, oi.name oname, b.prodesc, b.foundtime, b.occurrencetime, b.divisionproject, b.pilenumber, b.constructionteam, b.progress, h.pk_group
from zspm_qa_monthlyreport_b b
left outer join zspm_qa_monthlyreport_h h on b.pk_monthlyreport_h = h.pk_monthlyreport_h
left outer join org_itemorg oi on oi.pk_itemorg = h.pk_org
left outer join org_itemorg oi1 on oi1.pk_itemorg = oi.pk_fatherorg
where h.dr = 0 and h.billstatus = 1 and b.dr = 0
and oi.code like CONCAT ( ( select code from org_itemorg where pk_itemorg in () ), '%' ) and h.def1 = '2016-01' ) t_1
WHERE t_1.pk_group = '0001A2100000000007QL')
This is my sql code for query, but I don't know where is the issue.

The in list is empty. I don't think that is allowed.
More importantly, you probably intend this logic:
where . . . and
exists (select 1
from org_itemorg oio
where pk_itemorg in (. . .) and
oi.code like oio.code || '%'
) and
h.def1 = '2016-01'
Your subquery can return more than one row, which would be a problem when you run the query.

Related

Query fail Oracle but working in SQL Server

I have a query that runs perfectly n SQL server but in Oracle, I get the following error:
ORA-00923: FROM keyword not found where expected
SELECT
MyApps.AppConfigurationId,
MyApps.GUID,
MyApps.AppName,
MyApps.BinaryData,
MyApps.Color1,
MyApps.UserCreatorName,
MyApps.CreatedAt,
MyApps.UserUpdaterName,
MyApps.UpdatedAt,
MyApps.UserPublisherName,
MyApps.LastPublishedAt,
MyApps.AppConfigStateId,
MyApps.AppConfigStateLabel
FROM
(
SELECT
ROW_NUMBER() OVER (
PARTITION BY "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID"
ORDER BY LastestPublishInfo."PUBLISHEDAT" DESC
) AS RowNum,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" AS AppConfigurationId,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."GUID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."NAME" AS AppName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."BINARYDATA",
"OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."COLOR1",
UserCreator."NAME" AS UserCreatorName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDAT",
UserUpdater."NAME" AS UserUpdaterName,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDAT",
LastestPublishInfo.PublisherName AS UserPublisherName ,
LastestPublishInfo."PUBLISHEDAT" AS LastPublishedAt,
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."LABEL" AS AppConfigStateLabel
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266" JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."APPCONFIGSTATEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON20"."ID" AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."PARENTAPPCONFIGID" IS NULL AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ISACTIVE" = 1
)
JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserCreator
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."CREATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserUpdater
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."UPDATEDBY" = UserCreator."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."COLORPALETTEID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_COLORPA1"."ID")
LEFT JOIN
(
SELECT "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."APPCONFIGURATIONID",
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT",
UserPublisher."NAME" AS PublisherName
FROM "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16" LEFT JOIN "OSADMIN_OSDEV1"."OSSYS_USER_T266" UserPublisher
ON ( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDBY" = UserPublisher."ID")
WHERE "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."PUBLISHEDAT" IS NOT NULL
AND
"OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON16"."ISACTIVE" = 0
) LastestPublishInfo
ON (LastestPublishInfo.AppConfigurationId = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID")
LEFT JOIN "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"
ON( "OSADMIN_OSDEV1"."OSUSR_4BQ_APPICON1"."APPCONFIGURATIONID" = "OSADMIN_OSDEV1"."OSUSR_4BQ_APPCON15_T266"."ID" )
) MyApps
WHERE MyApps.RowNum = 1
ORDER BY
MyApps.UpdatedAt DESC
I already tried to check syntax follow some articles but I'm not able to solve it.
Added " around de the aliases, tried to replace attribute names in the subquery not to match the parent but nothing worked.
Hope you could help me with this since I went to the point I have no clue about what to try next!
Cheers
Using the following web: https://rextester.com/l/oracle_online_compiler
I tryed your SQL and got the same error as you.
I then started simplifing it up to:
SELECT 1 AS RowNum
FROM blah
and still getting the same error.
I think RowNum is a reserved word in Oracle, you can not use it as a field alias.

I am converting Oracle queries to Standard Bigquery, i am gettting error "IN subquery is not supported inside join predicate."

I have converted oracle query into below standard bq but in last statement(IN subquery). I am getting error:
"IN subquery is not supported inside join predicate."
Please advise how to use IN subquery in bq in the below code
#Last part of the code
INNER JOIN (
SELECT
DISTINCT `domain-rr.oracle_DB_DB.he_project_assoc`.PARENT_ISBN
PARENT_ISBN,
SUM (`domain-rr.DB_RPT.PROJECT_GR_QTY`.GR_QTY) GR_QTY
FROM
`domain-rr.oracle_DB_DB.he_project_assoc`
INNER JOIN
`domain-rr.DB_RPT.PROJECT_GR_QTY`
ON
`domain-rr.oracle_DB_DB.he_project_assoc`.child_ISBN = `domain-
rr.DB_RPT.PROJECT_GR_QTY`.BIC_GCISBN
AND `domain-rr.oracle_DB_DB.he_project_assoc`.BREAK_LABEL <>
'Associated ISBNs'
GROUP BY
`domain-rr.oracle_DB_DB.he_project_assoc`.PARENT_ISBN) xx
ON
yy.PARENT_ISBN = xx.PARENT_ISBN
AND yy.CIRCULATION_INT < xx.GR_QTY
AND yy.PARENT_ISBN IN
( SELECT
DISTINCT _BIC_GCISBN
FROM
`domain-rr.DB_RPT.BIC_GM_AGCPOAODS00_BO_VW`
INNER JOIN
`domain-rr.oracle_DB_boadmin.fiscal_bo`
ON
_BIC_ZC2GRIRIN = 'G'
AND _BIC_ZCLOEKZ = ' '
AND SUBSTR (BOUND_DATE, 1, 6) = `domain-
rr.oracle_DB_boadmin.fiscal_bo`.PRIOR_FISC_YEAR_MONTH )
Can you try like this:
Select * from (
#Last part of the code
INNER JOIN (
SELECT
DISTINCT `pearson-rr.oracle_grdw_grdw.he_project_assoc`.PARENT_ISBN
PARENT_ISBN,
SUM (`pearson-rr.GRDW_RPT.PROJECT_GR_QTY`.GR_QTY) GR_QTY
FROM
`pearson-rr.oracle_grdw_grdw.he_project_assoc`
INNER JOIN
`pearson-rr.GRDW_RPT.PROJECT_GR_QTY`
ON
`pearson-rr.oracle_grdw_grdw.he_project_assoc`.child_ISBN = `pearson-
rr.GRDW_RPT.PROJECT_GR_QTY`.BIC_GCISBN
AND `pearson-rr.oracle_grdw_grdw.he_project_assoc`.BREAK_LABEL <>
'Associated ISBNs'
GROUP BY
`pearson-rr.oracle_grdw_grdw.he_project_assoc`.PARENT_ISBN) xx
ON
yy.PARENT_ISBN = xx.PARENT_ISBN
AND yy.CIRCULATION_INT < xx.GR_QTY
) AA
where AA.PARENT_ISBN IN
( SELECT
DISTINCT _BIC_GCISBN
FROM
`pearson-rr.GRDW_RPT.BIC_GM_AGCPOAODS00_BO_VW`
INNER JOIN
`pearson-rr.oracle_grdw_boadmin.fiscal_bo`
ON
_BIC_ZC2GRIRIN = 'G'
AND _BIC_ZCLOEKZ = ' '
AND SUBSTR (BOUND_DATE, 1, 6) = `pearson-
rr.oracle_grdw_boadmin.fiscal_bo`.PRIOR_FISC_YEAR_MONTH )

SQL-query error in pyspark while using temp-table

I have a SQL query which I have to access in PySpark(DataBricks). due to complex query, PySpark is not able to read the same. can someone check my query and assist me to get this query written in a single 'SELECT' statement not using 'WITH' statement.
Stage:- 1
promotions="""
(WITH VCTE_Promotions as (SELECT v.Shortname, v.Employee_ID_ALT, v.Job_Level,
v.Management_Level, CAST(sysdatetime() AS date) AS PIT_Date, v.Employee_Status_Alt as Employee_Status,
v.Work_Location_Region, v.Work_Location_Country_Desc, v.HML,
[DM_GlobalStaff].[dbo].[V_Worker_PIT].Is_Manager
FROM [DM_GlobalStaff].[dbo].[V_Worker_CUR] as v
LEFT OUTER JOIN
[DM_GlobalStaff].[dbo].[V_Worker_PIT] ON v.Management_Level = [DM_GlobalStaff].[dbo].[V_Worker_PIT].Management_Level),
VCTE_Promotion_v2_Eval as (
SELECT Employee_ID_ALT,
( SELECT max([pit_date]) AS prior_data
FROM [DM_GlobalStaff].[dbo].[V_Worker_PIT] AS t
WHERE (employee_id_alt = a.Employee_ID_ALT) AND (PIT_Date < a.PIT_Date) AND (Is_Manager <> a.Is_Manager) OR
(employee_id_alt = a.Employee_ID_ALT) AND (PIT_Date < a.PIT_Date) AND (Job_Level <> a.Job_Level)) AS prev_job_change_date, Is_Manager
FROM VCTE_Promotions AS a)
SELECT VCTE_Promotion_v2_Eval.Employee_ID_ALT, COALESCE (v_cur.Employee_Status_ALT, N'') AS Curr_Emp_Status,
COALESCE (v_cur.Employee_Type, N'') AS Curr_Employee_Type, v_cur.Hire_Date_Alt AS Curr_Hire_Date,
v_cur.Termination_Date_ALT AS Curr_Termination_Date, COALESCE (v_cur.Termination_Action_ALT, N'')
AS Curr_Termination_Action, cast (v_cur.Job_Level as int) AS Curr_Job_Level,
COALESCE (v_cur.Management_Level, N'') AS Curr_Management_Level,
COALESCE (VCTE_Promotion_v2_Eval.Is_Manager, N'') AS Curr_Ismanager,
CASE WHEN v_m.Job_Level < v_cur.Job_Level OR
(VCTE_Promotion_v2_Eval.Is_Manager = 1 AND v_m.Is_Manager = 0 AND v_m.Job_Level <= v_cur.Job_Level)
THEN 'Promotion' WHEN v_m.Job_Level <> v_cur.Job_Level OR
VCTE_Promotion_v2_Eval.Is_Manager <> v_m.Is_Manager THEN 'Other' ELSE '' END AS Promotion, v_cur.Tenure,
v_cur.Review_Rating_Current
FROM VCTE_Promotion_v2_Eval INNER JOIN
[DM_GlobalStaff].[dbo].[V_Worker_CUR] as v_cur ON VCTE_Promotion_v2_Eval.Employee_ID_ALT = v_cur.Employee_ID_ALT LEFT OUTER JOIN
[DM_GlobalStaff].[dbo].[V_Worker_PIT] as v_m ON VCTE_Promotion_v2_Eval.prev_job_change_date = v_m.PIT_Date AND
VCTE_Promotion_v2_Eval.Employee_ID_ALT = v_m.employee_id_alt
) as pr """
stage-2
promotions = spark.read.jdbc(url=jdbcUrl, table=promotions, properties=connectionProperties)
stage-3
promotions.count()
promotions.show()
Getting below error from Stage-2 query:-
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword &apos;WITH&apos;.
---------------------------------------------------------------------------
Py4JJavaError Traceback (most recent call last)
<command-2532359884208251> in <module>()
----> 1 promotions = spark.read.jdbc(url=jdbcUrl, table=promotions, properties=connectionProperties)
/databricks/spark/python/pyspark/sql/readwriter.py in jdbc(self, url, table, column, lowerBound, upperBound, numPartitions, predicates, properties)
533 jpredicates = utils.toJArray(gateway, gateway.jvm.java.lang.String, predicates)
534 return self._df(self._jreader.jdbc(url, table, jpredicates, jprop))
--> 535 return self._df(self._jreader.jdbc(url, table, jprop))
536
537
I dont have issue with my query, this is working perfectly fine with my SQL prompt. But as soon as I am using same query in PYSPARK(DataBricks) I am getting syntax error. Will you kindly help me with PySpark syntax as well.
your Prompt assistance will be highly appreciated.
I have no way of testing, but please try it, and compare the result to see if everything is matching.
Also, I am using cross appy instead of correlated subquery because there is no simple join and correlated subquery isn't efficient,
So Cross apply should do the job
(
SELECT
VCTE_Promotion_v2_Eval.Employee_ID_ALT
,COALESCE(v_cur.Employee_Type, N'') AS Curr_Employee_Type
,v_cur.Review_Rating_Current
(
SELECT
Employee_ID_ALT,
pr.prev_job_change_date,
IsManager
From
( SELECT
v.Shortname
,v.Employee_ID_ALT
,v.Job_Level
,v.Management_Level
,CAST(SYSDATETIME() AS DATE) AS PIT_Date
,v.Employee_Status_Alt AS Employee_Status
,v.Work_Location_Region
,v.Work_Location_Country_Desc
,v.HML
,dbo.T_Mngmt_Level_IsManager_Mapping.IsManager
FROM Worker_CUR AS v
LEFT OUTER JOIN dbo.T_Mngmt_Level_IsManager_Mapping
ON v.Management_Level = dbo.T_Mngmt_Level_IsManager_Mapping.Management_Level
) as VCTE_Promotions a
Cross APPLY (
SELECT
MAX(PIT_Date) AS prior_data
FROM dbo.V_Worker_PIT_with_IsManager AS t
WHERE (employee_id_alt = a.Employee_ID_ALT)
AND (PIT_Date < a.PIT_Date)
AND (IsManager <> a.IsManager)
OR (employee_id_alt = a.Employee_ID_ALT)
AND (PIT_Date < a.PIT_Date)
AND (Job_Level <> a.Job_Level)
)
AS pr
) as VCTE_Promotion_v2_Eval
INNER JOIN [DM_GlobalStaff].[dbo].[V_Worker_CUR] AS v_cur
ON VCTE_Promotion_v2_Eval.Employee_ID_ALT = v_cur.Employee_ID_ALT
LEFT OUTER JOIN dbo.V_Worker_PIT_with_IsManager AS v_m
ON VCTE_Promotion_v2_Eval.prev_job_change_date = v_m.PIT_Date
AND VCTE_Promotion_v2_Eval.Employee_ID_ALT = v_m.employee_id_alt ) as promotions

VS2012 Table Adapter Syntax Error: Expecting identifier or quoted identifier

I am trying to add a table adapter to one of my datasets. The dataset has oracle as its datasource. When I put in the query (which works elsewhere) I get this error. The query has left outer joins in it. If i go to query builder, it then adds stuff like { oj to the query. I knew there was an issue with previous visual studio versions, but I thought they were fixed. How can I get this to work.
SELECT PERS.PERS_ID,
PERS.PERS_LAST_NM,
PERS.PERS_FIRST_NM,
PERS.PERS_MIDDLE_INIT,
PERS.PERS_MIDDLE_INIT,
PERS.PERS_PREFERRED_NM,
PERS.PERS_EMPLOYEE_NO,
PERS.PERS_EMPLOYEE_TYP,
PERS.PERS_STAT,
PERS.PERS_LENT_CD AS PERS_ENTITY,
PERS.PERS_TYP,
PERS.PERS_MSTP_ID,
PERS.PERS_BLDG_ID,
PERS.PERS_TITLE_INIT,
PERS.PERS_PERS_ID,
PERS.PERS_COCC_ID,
PERS.PERS_IORG_CD,
PERS.PERS_CORPORATE_ID,
CONCAT('(', CONCAT(PERS.PERS_PHONE_AREA_CD, CONCAT(')',CONCAT(PERS.PERS_PHONE_EXCH_NO,CONCAT('-',PERS.PERS_PHONE_EXTN_NO))))) AS PERS_PHONE_NO,
UPPER(EMAIL.ELID_LONG_USERID) AS EMPL_EMAIL,
UPPER(USERID.ELID_USERID) AS EMPL_USERID
FROM CDAS.TDWHPERS PERS
LEFT OUTER JOIN (SELECT ELID_PERS_ID,
ELID_LONG_USERID
FROM CDAS.TDWHELID
WHERE ( ELID_EICT_CD = '0010' )
AND ( ELID_OPEN_IND = 'Y' )) EMAIL
ON PERS.PERS_ID = EMAIL.ELID_PERS_ID
LEFT OUTER JOIN (SELECT ELID_PERS_ID,
ELID_USERID
FROM CDAS.TDWHELID TDWHELID_1
WHERE ( ELID_EICT_CD = '9100' )
AND ( ELID_OPEN_IND = 'Y' )) USERID
ON PERS.PERS_ID = USERID.ELID_PERS_ID
WHERE ( PERS.PERS_STAT <> 'INACTIVE' )
AND ( PERS.PERS_STAT <> 'UNKNOWN' )
ORDER BY PERS.PERS_LAST_NM

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