0
select
    Round(Sum(Totalamount) / Sum(totalweight), 2) as AVGRATE
    , DATENAME(MONTH, LIFTINGDATE) as [Month]
from k_LiftingEntryRecords 
where liftingdate BETWEEN '2013-04-01 00:00.000' AND '2013-07-30 00:00:00.000'
group by DATENAME(MONTH,LIFTINGDATE)
order by DATENAME(MONTH,LIFTINGDATE)

我得到了五月,六月,七月,四月。我需要四月,五月......

4

3 回答 3

1

尝试-:

SELECT DATENAME(MONTH, LIFTINGDATE) as [Month]
FROM (
    SELECT '2005-01-03 00:00:00.000' AS LIFTINGDATE
    UNION ALL SELECT '2005-07-06 00:00:00.000'
    UNION ALL SELECT '2005-03-03 00:00:00.000'
    UNION ALL SELECT '2005-06-05 00:00:00.000'
    UNION ALL SELECT '2005-04-02 00:00:00.000'
    UNION ALL SELECT '2005-05-04 00:00:00.000'
    UNION ALL SELECT '2005-02-16 00:00:00.000'
) AS X
GROUP BY DATENAME(MONTH, LIFTINGDATE), MONTH(LIFTINGDATE)
ORDER BY [Month]
于 2013-07-10T05:47:52.680 回答
1

在选择列表中添加另一个 MONTH(LIFTINGDATE) 列并按列表分组

SELECT  
 Round(Sum(Totalamount)/Sum(totalweight),2)as AVGRATE
,DATENAME(MONTH,LIFTINGDATE) 
,MONTH(LIFTINGDATE)
as [Month] from k_LiftingEntryRecords 
where (liftingdate BETWEEN '2013-04-01 00:00.000' AND '2013-07-30 00:00:00.000')
group by DATENAME(MONTH,LIFTINGDATE),MONTH(LIFTINGDATE)
order by MONTH(LIFTINGDATE)

如果您需要相同的列列表,那么

SELECT AVGRATE,Month_Name FROM
(
    SELECT  
     Round(Sum(Totalamount)/Sum(totalweight),2)as AVGRATE
    ,DATENAME(MONTH,LIFTINGDATE) AS Month_Name 
    ,MONTH(LIFTINGDATE) MonthNum
    as [Month] from k_LiftingEntryRecords 
    where (liftingdate BETWEEN '2013-04-01 00:00.000' AND '2013-07-30 00:00:00.000')
    group by DATENAME(MONTH,LIFTINGDATE),MONTH(LIFTINGDATE)
) T
ORDER BY MonthNum
于 2013-07-10T05:53:12.330 回答
-1

试试下面..

select  Round(Sum(Totalamount)/Sum(totalweight),2)as AVGRATE
,DATENAME(MONTH,LIFTINGDATE) as [Month] 
from k_LiftingEntryRecords 
where (liftingdate BETWEEN '2013-04-01 00:00.000' AND '2013-07-30 00:00:00.000')
group by DATENAME(MONTH,LIFTINGDATE) 
order by Month 
于 2013-07-10T05:56:16.437 回答