34,590
社区成员
发帖
与我相关
我的任务
分享
'85003061', '4', 999999.0800,1, 1000.0000, 4999.0000 union all select
'85003061', '4', 17.4900,2, 5000.0000, 9999.0000 union all select
'85003061', '4', 16.1400,3, 10000.0000, 99999.0000 union all select
'85003061', '4', 15.8900,4, 100000.0000, 999999.0000 union all select
/*85003061 4 999999080.00000000 15889984.11000000
/*85003061 4 999999080.000000 1613983.860000
select a.quoteno,a.line_no,min(minqty*minp) as MinQuoteAmount,max(maxqty*maxp) as MaxQuoteAmount
from tabquoteprice a
inner join
(select quoteno,line_no,price as minp from tabquoteprice t
where not exists(select 1 from tabquoteprice where t.quoteno=quoteno and t.line_no=line_no and t.minqty<minqty)) b
on a.quoteno=b.quoteno
and a.line_no=b.line_no
inner join
(select quoteno,line_no,price as maxp from tabquoteprice t
where not exists(select 1 from tabquoteprice where t.quoteno=quoteno and t.line_no=line_no and t.maxqty>maxqty)) c
on a.quoteno=c.quoteno
and a.line_no=c.line_no
group by a.quoteno,a.line_no
order by a.quoteno,a.line_no
/*
quoteno line_no MinQuoteAmount MaxQuoteAmount
85003061 1 170.00000000 999998380000.62000000
85003061 2 2610.00000000 999998680000.32000000
85003061 4 15890.00000000 999998080000.92000000
85003061 5 185.00000000 999998420000.58000000
85003061 6 334.00000000 999998600000.40000000
*/
散分贴绝对! 你说price 这个你按照这下面2种方法自己 换就是咯
这个是我最先开始写的1.
select distinct t1.quoteno ,t1.line_no,(select min(minqty*price) from [tabquoteprice] where quoteno= t1.quoteno and line_no=t1.line_no) as MinQuoteAmount
,(select max(maxqty*price) from [tabquoteprice] where quoteno= t1.quoteno and line_no=t1.line_no)as MaxQuoteAmount
from tabquoteprice t1
这个李斌写的
2.select
t.quoteno,t.line_no,
(select top 1 minqty*price from tabquoteprice where quoteno=t.quoteno and line_no=t.line_no order by minqty ),
(select top 1 maxqty*price from tabquoteprice where quoteno=t.quoteno and line_no=t.line_no order by maxqty desc)
from
tabquoteprice t
group by
t.quoteno,t.line_no
类似你自己改吧 就按照这种方法你不管怎么要都可以取出来 如果你想效率高一点就要研究了
select quoteno,line_no
,MinQuoteAmount=min([minqty])*(select top 1 [price] from [tabquoteprice] where quoteno='85003061' and line_no=a.line_no order by [minqty])
,MaxQuoteAmount= max([maxqty])*(select top 1 [price] from [tabquoteprice] where quoteno='85003061' and line_no=a.line_no order by [maxqty] desc)
from [tabquoteprice] a
where quoteno='85003061'
group by quoteno,line_no
/*
quoteno line_no MinQuoteAmount MaxQuoteAmount
----------- ----------- --------------------------------------- ---------------------------------------
85003061 1 99999938.00000000 1699998.30000000
85003061 2 499999840.00000000 5219994.78000000
85003061 4 999999080.00000000 15889984.11000000
85003061 5 99999942.00000000 1849998.15000000
85003061 6 19999992.00000000 16699983.30000000
(5 row(s) affected)
*/
if object_id('[tabquoteprice]')is not null drop table [tabquoteprice]
go
CREATE TABLE [dbo].[tabquoteprice](
[quoteno] [int] NULL,
[line_no] [int] NULL,
[price] [decimal](13, 4) NULL,
[line_seq] [int] NULL,
[minqty] [decimal](13, 4) NULL,
[maxqty] [decimal](13, 4) NULL,
) ON [PRIMARY]
insert into [tabquoteprice] select
'85003061', '1', 2.3800, 1, 100.0000, 999.0000 union all select
'85003061', '1', 1.9300, 2, 1000.0000, 9999.0000 union all select
'85003061', '1', 1.7000, 3, 10000.0000, 999999.0000 union all select
'85003061', '2', 7.6800, 1, 500.0000, 999.0000 union all select
'85003061', '2', 7.0900, 2, 1000.0000, 9999.0000 union all select
'85003061', '2', 5.2200, 3, 10000.0000, 999999.0000 union all select
'85003061', '4', 19.0800,1, 1000.0000, 4999.0000 union all select
'85003061', '4', 17.4900,2, 5000.0000, 9999.0000 union all select
'85003061', '4', 16.1400,3, 10000.0000, 99999.0000 union all select
'85003061', '4', 15.8900,4, 100000.0000, 999999.0000 union all select
'85003061', '5', 2.4200, 1, 100.0000, 999.0000 union all select
'85003061', '5', 2.1400, 2, 1000.0000, 9999.0000 union all select
'85003061', '5', 1.8500, 3, 10000.0000, 999999.0000 union all select
'85003061', '6', 21.6000,1, 20.0000, 49.0000 union all select
'85003061', '6', 18.0000,2, 50.0000, 99.0000 union all select
'85003061', '6', 16.7000,3, 100.0000, 999999.0000
select quoteno , line_no , sum(MinQuoteAmount)MinQuoteAmount, sum(MaxQuoteAmount)MaxQuoteAmount from (
select quoteno , line_no , MinQuoteAmount = minqty*price,0 MaxQuoteAmount from (
select * from [tabquoteprice] t where not exists(select 1 from [tabquoteprice] where [line_no]=t.[line_no] and [minqty]<t.[minqty] ))s
union all
select quoteno , line_no , 0, MaxQuoteAmount = maxqty*price from (
select * from [tabquoteprice] t where not exists(select 1 from [tabquoteprice] where [line_no]=t.[line_no] and [minqty]>t.[minqty] ))k)g
group by quoteno , line_no
/*quoteno line_no MinQuoteAmount MaxQuoteAmount
----------- ----------- ---------------------------------------- ----------------------------------------
85003061 1 238.00000000 1699998.30000000
85003061 2 3840.00000000 5219994.78000000
85003061 4 19080.00000000 15889984.11000000
85003061 5 242.00000000 1849998.15000000
85003061 6 432.00000000 16699983.30000000
(影響 5 個資料列)
*/
select quoteno,line_no,min(MinQuoteAmount) as MinQuoteAmount,max(MaxQuoteAmount)as MaxQuoteAmount
from (select quoteno,line_no,MinQuoteAmount =[minqty]*[price],MaxQuoteAmount =maxqty*price
from tabquoteprice
) a
group by quoteno,line_no
85003061 1 238.00000000 1699998.30000000
85003061 2 3840.00000000 5219994.78000000
85003061 4 19080.00000000 15889984.11000000
85003061 5 242.00000000 1849998.15000000
85003061 6 432.00000000 16699983.30000000
--建立测试环境
CREATE TABLE [dbo].[tabquoteprice](
[quoteno] [int] NULL,
[line_no] [int] NULL,
[price] [decimal](13, 4) NULL,
[line_seq] [int] NULL,
[minqty] [decimal](13, 4) NULL,
[maxqty] [decimal](13, 4) NULL,
) ON [PRIMARY]
insert into [tabquoteprice]
select
'85003061', '1', 2.3800, 1, 100.0000, 999.0000 union all select
'85003061', '1', 1.9300, 2, 1000.0000, 9999.0000 union all select
'85003061', '1', 1.7000, 3, 10000.0000, 999999.0000 union all select
'85003061', '2', 7.6800, 1, 500.0000, 999.0000 union all select
'85003061', '2', 7.0900, 2, 1000.0000, 9999.0000 union all select
'85003061', '2', 5.2200, 3, 10000.0000, 999999.0000 union all select
'85003061', '4', 19.0800,1, 1000.0000, 4999.0000 union all select
'85003061', '4', 17.4900,2, 5000.0000, 9999.0000 union all select
'85003061', '4', 16.1400,3, 10000.0000, 99999.0000 union all select
'85003061', '4', 15.8900,4, 100000.0000, 999999.0000 union all select
'85003061', '5', 2.4200, 1, 100.0000, 999.0000 union all select
'85003061', '5', 2.1400, 2, 1000.0000, 9999.0000 union all select
'85003061', '5', 1.8500, 3, 10000.0000, 999999.0000 union all select
'85003061', '6', 21.6000,1, 20.0000, 49.0000 union all select
'85003061', '6', 18.0000,2, 50.0000, 99.0000 union all select
'85003061', '6', 16.7000,3, 100.0000, 999999.0000
select
t.quoteno,t.line_no,
(select top 1 minqty*price from tabquoteprice where quoteno=t.quoteno and line_no=t.line_no order by minqty ),
(select top 1 maxqty*price from tabquoteprice where quoteno=t.quoteno and line_no=t.line_no order by maxqty desc)
from
tabquoteprice t
group by
t.quoteno,t.line_no
drop table tabquoteprice
--try it
SQL> CREATE TABLE tabquoteprice(
2 quoteno int primary key,
3 line_no int ,
4 price decimal (13, 4),
5 line_seq int,
6 minqty decimal(13, 4),
7 maxqty decimal(13, 4)
8 )
9 /
表已创建。
SQL> select tt.quoteno,tt.line_no,sum(tt.minquoteamount),sum(tt.maxquoteamount)
from
2 (select quoteno,line_no,minQty*price MinQuoteAmount,maxqty*price MaxQuoteAm
ount from tabquoteprice) tt
3 group by tt.quoteno,tt.line_no order by tt.line_no;
QUOTENO LINE_NO SUM(TT.MINQUOTEAMOUNT) SUM(TT.MAXQUOTEAMOUNT)
---------- ---------- ---------------------- ----------------------
85003061 1 19168 1721673.99
85003061 2 63130 5298560.01
85003061 4 1856930 17774231.4
85003061 5 20882 1873813.59
85003061 6 3002 16702823.7
SQL>
select quoteno,line_no,MinQuoteAmount=min(MinQuoteAmount),MaxQuoteAmount=max(MaxQuoteAmount) from
(select quoteno,line_no,MinQuoteAmount=minqty*price,MaxQuoteAmount=maxqty*price
from tabquoteprice) d
group by d.quoteno,d.line_no
/*
quoteno line_no MinQuoteAmount MaxQuoteAmount
----------- ----------- --------------------------------------- ---------------------------------------
85003061 1 238.00000000 1699998.30000000
85003061 2 3840.00000000 5219994.78000000
85003061 4 19080.00000000 15889984.11000000
85003061 5 242.00000000 1849998.15000000
85003061 6 432.00000000 16699983.30000000
(5 行受影响)
*/
CREATE TABLE [dbo].[tabquoteprice](
[quoteno] [int] NULL,
[line_no] [int] NULL,
[price] [decimal](13, 4) NULL,
[line_seq] [int] NULL,
[minqty] [decimal](13, 4) NULL,
[maxqty] [decimal](13, 4) NULL,
) ON [PRIMARY]
insert into [tabquoteprice]
select...
go
SELECT a.quoteno,a.line_no,a.price * mmi,c.price * mmx
FROM tabquoteprice a
INNER JOIN
(SELECT quoteno,line_no,MIN(minqty) mmi,MAX(maxqty)mmx FROM tabquoteprice GROUP BY quoteno,line_no) b
ON a.quoteno = b.quoteno AND a.line_no=b.line_no AND a.minqty = mmi
INNER JOIN tabquoteprice c
ON c.quoteno = b.quoteno AND c.line_no=b.line_no AND c.maxqty = mmx
/*
85003061 1 238.00000000 1699998.30000000
85003061 2 3840.00000000 5219994.78000000
85003061 4 19080.00000000 15889984.11000000
85003061 5 242.00000000 1849998.15000000
85003061 6 432.00000000 16699983.30000000
*/
GO
DROP TABLE tabquoteprice
GO
select a.quoteno,a.line_no,min(minqty*minp) as MinQuoteAmount,max(maxqty*maxp) as MaxQuoteAmount
from tabquoteprice a inner join (
select quoteno,line_no,min([price]) minp ,max([price]) maxp from tabquoteprice group by quoteno,line_no) b
on a.quoteno=b.quoteno
and a.line_no=b.line_no
group by a.quoteno,a.line_no
order by a.quoteno,a.line_no
/*
quoteno line_no MinQuoteAmount MaxQuoteAmount
85003061 1 170.00000000 2379997.62000000
85003061 2 2610.00000000 7679992.32000000
85003061 4 15890.00000000 19079980.92000000
85003061 5 185.00000000 2419997.58000000
85003061 6 334.00000000 21599978.40000000
*/
select distinct t1.quoteno ,t1.line_no,(select min(minqty*price) from [tabquoteprice] where quoteno= t1.quoteno and line_no=t1.line_no) as MinQuoteAmount
,(select max(maxqty*price) from [tabquoteprice] where quoteno= t1.quoteno and line_no=t1.line_no)as MaxQuoteAmount
from tabquoteprice t1
quoteno line_no MinQuoteAmount MaxQuoteAmount
85003061 1 238.000000 1699998.300000
85003061 2 3840.000000 5219994.780000
85003061 4 19080.000000 1613983.860000
85003061 5 242.000000 1849998.150000
85003061 6 432.000000 16699983.300000
这个对