Error #1349: View's select cluse contains a subquery in the from clause - sql

Here is my code:
SELECT CAST.first_name,
CAST.last_name,
AWARDED.castID,
COUNT(*) AwardsWon
FROM AWARDED,
ROLE,
CAST
WHERE ROLE.role = "Director"
AND ROLE.castID = AWARDED.castID
AND ROLE.movieID = AWARDED.movieID
AND AWARDED.castID = CAST.castID
GROUP BY castID
HAVING COUNT(*) =
(SELECT MAX(cnt) FROM
(SELECT COUNT(*) cnt FROM AWARDED GROUP BY CastID
) z
)
When I try and create a view from this code I get an error of the form: #1349 view's select contains a subquery in the from clause. Are there any ways that this can be worked around?

The statement says...
• The SELECT statement cannot contain a subquery in the FROM clause.
Your workaround would be to create a view for each of your subqueries.
Then access those views from within your view

If I am not wrong you are using MySQL. Not sure but your said query can be transformed as below. give it a try.
SELECT `CAST`.first_name,
`CAST`.last_name,
AWARDED.castID,
COUNT(AWARDED.castID) AwardsWon
FROM AWARDED
JOIN ROLE
ON ROLE.movieID = AWARDED.movieID
AND ROLE.castID = AWARDED.castID
JOIN `CAST`
ON AWARDED.castID = `CAST`.castID
WHERE ROLE.role = 'Director'
GROUP BY AWARDED.castID,`CAST`.first_name,`CAST`.last_name
HAVING COUNT(*) = MAX(AwardsWon);

Related

How can I pick the latest record from the left join in SQL Server?

I have a table view defined as:
CREATE view [SIR_SUMMARY]
AS
SELECT
dimsite.[SiteCode],
dimsite.[SiteName],
dimsite.[CityOrLocal] as City,
dimsite.[State],
dimsite.PostCode as Zip,
analysis.[ProductName] as GardeName,
analysis.[AnalysisTypeName] as ReportingType,
analysis.LastObsDate as ReportingDate
FROM
LOBDW.bjs.udv_BJS_DIM_SITE dimsite
LEFT JOIN
[LOBDW].[bjs].[udv_BJS_FACT_SIRA_ANALYSES] analysis ON dimsite.LegSiteId = analysis.LegSiteId
The above view returns three records for different ReportingDate; I would like to return single row for the latest ReportingDate.
How can I do that?
You can use window functions:
create view [SIR_SUMMARY] as
select . . .
from LOBDW.bjs.udv_BJS_DIM_SITE bds left join
(select a.*,
row_number() over (partition by LegSiteId order by ReportingDate desc) as seqnum
from [LOBDW].[bjs].[udv_BJS_FACT_SIRA_ANALYSES] a
) a
on bds.LegSiteId = a.LegSiteId and a.seqnum = 1;
Note that I changed the table aliases. I think shorter ones are easier to use in the query.

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

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.

SQL Correlation error using OVER & PARTITION in SELECT statement

I am getting the following error when I am trying to execute my SQL SELECT statement
Could not execute statement.
Correllation name 'contact' not found
SQLCODE=-142, ODBC 3 State"42S02"
Line 1, Column 1
My code is as follows
Select forename, surname, email, quotedate
From ( SELECT *, ROW_NUMBER() OVER (PARTITION BY tblQuote.contno ORDER BY quoteno DESC) AS rn
FROM dba.quotehdr as tblQuote left join dba.contact as tblContact on tblQuote.contno = tblContact.contno)q
where rn = 1 and quotedate <=today()-720 and emailbounced = 0 and email is not null and dba.contact.statusflag = 'A'
order by quotedate desc
This error only happended when I added in
dba.contact.statusflag = 'A'
I have tried this as
tblContact.statusflag = 'A'
and I get the same error!
Any suggestions?
(What about q.statusflag = 'A' , as it seems you are using q as an Alias.) This original answer is not correct, amended to:
#Shannon Severance is correct in his comment. You are trying to use the Where clause on the outer query - which does not contain any fields from the contact table. Let me tidy your query to help you see your subquery (q) - as:
Select
forename
,surname
,email
, quotedate
From
(
SELECT
*
, ROW_NUMBER() OVER (PARTITION BY tblQuote.contno ORDER BY quoteno DESC) AS rn
FROM dba.quotehdr as tblQuote
left join dba.contact as tblContact on tblQuote.contno = tblContact.contno
) q
left join dba.contact as tblContact on q.contno = tblContact.contno
where rn = 1
and quotedate <=today()-720
and emailbounced = 0
and email is not null
and tblContact.statusflag = 'A' -- Now sourced from last left join
order by quotedate desc
You will need another LEFT JOIN on the dba.contact table to be able to access this field (ADDED NOW as an example).
Also, depending on your database engine - if your field is duplicated in both tables, the SELECT * in a subquery may eject those fields, or rename them, or throw an error. Run your inner subquery by itself and see what it produces, or use explicit field name instead of *
(I still really think your * in the subquery is causing the error and also the confusion. Remove it and replace with table.field names - this will help you understand what is going wrong ...Otherwise your query logic is pretty fine, and adding the extra left join that I suggest is overkill)

Only one expression can be specified in the select list

I need to get this sql query working:
SELECT TOP 15 id, thumb, width, height
FROM (SELECT vPictures.id, vPictures.thumb, vPictureCrops.width, vPictureCrops.height
FROM vPictures INNER JOIN
vPictureCrops ON vPictures.id = vPictureCrops.picid
WHERE (vPictureCrops.width = '602') AND (vPictureCrops.height = '131')
GROUP BY vPictures.id, vPictures.thumb, vPictureCrops.width, vPictureCrops.height) AS derivedtbl_1
WHERE (id NOT IN
(SELECT TOP 0 vPictures_1.id, vPictures_1.datetime, vPictures_1.url, vPictures_1.author, vPictures_1.companyID, vPictures_1.source,
vPictures_1.people, vPictures_1.text, vPictures_1.thumb, vPictures_1.logo, vPictureCrops_1.id AS Expr1, vPictureCrops_1.picid,
vPictureCrops_1.url AS Expr2, vPictureCrops_1.width, vPictureCrops_1.height
FROM vPictures AS vPictures_1 INNER JOIN
vPictureCrops AS vPictureCrops_1 ON vPictures_1.id = vPictureCrops_1.picid))
ORDER BY id DESC
Can you help me?
The error message:
"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS)"
The Where (ID not IN(
The Select must only have one field are the IN can not understand which column you are trying to parse.
Eg from here
USE AdventureWorks2008R2;
GO
SELECT p.FirstName, p.LastName
FROM Person.Person AS p
JOIN Sales.SalesPerson AS sp
ON p.BusinessEntityID = sp.BusinessEntityID
WHERE p.BusinessEntityID IN
(SELECT BusinessEntityID
FROM Sales.SalesPerson
WHERE SalesQuota > 250000);
GO
Not sure what you are trying to achieve with the query anyway, can you explain the usage of the where in clause,
I can see two problems with (id NOT IN (SELECT TOP 0 vPictures_1.id, ...
only when column should be specified in the select statement after IN. For example (id NOT IN (SELECT vPictures_1.id FROM ...
even if you make it just one field vPictures_1.id the condition will always be false because of top 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;