日期:2014-05-18  浏览次数:20547 次

有没有写存储过程的高手帮我解决个问题啊???
是这样的...
我要从A表里以date为参数查出多条ID和content(ID是唯一的),再通过查询出的ID查询A表里另外一个date参数的title,name数据...然后把id,content,title,name   的数据全部整合起来得到一个数据条,多个数据...得一个结果集...


大家快帮帮忙!!!!!!!!!!!!!!!!在线等待.............盼啊..

------解决方案--------------------
既然楼主不说话,我先按照我理解的解答了!

create table MF_NetValue(fundcode varchar(6),AccumulatedUnitNV decimal(18,4),UnitNV decimal(18,4),enddate datetime)
insert MF_NetValue
select '020009 ',2.1740,1.2890, '2007-08-23 00:00:00 'union all
select '630001 ',1.3908,1.3908, '2007-08-23 00:00:00 'union all
select '020009 ',2.1920,1.3070, '2007-08-24 00:00:00 'union all
select '630001 ',1.4100,1.4100, '2007-08-24 00:00:00 '

select * from MF_NetValue
declare @sql VARCHAR(8000)

DECLARE @StartDate DATETIME--查询开始日期
DECLARE @EndDate DATETIME--查询结束日期

DECLARE @tmpCondition VARCHAR(2000)
DECLARE @tmpsql VARCHAR(8000)
DECLARE @CurrDate DATETIME
DECLARE @tmpDate VARCHAR(10)

set @StartDate= '2007-08-23 '
set @EndDate= '2007-08-25 '

SET @tmpCondition = 'WHERE enddate> = ' ' '+CONVERT(VARCHAR(10),@StartDate,120) + ' ' ' AND enddate <= ' ' '+CONVERT(VARCHAR(10),@EndDate,120) + ' ' ' '
SET @tmpsql= ' '
SET @CurrDate = @StartDate
while @CurrDate <= @EndDate
begin
SET @tmpDate = Convert(varchar(10),@CurrDate,121)
select @tmpsql = @tmpsql + ', SUM(CASE WHEN Convert(varchar(10),enddate,121) = ' ' '+@tmpDate+ ' ' ' THEN AccumulatedUnitNV ELSE 0.00 END) AS [ '+CONVERT(VARCHAR(10),@CurrDate,120)+ 'AccumulatedUnitNV] '
+ ', SUM(CASE WHEN Convert(varchar(10),enddate,121) = ' ' '+@tmpDate+ ' ' ' THEN UnitNV ELSE 0 END) AS [ '+CONVERT(VARCHAR(10),@CurrDate,120)+ 'UnitNV] '

select @CurrDate = @CurrDate +1
end
SET @tmpsql= 'SELECT fundcode ' +@tmpsql
+ ' FROM MF_NetValue '
+ @tmpCondition
+ ' GROUP BY fundcode '
PRINT @tmpsql
EXEC (@tmpsql)
drop table MF_NetValue


(4 行受影响)
fundcode AccumulatedUnitNV UnitNV enddate
-------- --------------------------------------- --------------------------------------- -----------------------
020009 2.1740 1.2890 2007-08-23 00:00:00.000
630001 1.3908 1.3908 2007-08-23 00:00:00.000
020009 2.1920 1.3070 2007-08-24 00:00:00.000
630001 1.4100 1.4100 2007-08-24 00:00:00.000

(4 行受影响)

SELECT fundcode , SUM(CASE WHEN Convert(varchar(10),enddate,121) = '2007-08-23 ' THEN AccumulatedUnitNV ELSE 0.00 END) AS [2007-08-23AccumulatedUnitNV], SUM(CASE WHEN Convert(varchar(10),enddate,121) = '2007-08-23 ' THEN UnitNV ELSE 0 END) AS [2007-08-23UnitNV], SUM(CASE WHEN Convert(varchar(10),enddate,121) = '2007-08-24 ' THEN AccumulatedUnitNV ELSE 0.00 END) AS [2007-08-24AccumulatedUnitNV], SUM(CASE WHEN Convert(varchar(10),enddate,121) = '2007-08-24 ' THEN UnitNV ELSE 0 END) AS [2007-08-24UnitNV], SUM(CASE WHEN Convert(varchar(10),enddate,121) = '2007-08-25 ' THEN AccumulatedUnitNV ELSE 0.00 END) AS [2007-08-25AccumulatedUnitNV], SUM(CASE WHEN Convert(varchar(10),enddate,121)