你这是交叉表查询
如果是2000的话就需要你自己写代码实现,你可上网搜索下,大把范例
如果是2005的话好象自身就可以支持交叉表查询
-------------------------------------2000代码如下
Create Table #product([ID] Varchar(10),[Name] Varchar(10))
Create Table #support([ID] Varchar(10),[Name] Varchar(10))
Create Table #uninSale([ID] Varchar(10),[Name] Varchar(10),SID Varchar(10),PID varchar(10),[count] varchar(10))
INSERT INTO #product values('1','杯子')
INSERT INTO #product values('2','手巾')
INSERT INTO #product values('3','桌子')
INSERT INTO #support values('1','张三')
INSERT INTO #support values('2','李四')
INSERT INTO #support values('3','王五')
INSERT INTO #uninSale values('1','单1','1','1',10)
INSERT INTO #uninSale values('2','单2','1','1',20)
INSERT INTO #uninSale values('3','单3','1','2',10)
INSERT INTO #uninSale values('4','单4','2','2',10)
INSERT INTO #uninSale values('5','单5','2','2',20)
INSERT INTO #uninSale values('6','单6','2','3',10)
INSERT INTO #uninSale values('7','单7','3','3',10)
INSERT INTO #uninSale values('8','单8','3','3',20)
INSERT INTO #uninSale values('9','单9','3','1',10)
--SELECT * FROM #uninSale
declare @sql varchar(8000)
set @sql=''
select @sql=@sql + 'sum(case PID when'''+ [ID] +'''
then [count] else 0 end) as'''+ [Name] +''','
from #product
--print '1:' + @sql
SET @sql= 'SELECT
#support.[Name] AS 供应商,' + LEFT(@sql,LEN(@sql)-1) +
' FROM #uninSale INNER JOIN #support ON #uninSale.SID=#support.[ID] GROUP BY #support.[Name] '
--print '2:' + @sql
exec(@sql)
Drop Table #uninSale
Drop Table #product
Drop Table #support
----------------------------------------结果
李四
0
30
10
王五
10
0
30
张三
30
10
0