日期:2014-05-19  浏览次数:20716 次

SQL 查询问题
日期                           编码               排量
2007-1-4   0:00:00 011 358.790000
2007-1-5   0:00:00 011 179.000000
2007-1-5   0:00:00 015 180.000000
2007-1-6   0:00:00 060 160.000000
以     日期为   yyyymm   格式     和编码     同时分组     进行查询显示

要求显示结果为

200701 011 537.790000
200701 015 180.000000
200701 060 160.000000




------解决方案--------------------
不懂什么意思?

------解决方案--------------------
Select
Convert(Varchar(6), 日期, 112) As 日期,
编码,
SUM(排量) As 排量
From

Group By
Convert(Varchar(6), 日期, 112), 编码
Order By
日期, 编码
------解决方案--------------------
select (cast(DATEPART(yy,日期) as char(4))+cast(DATEPART(mm,日期)as char(2))) as date,编码,sum(排量)
from table1
group by date,编码
------解决方案--------------------
Select
convert(varchar(6),(left(日期, 4)+substring(日期, 6,2)),112) As 日期,
编码,
SUM(排量) As 排量
From

Group By
convert(varchar(6),(left(日期, 4)+substring(日期, 6,2)),112)
, 编码
Order By
日期, 编码

------解决方案--------------------
用于月度统计的情况
sql里用datepart方法就好
------解决方案--------------------
DECLARE @temp TABLE(Month VARCHAR(7), ID int, Amount MONEY)

INSERT INTO
SELECT CONVERT(VARCHAR(7), Date, 127), ID, Amount
FROM table

SELECT Moneth ID, SUM(Amount)
FROM @temp
GROUP BY Month, ID