Can´t use GROUP BY after WHERE clause into subquery - sql

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

Related

Why can't I access a field defined as "Select 1" from a subquery in the outer query?

I have this subquery:
LEFT JOIN (SELECT 1 as exist
, MAX (ev.EventDate) as eventdate
, evt.EventCode
, CCaseID
FROM stg.Event ev
JOIN stg.EventTemplate evt
ON ev.EventTemplateID = evt.ID
WHERE evt.EventCode = 'UN002'
Group by CCaseID, evt.EventCode) as un002
ON un002.CCaseID = ev.CCaseID
WHERE evt.EventCode = 'UN001'
AND (un002.eventdate < ev.eventdate OR un002.eventdate IS NULL)
Group by ev.CCaseID, evt.EventCode) as un001
ON cc.ID = un001.CCaseID
I am now trying to access the exist field in the outer query as per un001.exist but SQL Server tells me that it is an invalid field. What am I missing?
un001 doesnt have exist that field belong to un002 subquery.
Also you have a GROUP BY and the and ON so there is some missing code there.
You should simplify the code and use CTE to make it easy to read and debug.
Something like this :
WITH un001 as ( SELECT ... ),
un002 as ( SELECT ...)
SELECT *
FROM un001
JOIN un002
ON un001 .CCaseID = un002.CCaseID

SQL multipart identifier into GrOUP BY clause

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.

Alias to a join query

( SELECT Vraboteni.v, Ulogi.p, Zarabotuva.honorar
FROM Vraboteni, Ulogi, Zarabotuva
WHERE Vraboteni.v = Ulogi.v
AND ima_uloga='sporedna'
AND Ulogi.p = Zarabotuva.p
) as F
JOIN
( SELECT Vraboteni.v, Ulogi.p, Zarabotuva.honorar
FROM Vraboteni, Ulogi, Zarabotuva
WHERE Vraboteni.v = Ulogi.v
AND ima_uloga='glavna'
AND Ulogi.p = Zarabotuva.p
) as S
ON (F.honorar > S.honorar)
Can anyone tell me what is wrong with the syntax that I am using above? I'm having the same issue over multiple queries and I'm not sure I quite understand how I am supposed to assign an alias when I use a join (having the same issue when trying to assign alliases to multiple nested joins)
The subselects you join should be considered as a normal table or view, so imagine they are, and your select statement looks like this:
SELECT1 as F
JOIN SELECT2 as S ON (F.honorar > S.honorar)
This statement is missing essential parts, like a SELECT and FROM clause.
So fix it, if you want to join two selects, you should encapsulate them in another select, so you get:
SELECT
S.*,
F.*
FROM
(SELECT ... ) AS F
JOIN (SELECT ...) AS S ON (F.honorar > S.honorar)
Alternatively, you could get rid of the two subselects, use normal joins for all your tables, and end up with a query like this:
SELECT
Vraboteni.v, Ulogi.p, Zarabotuva.honorar
FROM
Vraboteni AS v1
JOIN Ulogi AS u1 ON v1.v = u1.v
JOIN Zarabotuva AS z1 ON u1.p = z1.p
CROSS JOIN Vraboteni AS v2 -- Not sure if you would want/need a condition here
JOIN Ulogi AS u2 ON v2.v = u2.v
JOIN Zarabotuva AS z2 ON u2.p = z2.p
WHERE
v1.ima_uloga = 'sporedna' -- Not sure if this should be v1, u1 or z1
AND v2.ima_uloga = 'glavna'
AND z1.honorar > z2.honorar
this is what you need to do:
SELECT *
FROM
(SELECT Vraboteni.v
, Ulogi.p
, Zarabotuva.honorar
FROM Vraboteni
, Ulogi
, Zarabotuva
WHERE Vraboteni.v = Ulogi.v
AND ima_uloga = 'sporedna'
AND Ulogi.p = Zarabotuva.p) AS F
JOIN
(SELECT Vraboteni.v
, Ulogi.p
, Zarabotuva.honorar
FROM Vraboteni
, Ulogi
, Zarabotuva
WHERE Vraboteni.v = Ulogi.v
AND ima_uloga = 'glavna'
AND Ulogi.p = Zarabotuva.p) AS S ON F.honorar > S.honorar;

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

Oracle SELECT help

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