select from subquery select where condition throws error - sql

I have the below query where I am selecting from another subquery with where condition
if not exists
(select #DocTaxID from (select bundlechecklist.Bundle_TXN_ID, documentchecklist.Doc_Checklist_TXN_ID, documentchecklist.Doc_Type_Name from ecm.bundle_checklist_txn bundlechecklist
left outer join [ECM].[Document_Checklist_TXN] documentchecklist on documentchecklist.Bundle_TXN_ID = bundlechecklist.Bundle_TXN_ID
where bundlechecklist.originating_tran_id = "AMD"
and bundle_name = 'Line')
where #DocTaxID is not null )
begin
insert into [ECM].[Document_Checklist_TXN]
(bundle_txn_id, document_action_rule_id, action_rule_name, doc_type_name, cp_document_name, document_id, doc_status_code)
values
(1, 17, 'A', 'DEv', 'Dec', 12, 'Pend')
end
DocTaxID is integer field.
And the error I am getting is "Incorrect syntax near the keyword 'where'."

You need a table alias:
select #DocTaxID
from (select bundlechecklist.Bundle_TXN_ID, documentchecklist.Doc_Checklist_TXN_ID, documentchecklist.Doc_Type_Name
from ecm.bundle_checklist_txn bundlechecklist left outer join
[ECM].[Document_Checklist_TXN] documentchecklist
on documentchecklist.Bundle_TXN_ID = bundlechecklist.Bundle_TXN_ID
where bundlechecklist.originating_tran_id = 'AMD' and bundle_name = 'Line'
) d
-------^
where #DocTaxID is not null ;
Somehow, I think there is a much simpler way to express this logic, but off-hand, it is not clear exactly what it is doing.

Related

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 )

PostgreSQL syntax error at or near "."

I try create postgresql query, for get the same data from different tables, but when execute I have error. In mysql I did something like that query
> ERROR: syntax error at or near "."
LINE 4: `B`.`_id` AS `_id`
^
> Time: 0.02s
my query
SELECT *
FROM
(
SELECT
`B`.`_id` AS `_id`
FROM (
SELECT
`apdi`.`id` AS `_id`
FROM `application_person_document_image` AS `apdi`
INNER JOIN `application_person_document` AS `apd` ON `apdi`.`id` = `apd`.`application_person_document_id`
INNER JOIN `application_person` AS `ap` ON `ap`.`id` = `apd`.`application_person_id`
INNER JOIN `application` AS `a` ON `a`.`id` = `ap`.`application_id`
WHERE
`a`.`id` = 196733
) AS `B`
GROUP BY `B`.`_id`
UNION
SELECT
`N`.`_id` AS `_id`
FROM (
SELECT
`app`.`id` AS `_id`,
FROM `application_person_photo` AS `app`
INNER JOIN `application_person` AS `ap` ON `ap`.`id` = `app`.`application_person_id`
INNER JOIN `application` AS `a` ON `a`.`id` = `ap`.`application_id`
WHERE
`a`.`id` = 196733
) AS `N`
GROUP BY `N`.`_id`
) AS t
What's wrong ? I looked documentation, using aliases exist.

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;

SQL JOIN with a condition

I am using SQL SERVER 2008 R2.
My query join is like this:
SELECT dbo.TARIF_COURANT.CODE_TARIF,
dbo.TARIF_COURANT.NO_CLIENT,dbo.TARIF_COURANT.ZONE, dbo.TARIF_COURANT.UNITE,
dbo.TARIF_COURANT.LIBELLE, dbo.TARIF_COURANT.TR_DEB, dbo.TARIF_COURANT.TR_FIN,
dbo.TARIF_COURANT.MONTANT,dbo.T_TARIF_ZONE.LIBELLE AS ZONELIB,
dbo.T_TARIF_ZONE.DEPARTEMENT, dbo.T_TARIF_ZONE.DELAI
FROM dbo.TARIF_COURANT LEFT OUTER JOIN dbo.T_TARIF_ZONE ON
dbo.TARIF_COURANT.ZONE = dbo.T_TARIF_ZONE.NO_ID
WHERE (dbo.TARIF_COURANT.TEMPO = 2) AND
(dbo.TARIF_COURANT.ZONE IN (1, 2, 3, 4, 6))
ORDER BY dbo.TARIF_COURANT.TR_DEB
Now i add another table LS_CLIENT_DEPT, and i modify my join:
SELECT dbo.TARIF_COURANT.CODE_TARIF, dbo.TARIF_COURANT.NO_CLIENT,
dbo.TARIF_COURANT.ZONE, dbo.TARIF_COURANT.UNITE, dbo.TARIF_COURANT.LIBELLE,
dbo.TARIF_COURANT.TR_DEB, dbo.TARIF_COURANT.TR_FIN, dbo.TARIF_COURANT.MONTANT,
dbo.T_TARIF_ZONE.LIBELLE AS ZONELIB, dbo.LS_CLIENT_DEPT.DEPARTEMENT,
dbo.T_TARIF_ZONE.DELAI
FROM dbo.TARIF_COURANT LEFT OUTER JOIN dbo.T_TARIF_ZONE ON
dbo.TARIF_COURANT.ZONE = dbo.T_TARIF_ZONE.NO_ID
LEFT OUTER JOIN dbo.LS_CLIENT_DEPT ON dbo.TARIF_COURANT.NO_CLIENT =
dbo.LS_CLIENT_DEPT.CODE_CLIENT AND dbo.TARIF_COURANT.ZONE =
dbo.LS_CLIENT_DEPT.ZONE
WHERE (dbo.TARIF_COURANT.TEMPO = 2) AND (dbo.TARIF_COURANT.ZONE IN (1, 2, 3,
4, 6)) ORDER BY dbo.TARIF_COURANT.TR_DEB
The object is if the column DEPARTEMENT in table 'LS_CLIENT_DEPT' is 'NOT NULL' then use this column value else use the column DEPARTEMENT value from table 'T_TARIF_ZONE'
Thanks you in advance
Sounds like you want:
COALESCE(LS_CLIENT_DEPT.DEPARTEMENT, T_TARIF_ZONE.DEPARTEMENT)
So it will take Department from LS_Client_Dept if it isn't null, and if it is null, return Department from T_TARIF_ZONE instead (this could be null too, however).
MSDN Documentation on Coalesce.

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