SQL Teradata TOP and DISTINCT - sql

I'm trying to write a nested TOP + DISTINCT query in Teradata SQL. My query looks like this:
SELECT TOP 5
*
FROM
(SELECT DISTINCT k_name.KUNDE_NAME1
FROM DB_DWH_MART_AKM_PLT.VW_F_EVENT f_ev
INNER JOIN DBX_DWH_SBX_AKM_PRD.TB_KUNDE_EKP_NAME_AKTUELL k_name ON f_ev.AUFTRAGGEBER_EKP = k_name.EKP
WHERE f_ev.PROCESS_NO = 1075)
I get an error:
Expected something like a name or a Unicode delimited identifier... between ) and ;".
I don't know what I did wrong.
The DISTINCT query would execute correctly on its own.

Why not use a group by?
SELECT TOP 5 k_name.KUNDE_NAME1
FROM DB_DWH_MART_AKM_PLT.VW_F_EVENT f_ev
INNER JOIN DBX_DWH_SBX_AKM_PRD.TB_KUNDE_EKP_NAME_AKTUELL k_name ON f_ev.AUFTRAGGEBER_EKP = k_name.EKP
WHERE f_ev.PROCESS_NO = 1075
GROUP BY k_name.KUNDE_NAME1 --instead of using distinct
ORDER BY k_name.KUNDE_NAME1; --remove or modify as needed

Related

SQL Selecting data from 3 tables using GROUP BY

I am trying to write a formula to pull data from 3 tables and struggling to get it working.
I need to use the InventoryNbr from table s and do a group by, because there is many of the same InventoryNbr's, and I just want the MAX IndexListID Returned. The IndexListID is a Unique Key, so when I search on table il, I should only return 1 row. I want to then pull the end year from that row as well as the modelnm, and use those 2 values to get the CarlineNm. Here is my code:
SELECT s.InventoryNbr, MAX(s.IndexListID) AS IndexListID, il.EndYear, c.CarlineNm
FROM sysidla as s
INNER JOIN IndexList as il
ON s.IndexListID = il.IndexListID
INNER JOIN Carline as c
ON il.EndYear = c.CarlineYear
AND il.ModelNm = c.ModelNm
GROUP BY InventoryNbr
ORDER BY InventoryNbr ASC;
The error I keep getting is:
Column 'IndexList.EndYear' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Here is some sample data:
sysidla
InventoryNbr|IndexListID|Junk|Junk2
12345|1|x|y
12345|2|c|r
12345|3|c|e
12346|4|e|w
IndexList
ModelNm|Junk|Junk1|Junk3|EndYear|IndexListID
name1|c|f|r|2004|1
name2|c|f|r|2008|2
name3|c|f|r|2012|3
name4|c|f|r|2004|4
name5|c|f|r|2018|5
Carline
CarlineYear|CarlineNm|Junk9|ModelNm
2005|NAME|d|name1
2012|NAME22|d|name3
2005|NAME354|d|name4
2005|NAME1|d|name5
So for instance, this is an incomplete data sample because every IndexListID will have a match in IndexList, but I want to be able match InventoryNbr 12345 and select the max INDEXLISTID which is 3, then use 3 on IndexList to grab name3 and 2012. Then I want to use 2012 and name3 to get NAME22 From Carine.
Use window functions in a subquery:
SELECT s.InventoryNbr, s.IndexListID, il.EndYear, c.CarlineNm
FROM (SELECT s.*,
ROW_NUMBER() OVER (PARTITION BY il.IndexListID ORDER BY il.IndexListID DESC) as seqnum
FROM sysidla s
) s INNER JOIN
IndexList il
ON s.IndexListID = il.IndexListID INNER JOIN
Carline c
ON il.EndYear = c.CarlineYear AND
il.ModelNm = c.ModelNm
WHERE seqnum = 1
ORDER BY InventoryNbr ASC;
No aggregation is needed.
Your are getting this error because, there is a problem with your group by and SELECT section . You can not select column while using group by like this. Either you have to use an aggregate function or your column should in the group by to select like this.
So,Here is a solution you can check =>
SELECT T.*,il.EndYear, c.CarlineNm FROM
(SELECT s.InventoryNbr, MAX(s.IndexListID) AS IndexListID
FROM sysidla as s
GROUP BY InventoryNbr) T
INNER JOIN IndexList as il ON T.IndexListID = il.IndexListID
INNER JOIN Carline as c ON il.EndYear = c.CarlineYear AND il.ModelNm = c.ModelNm
ORDER BY T.InventoryNbr ASC;
Note: This code is not optimized. Sub-query is little slow however, you can optimize that using window function (CTE). Please check and let me know.

subquery returning more than 1 values

SELECT user_info.s_name, user_info.name, user_info.f_name, user_info.usr_id, user_info.img_path, Village_master.v_nm
FROM Village_master
INNER JOIN User_reg_master ON Village_master.v_id = User_reg_master.v_id
INNER JOIN user_info ON User_reg_master.usr_id = user_info.usr_id
WHERE user_info.usr_id NOT LIKE #u_id
AND user_info.usr_id NOT LIKE (
SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids
WHERE pers_dict_master.usr_id=#u_id
)
usr_id| pers_dict_usr_id
1 | 13
1 | 6
The problem you are facing is this ,
user_info.usr_id NOT LIKE
(SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids
WHERE pers_dict_master.usr_id=#u_id)
This is your sub query
(SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids
WHERE pers_dict_master.usr_id=#u_id)
It is fetching two columns.But In the LIKE command you are using a one Column.
So make it work USE NOT INCOMMAND
SELECT user_info.s_name,
user_info.name,
user_info.f_name,
user_info.usr_id,
user_info.img_path,
Village_master.v_nm
FROM Village_master
INNER JOIN User_reg_master ON Village_master.v_id = User_reg_master.v_id
INNER JOIN user_info ON User_reg_master.usr_id = user_info.usr_id
WHERE user_info.usr_id NOT LIKE #u_id
AND user_info.usr_id NOT IN
( SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids
WHERE pers_dict_master.usr_id=#u_id )
You should probably include some more detail about your issue. Is the sub query expected to return more than one result? If so then you could simply use TOP(ie. select top 1 ...) to get just a single result and add an ORDER BY to the sub query if you want to get a certain sorted top value from that result set.
If the issue is that the sub query returns more than 1 result when it shouldn't, then your problem lies deeper. It looks like you may have meant to use IN for the subquery and for a different comparison, but it's difficult to tell. Perhaps you meant this?:
user_info.usr_id NOT IN
(SELECT pers_dict_master.pers_dict_ids
FROM pers_dict_ids WHERE pers_dict_master.usr_id=#u_id)

select query with Not IN keyword

I have two Tables as below..
tbPatientEncounter
tbVoucher
when i execute select query as below
Select EncounterIDP,EncounterNumber from tbPatientEncounter
it returens me 180 rows. and
Select VoucherIDP,EncounterIDF from tbVoucher
above query returns me 165 rows.
but i want to execute select query that returns me data like EncounterIDP not in tbVoucher, for that i have tried below Select query...
Select * from tbPatientEncounter pe
where pe.EncounterIDP not in
(Select v.EncounterIDF from tbVoucher v )
it doesn't returns any row. in first image it shows EncounterIDP 9 in tbPatientEncounter, but it not inserted in tbVoucher for that i have tried select Query like
Select * from tbVoucher where EncounterIDF = 9
it returns me 0 rows.
My question is what is wrong with my above Not In Query.?
In all likelihood, the problem is NULL values in tbVoucher. Try this:
Select *
from tbPatientEncounter pe
where pe.EncounterIDP not in (Select v.EncounterIDF
from tbVoucher v
where v.EncounterIDF is not NULL
)
Are you comparing the correct fields in tbVoucher?
Try using a left join
Select EncounterIDP,EncounterNumber from tbPatientEncounter
left join tbVoucher on EncounterIDP = EncounterIDF
where EncounterIDF is null
Call me a skeptic because I don't see anything wrong with your query. Is this really all in the query or did you simplify it for us?
Select * from tbPatientEncounter pe
where pe.EncounterIDP not in
(Select v.EncounterIDF from tbVoucher v )

MS-Access -> SELECT AS + ORDER BY = error

I'm trying to make a query to retrieve the region which got the most sales for sweet products. 'grupo_produto' is the product type, and 'regiao' is the region. So I got this query:
SELECT TOP 1 r.nm_regiao, (SELECT COUNT(*)
FROM Dw_Empresa
WHERE grupo_produto='1' AND
cod_regiao = d.cod_regiao) as total
FROM Dw_Empresa d
INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao ORDER BY total DESC
Then when i run the query, MS-Access asks for the "total" parameter. Why it doesn't consider the newly created 'column' I made in the select clause?
Thanks in advance!
Old Question I know, but it may help someone knowing than while you cant order by aliases, you can order by column index. For example, this will work without error :
SELECT
firstColumn,
IIF(secondColumn = '', thirdColumn, secondColumn) As yourAlias
FROM
yourTable
ORDER BY
2 ASC
The results would then be ordered by the values found in the second column wich is the Alias "yourAlias".
Aliases are only usable in the query output. You can't use them in other parts of the query. Unfortunately, you'll have to copy and paste the entire subquery to make it work.
You can do it like this
select * from(
select a + b as c, * from table)
order by c
Access has some differences compared to Sql Server.
Why it doesn't consider the newly
created 'column' I made in the select
clause?
Because Access (ACE/Jet) is not compliant with the SQL-92 Standard.
Consider this example, which is valid SQL-92:
SELECT a AS x, c - b AS y
FROM MyTable
ORDER
BY x, y;
In fact, x and y the only valid elements in the ORDER BY clause because all others are out of scope (ordinal numbers of columns in the SELECT clause are valid though their use id deprecated).
However, Access chokes on the above syntax. The equivalent Access syntax is this:
SELECT a AS x, c - b AS y
FROM MyTable
ORDER
BY a, c - b;
However, I understand from #Remou's comments that a subquery in the ORDER BY clause is invalid in Access.
Try using a subquery and order the results in an outer query.
SELECT TOP 1 * FROM
(
SELECT
r.nm_regiao,
(SELECT COUNT(*)
FROM Dw_Empresa
WHERE grupo_produto='1' AND cod_regiao = d.cod_regiao) as total
FROM Dw_Empresa d
INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao
) T1
ORDER BY total DESC
(Not tested.)
How about:
SELECT TOP 1 r.nm_regiao
FROM (SELECT Dw_Empresa.cod_regiao,
Count(Dw_Empresa.cod_regiao) AS CountOfcod_regiao
FROM Dw_Empresa
WHERE Dw_Empresa.[grupo_produto]='1'
GROUP BY Dw_Empresa.cod_regiao
ORDER BY Count(Dw_Empresa.cod_regiao) DESC) d
INNER JOIN tb_regiao AS r
ON d.cod_regiao = r.cod_regiao
I suggest using an intermediate query.
SELECT r.nm_regiao, d.grupo_produto, COUNT(*) AS total
FROM Dw_Empresa d INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao
GROUP BY r.nm_regiao, d.grupo_produto;
If you call that GroupTotalsByRegion, you can then do:
SELECT TOP 1 nm_regiao, total FROM GroupTotalsByRegion
WHERE grupo_produto = '1' ORDER BY total DESC
You may think it's extra work to create the intermediate query (and, in a sense, it is), but you will also find that many of your other queries will be based off of GroupTotalsByRegion. You want to avoid repeating that logic in many other queries. By keeping it in one view, you provide a simplified route to answering many other questions.
How about use:
WITH xx AS
(
SELECT TOP 1 r.nm_regiao, (SELECT COUNT(*)
FROM Dw_Empresa
WHERE grupo_produto='1' AND
cod_regiao = d.cod_regiao) as total
FROM Dw_Empresa d
INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao
) SELECT * FROM xx ORDER BY total

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;