简体   繁体   English

如何从字段和 w 中获取月份名称和年份

[英]how to get both month name and year from field and by w

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.列“Enquiry.Arrives_On”在选择列表中无效,因为它既不包含在聚合函数中也不包含在 GROUP BY 子句中。

Please help me to resolve the error.请帮我解决错误。

Your group by clause is not complete.您的 group by 子句不完整。 Every expression in your projection which is not contained in an aggregate function has to be used in group by.投影中未包含在聚合函数中的每个表达式都必须在 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM