[bo][un]西风独自凉[/un] 在 2008-11-7 09:29 的发言:[/bo]
建表﹕
select * into xx from (
select '鋁' as ScrapeType,'片材' as RawMType ,23.00 as qty union all
select '鐵' ,'片材',799.60 union all
select '鋁' ,'料渣',16.20 union all
select '鋼' ,'料渣' ...
很典型的交叉表问题 若使用的是2000的话
select * into #ls1 from (
select '鋁' as ScrapeType,'片材' as RawMType
,23.00
as qty union all
select '鐵'
,'片材',799.60
union all
select '鋁'
,'料渣',16.20
union all
select '鋼'
,'料渣',32.00
union all
select '鐵'
,'料渣',105.00
union all
select '鋁'
,'料頭',1.50
union all
select '鐵'
,'料頭',16.40
union all
select '鐵'
,'片材',10 )dd
SELECT * FROM #ls1
SELECT RawMType,SUM(CASE ScrapeType WHEN
'銅' THEN qty ELSE 0 END) AS 銅,
SUM(CASE ScrapeType WHEN
'鐵' THEN qty ELSE 0 END) AS 鐵,
SUM(CASE ScrapeType WHEN
'鋁' THEN qty ELSE 0 END) AS 鋁,
SUM(CASE ScrapeType WHEN
'鋼' THEN qty ELSE 0 END) AS 鋼
FROM #ls1 GROUP BY RawMType
若是2000以上的版本 好象数据库本身就支持交叉表查询
[[it] 本帖最后由 happynight 于 2008-11-7 14:00 编辑 [/it]]