set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetFiveDayPrice]
@ProductID int,
@flag bit
AS
BEGIN
create table #temp1
(
QasUnit varchar(50),
monthAvgPrice float(20),
weekAvgPrice float(20),
price1 float(20),
price2 float(20),
price3 float(20),
price4 float(20),
price5 float(20),
Bak text,
DisplayOrder int
)
declare
@QasID INT
,@CreateDate Datetime
,@d Datetime
,@Q INT
declare @SortID int
--只选择查看一个地区
if(@flag=1)
begin
select @QasID = 0, @CreateDate = '1900-1-1'
select * Into #list from news_Price as a where
exists(select * from news_QasUnit b where ProductID=@ProductID and a.QasID=b.QasID)
AND DATEDIFF(month, CreateDate, Getdate()) <= 1
order by CreateDate DESC
UPDATE #list SET @Q = @QasID, @QasID = QasID, @d = @CreateDate, @CreateDate = CreateDate, CreateDate = CASE WHEN @QasID = @Q AND DATEDIFF(d, @d, @CreateDate) = 0 THEN '1900-1-1' ELSE CreateDate END
insert into #temp1 select QasUnit=(select top 1 c.QasUnit from news_QasUnit as c where c.QasID=#list.QasID )
,avgMonth = avg(CASE WHEN DATEDIFF(month, CreateDate, Getdate()) = 1 THEN Price ELSE NULL END)
,avgWeek = avg(CASE WHEN DATEDIFF(week,CreateDate,Getdate()) = 1 THEN Price ELSE NULL END)
,yesprice5=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list.QasID and DateDiff(day,CreateDate,GetDate())=4 order by CreateDate desc)
,yesprice4=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list.QasID and DateDiff(day,CreateDate,GetDate())=3 order by CreateDate desc)
,yesprice3=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list.QasID and DateDiff(day,CreateDate,GetDate())=2 order by CreateDate desc)
,yesprice2=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list.QasID and DateDiff(day,CreateDate,GetDate())=1 order by CreateDate desc)
,yesprice1=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list.QasID and DateDiff(day,CreateDate,GetDate())=0 order by CreateDate desc)
,bak=(select top 1 bak from news_Price where news_Price.QasID=#list.QasID and DATEDIFF(d,CreateDate,Getdate()) = 0 order by CreateDate desc)
,DisplayOrder=(SELECT top 1 DisplayOrder from news_QasUnit where news_QasUnit.QasID=#list.QasID )
from #list where CreateDate > '2000-1-1' group by #list.QasID order by DisplayOrder
drop table #list
end
--选择查看全国报价
else
begin
Declare Cur Cursor For
select sortid from dbo.news_sort where parentid = @ProductID
Open Cur
Fetch Cur Into @SortID
While @@FETCH_STATUS=0
BEGIN
select @QasID = 0, @CreateDate = '1900-1-1'
select * Into #list2 from news_Price as a where
exists(select * from news_QasUnit b where ProductID=@SortID and a.QasID=b.QasID)
AND DATEDIFF(month, CreateDate, Getdate()) <= 1
order by CreateDate DESC
UPDATE #list2 SET @Q = @QasID, @QasID = QasID, @d = @CreateDate, @CreateDate = CreateDate, CreateDate = CASE WHEN @QasID = @Q AND DATEDIFF(d, @d, @CreateDate) = 0 THEN '1900-1-1' ELSE CreateDate END
insert into #temp1 select QasUnit=(select top 1 c.QasUnit from news_QasUnit as c where c.QasID=#list2.QasID )
,avgMonth = avg(CASE WHEN DATEDIFF(month, CreateDate, Getdate()) = 1 THEN Price ELSE NULL END)
,avgWeek = avg(CASE WHEN DATEDIFF(week,CreateDate,Getdate()) = 1 THEN Price ELSE NULL END)
,yesprice5=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list2.QasID and DateDiff(day,CreateDate,GetDate())=4 order by CreateDate desc)
,yesprice4=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list2.QasID and DateDiff(day,CreateDate,GetDate())=3 order by CreateDate desc)
,yesprice3=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list2.QasID and DateDiff(day,CreateDate,GetDate())=2 order by CreateDate desc)
,yesprice2=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list2.QasID and DateDiff(day,CreateDate,GetDate())=1 order by CreateDate desc)
,yesprice1=(select top 1 price from News_Price where CenterFlag=2 and News_Price.QasID=#list2.QasID and DateDiff(day,CreateDate,GetDate())=0 order by CreateDate desc)
,bak=(select top 1 bak from news_Price where news_Price.QasID=#list2.QasID and DATEDIFF(d,CreateDate,Getdate()) = 0 order by CreateDate desc)
,DisplayOrder=(SELECT top 1 DisplayOrder from news_QasUnit where news_QasUnit.QasID=#list2.QasID )
from #list2 where CreateDate > '2000-1-1' group by #list2.QasID order by DisplayOrder
Fetch Cur Into @SortID
drop table #list2
end
Close Cur
Deallocate cur