I have to fetch both month name and year from field in my table but writing below query I am getting an error. Kindly do the needful to rectify my error. Thanks in advance.
Query:
select SUM(p.Actual_Amount) as buying,
SUM(p.TotalPackageCost) as selling,
(SUM(p.TotalPackageCost) - SUM(p.Actual_Amount)) as profit,
COUNT(e.Enquiry_Id) as Sales,
DATENAME(MM,e.Arrives_On) as Month,
(DATENAME(MONTH,e.Arrives_On) +'-'+ DATENAME(YYYY,e.Arrives_On)) as Date
from Pricing p
inner join Enquiry e on e.Enquiry_Id = p.Enquiry_Id where e.Agent_Name ='Ish Travels' and e.status = 'Confirmed By Company'
group by DATENAME(MM,e.Arrives_On)
order by Month desc
But I am getting the error:
Column 'Enquiry.Arrives_On' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Please help me to resolve the error.
Your group by clause is not complete. Every expression in your projection which is not contained in an aggregate function has to be used in group by. I'm pretty sure this is working:
select
SUM(p.Actual_Amount) as buying,
SUM(p.TotalPackageCost) as selling,
(SUM(p.TotalPackageCost) - SUM(p.Actual_Amount)) as profit,
COUNT(e.Enquiry_Id) as Sales,
DATENAME(MM,e.Arrives_On) as Month,
(DATENAME(MONTH,e.Arrives_On) +'-'+ DATENAME(YYYY,e.Arrives_On)) as Date
from
Pricing p inner join Enquiry e on e.Enquiry_Id = p.Enquiry_Id
where
e.Agent_Name ='Ish Travels'
and
e.status = 'Confirmed By Company'
group by
DATENAME(MM,e.Arrives_On),
(DATENAME(MONTH,e.Arrives_On) +'-'+ DATENAME(YYYY,e.Arrives_On))
order by
5 desc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.