I have a left join like this:
LEFT MERGE JOIN --IVA0A
( SELECT VOUCHER,DATAAREAID,ISNULL(VENDTRANSID,0)AS LJT, SUM(IIF(TAXITEMGROUP = 'ANTICIPOS',-1*TAXBASEAMOUNT,TAXBASEAMOUNT))AS TAXBASEAMOUNT,
SUM(IIF(TAXITEMGROUP = 'ANTICIPOS',-1*TAXAMOUNT,TAXAMOUNT)) AS TAXAMOUNT FROM
##CPP TT
WHERE (
(TT.TAXCODE LIKE 'IVA0A' OR TT.TAXCODE LIKE 'IVA0AFA' OR TT.TAXCODE LIKE 'IVA0AEXP') OR
(TT.TAXITEMGROUP = 'ANTICIPOS' AND (TT.TAXCODE LIKE 'IVA0AP' OR TT.TAXCODE LIKE 'IVA0AFAP' OR TT.TAXCODE LIKE 'IVA0AEXPP'))
)
GROUP BY TT.VOUCHER,TT.DATAAREAID,ISNULL(LJT.VENDTRANSID,0))
AS TTIVA0A ON VT.VOUCHER = TTIVA0A.VOUCHER AND TTIVA0A.DATAAREAID = VT.DATAAREAID
and IIF(TTIVA0A.LJT=0,VT.RECID,TTIVA0A.LJT) = VT.RECID
Problem is when I trying to use GROUP BY clause
SQL returns:
The multi-part identifier "LJT.VENDTRANSID" could not be bound.
I can't understand why it happen, can anyone explain me please? Regards
I suppose that you want to group by ISNULL(VENDTRANSID,0) AS LJT, since you can't use the alias LJT in the WHERE clause you should group using the expression itself:
GROUP BY TT.VOUCHER, TT.DATAAREAID, ISNULL(VENDTRANSID,0)
LJT is the alias given to column ISNULL(VENTTRANSID, 0).
I think that, in GROUP BY clause instead of ISNULL(LJT.VENTTRANSID, 0) if you just give ISNULL(VENTTRANSID, 0) it will solve the issue.
Related
Here is my query and I want to add the "count of SalID group by OFID" and store the result in the same table.
SELECT
T_OF.OFID,
T_OF.OFDateDPrev, T_OF.OFDateFPrev,
T_OF_User.OFUserID,
T_OF_User.SalID
INTO T_tracing
FROM T_OF
INNER JOIN T_OF_User
ON T_OF_User.OFID = T_OF.OFID
I tried this:
SELECT
T_OF.OFID,
T_OF.OFDateDPrev, T_OF.OFDateFPrev,
T_OF_User.OFUserID,
Count (SalID) FROM T_OF_User GROUP BY OFID
INTO T_tracing
FROM T_OF
INNER JOIN T_OF_User
ON T_OF_User.OFID = T_OF.OFID
But I have an error message. Any help please?
I think you want a window function:
SELECT T_OF.OFID, T_OF.OFDateDPrev, T_OF.OFDateFPrev, T_OF_User.OFUserID,
Count(SalID) OVER (PARTITION BY T_OF.OFID) as cnt
INTO T_tracing
FROM T_OF JOIN
T_OF_User
ON T_OF_User.OFID = T_OF.OFID;
You also need to give the result of the expression a name for T_Tracing.
This clause keeps showing "ERROR: missing FROM-clause entry for table "subid" in postgresql 10.
How so?
UPDATE "io_S1"."tc_history"
SET "c_TIME" = TT."c_TIME",
"b_TIME_c"=TT."b_TIME_c",
"b_TIME_COLLECTION"=TT."b_TIME_COLLECTION",
"c_NOTE"=TT."c_NOTE",
"b_TIME_TAXI"=TT."b_TIME_TAXI",
"b_LOCATION_TAXI"=TT."b_LOCATION_TAXI",
"ESTABLISHED_TIME"=TT."c_TIME"
FROM (
SELECT "c_TIME","b_TIME_c","b_TIME_COLLECTION","c_NOTE","b_TIME_TAXI","b_LOCATION_TAXI","c_TIME"
FROM "io_TRACE"."PERSONAL_Tc_RECORD" TR
WHERE TR."PERSONAL_SERIAL_ID" IN (SELECT "SUBJECT_ID"
FROM "io_COLLECTION"."COLLECTION_CONSENT_RECORD" AS SUBID
WHERE SUBID."SUBJECT_CITIZEN_ID" IN (SELECT "SUBJECT_CITIZEN_ID"
FROM "io_S1"."tc_history" AS TC))
) AS TT
WHERE "SUBJECT_CITIZEN_ID"=SUBID."SUBJECT_CITIZEN_ID";
You can try below
UPDATE "io_S1"."tc_history"
SET "c_TIME" = TT."c_TIME",
"b_TIME_c"=TT."b_TIME_c",
"b_TIME_COLLECTION"=TT."b_TIME_COLLECTION",
"c_NOTE"=TT."c_NOTE",
"b_TIME_TAXI"=TT."b_TIME_TAXI",
"b_LOCATION_TAXI"=TT."b_LOCATION_TAXI",
"ESTABLISHED_TIME"=TT."c_TIME"
FROM (
SELECT "c_TIME","b_TIME_c","b_TIME_COLLECTION","c_NOTE","b_TIME_TAXI","b_LOCATION_TAXI","c_TIME",
SUBID."SUBJECT_CITIZEN_ID"
FROM "io_TRACE"."PERSONAL_Tc_RECORD" TR inner join
"io_COLLECTION"."COLLECTION_CONSENT_RECORD" AS SUBID
on TR."PERSONAL_SERIAL_ID"=SUBID."SUBJECT_ID"
inner join "io_S1"."tc_history" AS TC
on TC."SUBJECT_CITIZEN_ID"=SUBID."SUBJECT_CITIZEN_ID"
) AS TT
WHERE "SUBJECT_CITIZEN_ID"=TT."SUBJECT_CITIZEN_ID";
Alias used 'SUBID' is out of scope, therefore, the from clause doesn't recognize it. Best to use the normal convention as 'TableName.ColumnName', since two tables have the same column name.
I have this query:
left JOIN
(SELECT * FROM ##TTA TT WHERE (TT.TAXCODE LIKE 'IVA0A' OR TT.TAXCODE LIKE 'IVA0EXPT')) TT
GROUP BY TT.DATAAREAID, tt.VOUCHER, TT.MAINACCOUNTID
AS TTIVA0A ON Asiento.Asiento = TTIVA0A.VOUCHER
AND Asiento.Cuenta = TTIVA0A.AD
AND Asiento.Empresa = TTIVA0A.DATAAREAID
Problem is into GROUP BY clause I get:
Incorrect syntax near the keyword 'GROUP'.
Can someone explain me why I cant use GROUP BY after WHERE clause?
If you were more careful with your formatting this would be painfully obvious.
Here is your code with the group by in the right place.
left JOIN
(
SELECT *
FROM ##TTA TT
WHERE TT.TAXCODE LIKE 'IVA0A'
OR TT.TAXCODE LIKE 'IVA0EXPT'
GROUP BY TT.DATAAREAID
, TT.VOUCHER
, TT.MAINACCOUNTID
) AS TTIVA0A ON Asiento.Asiento = TTIVA0A.VOUCHER
AND Asiento.Cuenta = TTIVA0A.AD
AND Asiento.Empresa = TTIVA0A.DATAAREAID
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
Here is my SELECT query:
SELECT
a.id_auto,
SUM(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km) celkova_trzba
FROM Auta a
INNER JOIN (SELECT
id_auto,
(SUM(koniec_pozicania - zaciatok_pozicania)) pozicane_dni,
(SUM(najazdene_km)) najazdene_km,
zaloha
FROM Zakaznik GROUP BY id_auto) z
ON z.id_auto = a.id_auto
INNER JOIN (SELECT
id_auto,
poplatok_denny,
poplatok_km
FROM Auta_zaloha) az
ON az.id_auto = a.id_auto
GROUP BY a.id_auto;
But I'm getting this error:
ORA-00979: not a GROUP BY expression
Anybody knows where could be the problem? I'm a little bit confused. I have GROUP BY clause everywhere where I use aggregate function SUM().
EDIT:
One more thing, it stop working when I add a CASE WHEN to the query:
SELECT
a.id_auto, a.poplatok_denny, a.poplatok_km,
CASE WHEN z.zaloha IS NULL THEN
(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km)
ELSE
(pozicane_dni * az.poplatok_denny + najazdene_km * az.poplatok_km)
END
celkova_trzba
FROM Auta a
INNER JOIN (SELECT
id_auto,
(SUM(koniec_pozicania - zaciatok_pozicania)) pozicane_dni,
(SUM(najazdene_km)) najazdene_km,
zaloha
FROM Zakaznik GROUP BY id_auto, zaloha) z
ON z.id_auto = a.id_auto
INNER JOIN (SELECT
id_auto,
poplatok_denny,
poplatok_km
FROM Auta_zaloha) az
ON az.id_auto = a.id_auto
GROUP BY a.id_auto;
In your first inner SELECT, you should either remove "zaloha", GROUP BY it or apply some aggregate function to it.
The problem will be in using the value of
a.poplatok_km
If you're grouping by a.id_auto, you need to give a rule on what to do if there are multiple rows in the result set (even if there is only one possible row, that is something the SQL doesn't "know").
Two ways around this:
add all columns from table a that are required in the calculation in the group by clause
use a "pseudo-Function" like min (a.poplatok_km) which doesn't change anything
Your second inner select:
INNER JOIN (SELECT
id_auto,
poplatok_denny,
poplatok_km
FROM Auta_zaloha) az
Appears to be missing a group by clause like:
INNER JOIN (SELECT
id_auto,
poplatok_denny,
poplatok_km
FROM Auta_zaloha group by id_auto) az
Try using NVL2 function in place of CASE WHEN, at least it could yield more understandable error message:
NVL2(z.zaloha,
(pozicane_dni * az.poplatok_denny + najazdene_km * az.poplatok_km),
(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km)
) celkova_trzba