How to fix error 1052 relating to SQL alias [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I received 1052 error code. How can I add an alias to this code below?
SELECT consumerid as , count(stars) as starcount
FROM consumer left JOIN review on consumer.consumerid = review.consumerid
group by consumerid
ORDER by reviewcount asc, consumerid asc;

consumer_id is ambiguous in the query since it belongs to both tables. You need to qualify it with the identifier of the table it belongs to. It is generally a good practice in SQL to qualify all columns in multi-table queries.
select c.consumerid, count(r.stars) as review_count
from consumer c
left join review r on c.consumerid = r.consumerid
group by c.consumerid
order by review_count, c.consumerid;

Related

Msg 8120 - Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
select sum(orderinfo.orderamount) as total, userinfo.username
from orderinfo
left join userinfo on orderinfo.userid = userinfo.userid
group by orderinfo.userid
order by total desc
Table orderinfo:
userid
orderamount
Table userinfo:
userid
username
I will need to search for top 10 most buy users with their names.
But I got this error:
Msg 8120, Level 16, State 1, Line 1
Column 'userinfo.username' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I am using SSMS. I must be doing something wrong...please help
Thanks
You cannot aggregate by the userid and also select the username. The quickest fix here would be to aggregate by both the userid and username:
SELECT SUM(oi.orderamount) AS total, ui.username
FROM orderinfo oi
LEFT JOIN userinfo ui ON oi.userid = ui.userid
GROUP BY ui.userid, ui.username
ORDER BY total DESC;

How to update a column of a temp table by joining 2 seperate tables? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
How can I update a temp table column by joining 2 separate tables?
I had something like this -
UPDATE t
SET t.ContactCode = PC.Code
FROM #tempUsers t,
Users U LEFT JOIN PhoneCodes PC ON U.ContactCodeId = PC.PhoneCodes_Id
So here, #tempUsers is my temp table and Phone Codes and Users are two different tables.
This is running with no syntax error, but the column values are all displaying as null, which is not correct.
I'm not sure what I'm doing wrong in the query.
You need to have a joining column between #tempUsers and users table to update #tempUsers (where t.userid=u.id).
update #tempUsers
set t.ContactCode = PC.Code
from Users U
LEFT JOIN PhoneCodes PC
ON U.ContactCodeId = PC.PhoneCodes_Id
where #tempUsers.userid=u.id
It would be better if you share structures of those three tables along with some sample data.
You should use proper JOINs, then you would have seen that you didn't define a condition to join the table Users.
You are looking for something like this :
UPDATE T SET T.ContactCode = PC.Code
FROM #tempUsers T
INNER JOIN Users U ON U.Code = T.UserCode
LEFT JOIN PhoneCodes PC ON PC.PhoneCodes_Id = U.ContactCodeId

Issue with "group by" and "having" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
SELECT *
FROM RF_CustomerCard
WHERE DateOfBirth IN (
SELECT DateOfBirth
FROM RF_CustomerCard
HAVING COUNT(DateOfBirth) > 1
GROUP BY DateOfBirth
)
SQL Server 2019 is saying I have a syntax error by "GROUP"
I am trying to find employees who have the same date of birth
As D. Use a GROUP BY clause with a HAVING clause from Microsofts's SELECT - GROUP BY- Transact-SQL shows HAVING comes after GROUP BY:
SELECT *
FROM RF_CustomerCard
WHERE DateOfBirth IN (
SELECT DateOfBirth
FROM RF_CustomerCard
GROUP BY DateOfBirth
HAVING COUNT(DateOfBirth) > 1
)
ORDER BY DateOfBirth -- Optional

How can I extract both column from View named "Max_part_supplied'? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have created a view named 'Max_part_supplied' which consists of two column as
Number_of_parts and Supplier_name.
I am trying to access the Supplier_name who supplies maximum number of parts, as
select MAX(Number_of_parts) as max_part, Supplier_name
from Max_part_supplied;
This is the view Max_part_supplied
Number_of_parts Supplier_name
1 Ambani Traders
3 Lalu Traders
2 Paltu Traders
1 Sunil Traders
But I am getting error message as
Msg 8120,Level 16,State 1,line 84
Column Max_part_supplied.Supplier_name is invalid in the select list because it is not containted
in either aggregate function or the GROUP BY clause.
It looks like your view already has the max per supplier, you just want the maximum of these?
select top (1) Number_of_parts, Supplier_name
from Max_part_supplied
order by Number_of_parts desc
If you want all suppliers in the event two or more have the same max, use top (1) with ties

SQL QUERY for finding set of highest numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how to get top 1,00,000 customers name and email id who have booked maximum number of tickets? The table has columns like:
NAME, CUST_ID, JOINING DATE, NO. OF TICKETS BOOKED, EMAIL_ID.
Something like the following should work if you are using Microsoft Sql server (tsql)
Select TOP 120 columns FROM table ORDER BY columns desc
SELECT TOP 100000 NAME, CUST_ID, [JOINING DATE], [NO. OF TICKETS BOOKED], EMAIL_ID
FROM YOUR_TABLE
ORDER BY [NO. OF TICKETS BOOKED] DESC
Are you looking for this ?
if you are working with SQL SERVER