select [单位],[品种],[月份],
sum(case when [月份]=1 then [数量] else 0 end) as '一月数量合计',
sum(case when [月份]=2 then [数量] else 0 end) as '二月数量合计',
sum(case when [月份]=3 then [数量] else 0 end) as '三月数量合计',
sum(case when [月份]=4 then [数量] else 0 end) as '四月数量合计',
sum(case when [月份]=5 then [数量] else 0 end) as '五月数量合计',
sum(case when [月份]=6 then [数量] else 0 end) as '六月数量合计',
sum(case when [月份]=7 then [数量] else 0 end) as '七月数量合计',
sum(case when [月份]=8 then [数量] else 0 end) as '八月数量合计',
sum(case when [月份]=9 then [数量] else 0 end) as '九月数量合计',
sum(case when [月份]=10 then [数量] else 0 end) as '十月数量合计',
sum(case when [月份]=11 then [数量] else 0 end) as '十一月数量合计',
sum(case when [月份]=12 then [数量] else 0 end) as '十二月数量合计'
from testqty
group by [单位],[品种],[月份]
sum(case when [月份]=1 then [数量] else 0 end) as '一月数量合计',
sum(case when [月份]=2 then [数量] else 0 end) as '二月数量合计',
sum(case when [月份]=3 then [数量] else 0 end) as '三月数量合计',
sum(case when [月份]=4 then [数量] else 0 end) as '四月数量合计',
sum(case when [月份]=5 then [数量] else 0 end) as '五月数量合计',
sum(case when [月份]=6 then [数量] else 0 end) as '六月数量合计',
sum(case when [月份]=7 then [数量] else 0 end) as '七月数量合计',
sum(case when [月份]=8 then [数量] else 0 end) as '八月数量合计',
sum(case when [月份]=9 then [数量] else 0 end) as '九月数量合计',
sum(case when [月份]=10 then [数量] else 0 end) as '十月数量合计',
sum(case when [月份]=11 then [数量] else 0 end) as '十一月数量合计',
sum(case when [月份]=12 then [数量] else 0 end) as '十二月数量合计'
from testqty
group by [单位],[品种],[月份]
可以直接通过ADO执行
dim rs as adodb.recordset
dim sql as string sql="select [单位],[品种],[月份]," & _
"sum(case when [月份]=1 then [数量] else 0 end) as '一月数量合计'," & _
"sum(case when [月份]=2 then [数量] else 0 end) as '二月数量合计'," & _
"sum(case when [月份]=3 then [数量] else 0 end) as '三月数量合计'," & _
"sum(case when [月份]=4 then [数量] else 0 end) as '四月数量合计'," & _
"sum(case when [月份]=5 then [数量] else 0 end) as '五月数量合计'," & _
"sum(case when [月份]=6 then [数量] else 0 end) as '六月数量合计'," & _
"sum(case when [月份]=7 then [数量] else 0 end) as '七月数量合计'," & _
"sum(case when [月份]=8 then [数量] else 0 end) as '八月数量合计'," & _
"sum(case when [月份]=9 then [数量] else 0 end) as '九月数量合计'," & _
"sum(case when [月份]=10 then [数量] else 0 end) as '十月数量合计'," & _
"sum(case when [月份]=11 then [数量] else 0 end) as '十一月数量合计'," & _
"sum(case when [月份]=12 then [数量] else 0 end) as '十二月数量合计'" & _
"from testqty" & _
"group by [单位],[品种],[月份] "
set rs=new adodb.recordset
rs.ActiveConnection ="driver=sql server;server=yourdatabaseserver;uid=sa;pwd=;database=yourdatabase"
rs.open sql
'so rs 就是你需要的结果集