error is Reason for Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause - sql

I got an error
Column 'Employee.EmpID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
SQL code:
with cte(stu_id,term_cd, spcl_cd ) as
(
Select
zt.[STU_ID], zt.TERM_CD, zt.SPCL_CD
From
SR0TZT(nolock) zt
Inner Join
(Select
STU_ID, MIN(TERM_SEQ_NUM) MinPoint, SPCL_CD
From SR0TZT
Group By STU_ID) tbl1 On zt.STU_ID = tbl1.STU_ID
Where
tbl1.MinPoint = zt.TERM_SEQ_NUM
and zt.STU_ID = '202716354'
and tbl1.SPCL_CD = zt.SPCL_CD
)
SELECT
zt.[STU_ID], zt.[TERM_CD], zt.[SPCL_CD],
zt.[SPCL_STRT_TERM], zt.TERM_SEQ_NUM, t.term_id
FROM
SR0TZT zt
JOIN
cte ON zt.STU_ID = cte.stu_id
WHERE
zt.STU_ID = '202716354'
Condition is:
For each unique combination of TZT.STU_ID and TZT.SPCL_CD where TZT.COLL_CD = '', display the TZT.TERM_CD with the minimum TZT.TERM_SEQ_NUM.
For UID 202716354, based on the above rule, the value of this column is incorrect for both specialization codes.

Not sure I understand why you are getting an error for 'Employee.EmpID' as it doesn't appear in your query.
At first look I can see that the following part of your SQL code (the derived table 'tbl1')...
Select STU_ID,MIN(TERM_SEQ_NUM) MinPoint,SPCL_CD From SR0TZT Group By STU_ID
..is incorrect and would cause a similar error because SPCL_CD isn't used in an aggregate function (such as MIN) or in the GROUP BY. You should change it to:-
Select STU_ID,MIN(TERM_SEQ_NUM) MinPoint,SPCL_CD From SR0TZT Group By STU_ID, SPCL_CD
And that should solve your problem.

Your problem is in this below script:
Select STU_ID,MIN(TERM_SEQ_NUM) MinPoint,SPCL_CD From SR0TZT Group By STU_ID
It should be:
Select STU_ID,MIN(TERM_SEQ_NUM) MinPoint,SPCL_CD From SR0TZT Group By STU_ID, SPCL_CD
Every column that you put in select, you should put them too in group by.

Related

Cannot do inner join on an select, invalid in the select list

I have a stored proceedure that builds a temporary table and executes a select on the end. It works fine as long as I don't include the l.beskrivning which is a table that maps country codes to country names. The proceedure is valid but when I test it I get the following error:
Column 'LK.b' is invalid in the select list because it is
not contained in either an aggregate function or the GROUP BY clause.
SELECT TOP 100 tmp.BuyerID, tmp.BuyerNumber, tmp.BuyerName, tmp.BuyerAddress, tmp.BuyerCountryCode, l.b
FROM #tempGL AS tmp
INNER JOIN LK AS l ON l.land_id LIKE tmp.BuyerCountryCode
GROUP BY tmp.BuyerID, tmp.BuyerNumber, tmp.BuyerName, tmp.BuyerAddress, tmp.BuyerCountryCode
ORDER BY Count(*) DESC, tmp.BuyerName
I've also tried removing the GROUP BY and ORDER BY completely but I still get the same error. What am I doing wrong here?
You need to include l.beskrivning in group by clause
SELECT TOP 100 tmp.BuyerID, tmp.BuyerNumber, tmp.BuyerName, tmp.BuyerAddress, tmp.BuyerCountryCode, l.beskrivning
FROM #tempGL AS tmp
INNER JOIN LK AS l ON l.land_id LIKE tmp.BuyerCountryCode
GROUP BY tmp.BuyerID, tmp.BuyerNumber, tmp.BuyerName, tmp.BuyerAddress, tmp.BuyerCountryCode,l.beskrivning
ORDER BY Count(*) DESC, tmp.BuyerName
What part of the error message do you not understand? All columns not being aggregated should be in the GROUP BY:
SELECT TOP 100 t.BuyerID, t.BuyerNumber, t.BuyerName, tmp.BuyerAddress, t.BuyerCountryCode, l.beskrivning
FROM #tempGL t INNER JOIN
LK l
ON l.land_id LIKE t.BuyerCountryCode
GROUP BY t.BuyerID, t.BuyerNumber, t.BuyerName, t.BuyerAddress, t.BuyerCountryCode, l.beskrivning
ORDER BY Count(*) DESC, t.BuyerName

How to use outer field name or alias in a nested subquery

I would use the selected field Referencia in the subquery.
I have tried including the field name the alias, and table name but not works.
How I can achieve this ?
Thanks
SELECT * FROM
(
SELECT
articulos.Codigo AS Referencia,
articulos.Nombre AS Descripcion,
barras.Codigo AS [Codigo de Barras],
ROW_NUMBER() OVER (PARTITION BY articulos.Codigo ORDER BY
articulos.Codigo ASC) as cantidad,
articulos.Familia,
articulos.Marca,
categorias.Codigo as Categoria,
articulos.ImpuestoEspecial AS Ecotasa,
articulos.Fase,
articulos.Iva,
--
-- Tarifa1
( SELECT [Codigo],[EuroPrecio]
FROM [GES16100].[dbo].[Tarifas]
WHERE [Codigo] = 1 AND [Articulo] = <------- Here, Referencia
)AS T1,
articulos.Proveedor,
articulos.GUID_Registro
FROM [GES16100].[dbo].[Articulos] as articulos
FULL JOIN [GES16100].[dbo].[Barras] as barras
ON articulos.Codigo = barras.Articulo
FULL JOIN [GES16100].[dbo].[Categorias_Asignaciones] catasignaciones
ON catasignaciones.GUID_RegistroFichero =articulos.GUID_Registro
FULL JOIN [GES16100].[dbo].[CategoriasFicheros] categorias
ON categorias.GUID_Registro = catasignaciones.GUID_Categoria
)AS supersub
WHERE supersub.cantidad = 1
Use table aliases and qualified column names whenever you have more than one table in a query.
Second, your subquery will not work because it returns two columns where one is expected.
For your example, I am guessing:
( SELECT t.EuroPrecio
FROM [GES16100].[dbo].[Tarifas] t
WHERE t.Codigo = 1 AND t.Articulo = a.Codigo
) AS T1,
You cannot use the column alias Referencias because it is defined in the same SELECT. Just use the column it is refering to.

SQL error on ORDER BY in subquery (TOP is used)

I am getting Syntax error near 'ORDER' from the following query:
SELECT i.ItemID, i.Description, v.VendorItemID
FROM Items i
JOIN ItemVendors v ON
v.RecordID = (
SELECT TOP 1 RecordID
FROM ItemVendors iv
WHERE
iv.VendorID = i.VendorID AND
iv.ParentRecordID = i.RecordID
ORDER BY RecordID DESC
);
If I remove the ORDER BY clause the query runs fine, but unfortunately it is essential to pull from a descending list rather than ascending. All the answers I have found relating to this indicate that TOP must be used, but in this case I am already using it. I don't have any problems with TOP and ORDER BY when not part of a subquery. Any ideas?
This error has nothing to do with TOP.
ASE simply does not allow ORDER BY in a subquery. That's the reason for the error.
RecordID in the ORDER BY is ambiguous. Add the appropriate table alias in front of it (e.g., iv.RecordID).
I'd use max instead of top 1 ... order by
SELECT i.ItemID, i.Description, v.VendorItemID
FROM Items i
JOIN ItemVendors v ON
v.RecordID = (
SELECT max(RecordID)
FROM ItemVendors iv
WHERE
iv.VendorID = i.VendorID AND
iv.ParentRecordID = i.RecordID);

UsingCoaslesce function with distinct

I'm trying to fetch some data from a relational database and order it based on the payed_at column. In case payed_at is null, I want he updated_at column to be used instead. My query so far is bellow:
SELECT DISTINCT "orders".*
FROM "orders" INNER JOIN "order_items" ON "order_items"."order_id" = "orders"."id"
WHERE (orders.state = 'payed') AND (physical = true) AND (kind = 'output')
ORDER BY coalesce(orders.payed_at, orders.updated_at) DESC
LIMIT 200 OFFSET 0
I'm getting the following error from Postgres:
PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
It might be a problem with DISTINCT in the query, because when I remove DISTINCT word I get no error.
I'm using the coalesce function.
I'm stuck with this error.
The error states that when using SELECT DISTINCT and ordering by an expression, you must include the expression in the SELECT list. Your expression is the COALESCE.
SELECT DISTINCT coalesce(orders.payed_at, orders.updated_at),...
FROM "orders"
INNER JOIN "order_items" ON "order_items"."order_id" = "orders"."id"
WHERE (orders.STATE = 'payed')
AND (physical = true)
AND (kind = 'output')
ORDER BY coalesce(orders.payed_at, orders.updated_at) DESC LIMIT 200 OFFSET 0

MySQL subquery returns more than one row

I am executing this query:
SELECT
voterfile_county.Name,
voterfile_precienct.PREC_ID,
voterfile_precienct.Name,
COUNT((SELECT voterfile_voter.ID
FROM voterfile_voter
JOIN voterfile_household
WHERE voterfile_voter.House_ID = voterfile_household.ID
AND voterfile_household.Precnum = voterfile_precienct.PREC_ID)) AS Voters
FROM voterfile_precienct JOIN voterfile_county
WHERE voterfile_precienct.County_ID = voterfile_County.ID;
I am trying to make it return something like this:
County_Name Prec_ID Prec_Name Voters(Count of # of voters in that precienct)
However, I am getting the error:
#1242 - Subquery returns more than 1 row.
I have tried placing the COUNT statement in the subquery but I get an invalid syntax error.
If you get error:error no 1242 Subquery returns more than one row, try to put ANY before your subquery. Eg:
This query return error:
SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
This is good query:
SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
You can try it without the subquery, with a simple group by:
SELECT voterfile_county.Name,
voterfile_precienct.PREC_ID,
voterfile_precienct.Name,
count(voterfile_voter.ID)
FROM voterfile_county
JOIN voterfile_precienct
ON voterfile_precienct.County_ID = voterfile_County.ID
JOIN voterfile_household
ON voterfile_household.Precnum = voterfile_precienct.PREC_ID
JOIN voterfile_voter
ON voterfile_voter.House_ID = voterfile_household.ID
GROUP BY voterfile_county.Name,
voterfile_precienct.PREC_ID,
voterfile_precienct.Name
When you use GROUP BY, any column that you are not grouping on must have an aggregate clause (f.e. SUM or COUNT.) So in this case you have to group on county name, precienct.id and precient.name.
Try this
SELECT
voterfile_county.Name, voterfile_precienct.PREC_ID,
voterfile_precienct.Name,
(SELECT COUNT(voterfile_voter.ID)
FROM voterfile_voter JOIN voterfile_household
WHERE voterfile_voter.House_ID = voterfile_household.ID
AND voterfile_household.Precnum = voterfile_precienct.PREC_ID) as Voters
FROM voterfile_precienct JOIN voterfile_county
ON voterfile_precienct.County_ID = voterfile_County.ID
See the below example and modify your query accordingly.
select COUNT(ResultTPLAlias.id) from
(select id from Table_name where .... ) ResultTPLAlias;