【急】SQL达人乱入

kangnan008 2009-11-11 07:35:27
小弟遇到一个问题。。。

现在有2张表。。。

表A
ProductID a_price userid productName p_price
8577 10.00 422 StampP3-20091102 10.00
8577 12.00 422 StampP3-20091102 10.00
8577 14.00 422 StampP3-20091102 10.00
8577 16.00 422 StampP3-20091102 10.00
8576 1.00 422 StampP2-20091102 1.00
8575 5.00 422 StampP1-20091102 1.00
8575 3.00 422 StampP1-20091102 1.00
8575 1.00 422 StampP1-20091102 1.00

表B
productID p_price userid
8577 10.00 422
8577 12.00 422
8577 14.00 422
8577 16.00 422
8576 1.00 422
8575 5.00 422
8575 3.00 422
8575 1.00 422

想得到的

ProductID a_price userid productName p_price maxpirce
8577 10.00 422 StampP3-20091102 10.00 16.00
8577 12.00 422 StampP3-20091102 10.00 16.00
8577 14.00 422 StampP3-20091102 10.00 16.00
8577 16.00 422 StampP3-20091102 10.00 16.00
8576 1.00 422 StampP2-20091102 1.00 1.00
8575 5.00 422 StampP1-20091102 1.00 5.00
8575 3.00 422 StampP1-20091102 1.00 5.00
8575 1.00 422 StampP1-20091102 1.00 5.00

哪位达人能帮忙解决下。。。
...全文
83 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangyn77 2009-11-12
  • 打赏
  • 举报
回复
照五楼、7楼执行
dawugui 2009-11-11
  • 打赏
  • 举报
回复
[Quote=引用楼主 kangnan008 的回复:]
小弟遇到一个问题。。。

现在有2张表。。。

表A
ProductID a_price userid  productName        p_price
8577 10.00 422 StampP3-20091102 10.00
8577 12.00 422 StampP3-20091102 10.00
8577 14.00 422 StampP3-20091102 10.00
8577 16.00 422 StampP3-20091102 10.00
8576 1.00 422 StampP2-20091102 1.00
8575 5.00 422 StampP1-20091102 1.00
8575 3.00 422 StampP1-20091102 1.00
8575 1.00 422 StampP1-20091102 1.00

表B
productID p_price  userid
8577 10.00    422
8577 12.00    422
8577 14.00    422 
8577 16.00    422
8576 1.00      422
8575 5.00      422
8575 3.00      422
8575 1.00      422

想得到的

ProductID a_price userid  productName        p_price    maxpirce
8577 10.00 422 StampP3-20091102 10.00       16.00
8577 12.00 422 StampP3-20091102 10.00       16.00
8577 14.00 422 StampP3-20091102 10.00       16.00
8577 16.00 422 StampP3-20091102 10.00       16.00
8576 1.00 422 StampP2-20091102 1.00       1.00
8575 5.00 422 StampP1-20091102 1.00     5.00
8575 3.00 422 StampP1-20091102 1.00       5.00
8575 1.00 422 StampP1-20091102 1.00       5.00

哪位达人能帮忙解决下。。。
[/Quote]
create table [A]([ProductID] int,[a_price] numeric(4,2),[userid] int,[productName] varchar(16),[p_price] numeric(4,2))
insert [A]
select 8577,10.00,422,'StampP3-20091102',10.00 union all
select 8577,12.00,422,'StampP3-20091102',10.00 union all
select 8577,14.00,422,'StampP3-20091102',10.00 union all
select 8577,16.00,422,'StampP3-20091102',10.00 union all
select 8576,1.00,422,'StampP2-20091102',1.00 union all
select 8575,5.00,422,'StampP1-20091102',1.00 union all
select 8575,3.00,422,'StampP1-20091102',1.00 union all
select 8575,1.00,422,'StampP1-20091102',1.00
create table [B]([productID] int,[p_price] numeric(4,2),[userid] int)
insert [B]
select 8577,10.00,422 union all
select 8577,12.00,422 union all
select 8577,14.00,422 union all
select 8577,16.00,422 union all
select 8576,1.00,422 union all
select 8575,5.00,422 union all
select 8575,3.00,422 union all
select 8575,1.00,422

select a.* , maxpirce = (select max(p_price) from b where ProductID = a.ProductID) from a

drop table a , b

/*
ProductID a_price userid productName p_price maxpirce
----------- ------- ----------- ---------------- ------- --------
8577 10.00 422 StampP3-20091102 10.00 16.00
8577 12.00 422 StampP3-20091102 10.00 16.00
8577 14.00 422 StampP3-20091102 10.00 16.00
8577 16.00 422 StampP3-20091102 10.00 16.00
8576 1.00 422 StampP2-20091102 1.00 1.00
8575 5.00 422 StampP1-20091102 1.00 5.00
8575 3.00 422 StampP1-20091102 1.00 5.00
8575 1.00 422 StampP1-20091102 1.00 5.00

(所影响的行数为 8 行)
*/
SQL77 2009-11-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hery2002 的回复:]
SQL codeselect A.*, (selecttop1[p_price]from Bwhere b.[productID]= A.[productID]and b.[userid]= a.[userid]orderby b.[p_price]desc)from A/*
ProductID a_price userid ?-
[/Quote]
看来我还弄复杂了,晕,学习
hery2002 2009-11-11
  • 打赏
  • 举报
回复

select A.*, (select top 1 [p_price] from B where b.[productID] = A.[productID] and b.[userid] = a. [userid] order by b.[p_price] desc)
from A
/*
ProductID a_price userid productName p_price
----------- --------------------------------------- ----------- ---------------- --------------------------------------- ---------------------------------------
8577 10.00 422 StampP3-20091102 10.00 16.00
8577 12.00 422 StampP3-20091102 10.00 16.00
8577 14.00 422 StampP3-20091102 10.00 16.00
8577 16.00 422 StampP3-20091102 10.00 16.00
8576 1.00 422 StampP2-20091102 1.00 1.00
8575 5.00 422 StampP1-20091102 1.00 5.00
8575 3.00 422 StampP1-20091102 1.00 5.00
8575 1.00 422 StampP1-20091102 1.00 5.00

*/
bancxc 2009-11-11
  • 打赏
  • 举报
回复
if object_id('[A]') is not null drop table [A]
go
create table [A]([ProductID] int,[a_price] numeric(4,2),[userid] int,[productName] varchar(16),[p_price] numeric(4,2))
insert [A]
select 8577,10.00,422,'StampP3-20091102',10.00 union all
select 8577,12.00,422,'StampP3-20091102',10.00 union all
select 8577,14.00,422,'StampP3-20091102',10.00 union all
select 8577,16.00,422,'StampP3-20091102',10.00 union all
select 8576,1.00,422,'StampP2-20091102',1.00 union all
select 8575,5.00,422,'StampP1-20091102',1.00 union all
select 8575,3.00,422,'StampP1-20091102',1.00 union all
select 8575,1.00,422,'StampP1-20091102',1.00
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([productID] int,[p_price] numeric(4,2),[userid] int)
insert [B]
select 8577,10.00,422 union all
select 8577,12.00,422 union all
select 8577,14.00,422 union all
select 8577,16.00,422 union all
select 8576,1.00,422 union all
select 8575,5.00,422 union all
select 8575,3.00,422 union all
select 8575,1.00,422

Select A.*,maxprice
From A
inner join
(Select productid,maxprice=MAX(p_price) from b group by productid
) B on A.productid=B.productid

/*8575 5.00 422 StampP1-20091102 1.00 5.00
8575 3.00 422 StampP1-20091102 1.00 5.00
8575 1.00 422 StampP1-20091102 1.00 5.00
8576 1.00 422 StampP2-20091102 1.00 1.00
8577 10.00 422 StampP3-20091102 10.00 16.00
8577 12.00 422 StampP3-20091102 10.00 16.00
8577 14.00 422 StampP3-20091102 10.00 16.00
8577 16.00 422 StampP3-20091102 10.00 16.00*/
SQL77 2009-11-11
  • 打赏
  • 举报
回复
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([ProductID] int,[a_price] numeric(4,2),[userid] int,[productName] varchar(16),[p_price] numeric(4,2))
insert [A]
select 8577,10.00,422,'StampP3-20091102',10.00 union all
select 8577,12.00,422,'StampP3-20091102',10.00 union all
select 8577,14.00,422,'StampP3-20091102',10.00 union all
select 8577,16.00,422,'StampP3-20091102',10.00 union all
select 8576,1.00,422,'StampP2-20091102',1.00 union all
select 8575,5.00,422,'StampP1-20091102',1.00 union all
select 8575,3.00,422,'StampP1-20091102',1.00 union all
select 8575,1.00,422,'StampP1-20091102',1.00
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([productID] int,[p_price] numeric(4,2),[userid] int)
insert [B]
select 8577,10.00,422 union all
select 8577,12.00,422 union all
select 8577,14.00,422 union all
select 8577,16.00,422 union all
select 8576,1.00,422 union all
select 8575,5.00,422 union all
select 8575,3.00,422 union all
select 8575,1.00,422

SELECT DISTINCT
A.*,
(SELECT MAX(P_PRICE) FROM B
WHERE productID=T.productID AND userid=T.userid GROUP BY productID,userid)AS MAXPRICE
FROM A,B T WHERE A.ProductID=T.ProductID AND A.userid=T.userid
ORDER BY A.p_price DESC,MAXPRICE


(所影响的行数为 8 行)


(所影响的行数为 8 行)

ProductID a_price userid productName p_price MAXPRICE
----------- ------- ----------- ---------------- ------- --------
8577 10.00 422 StampP3-20091102 10.00 16.00
8577 12.00 422 StampP3-20091102 10.00 16.00
8577 14.00 422 StampP3-20091102 10.00 16.00
8577 16.00 422 StampP3-20091102 10.00 16.00
8576 1.00 422 StampP2-20091102 1.00 1.00
8575 1.00 422 StampP1-20091102 1.00 5.00
8575 3.00 422 StampP1-20091102 1.00 5.00
8575 5.00 422 StampP1-20091102 1.00 5.00

(所影响的行数为 8 行)
--小F-- 2009-11-11
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-11-11 19:41:46
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([ProductID] int,[a_price] numeric(4,2),[userid] int,[productName] varchar(16),[p_price] numeric(4,2))
insert [A]
select 8577,10.00,422,'StampP3-20091102',10.00 union all
select 8577,12.00,422,'StampP3-20091102',10.00 union all
select 8577,14.00,422,'StampP3-20091102',10.00 union all
select 8577,16.00,422,'StampP3-20091102',10.00 union all
select 8576,1.00,422,'StampP2-20091102',1.00 union all
select 8575,5.00,422,'StampP1-20091102',1.00 union all
select 8575,3.00,422,'StampP1-20091102',1.00 union all
select 8575,1.00,422,'StampP1-20091102',1.00
--> 测试数据:[B]
if object_id('[B]') is not null drop table [B]
go
create table [B]([productID] int,[p_price] numeric(4,2),[userid] int)
insert [B]
select 8577,10.00,422 union all
select 8577,12.00,422 union all
select 8577,14.00,422 union all
select 8577,16.00,422 union all
select 8576,1.00,422 union all
select 8575,5.00,422 union all
select 8575,3.00,422 union all
select 8575,1.00,422
--------------开始查询--------------------------
select
a.*,b.[p_price]
from
a
join
(Select productID,max(p_price) as p_price from b group by productID )b
on
a.productID=b.productID
----------------结果----------------------------
/*
(8 行受影响)

(8 行受影响)
ProductID a_price userid productName p_price p_price
----------- --------------------------------------- ----------- ---------------- --------------------------------------- ---------------------------------------
8575 5.00 422 StampP1-20091102 1.00 5.00
8575 3.00 422 StampP1-20091102 1.00 5.00
8575 1.00 422 StampP1-20091102 1.00 5.00
8576 1.00 422 StampP2-20091102 1.00 1.00
8577 10.00 422 StampP3-20091102 10.00 16.00
8577 12.00 422 StampP3-20091102 10.00 16.00
8577 14.00 422 StampP3-20091102 10.00 16.00
8577 16.00 422 StampP3-20091102 10.00 16.00

(8 行受影响)


*/
SQL77 2009-11-11
  • 打赏
  • 举报
回复
SELECT 
A.*,
(SELECT MAX(P_PRICE) FROM B GROUP BY productID,USRID
WHERE productID=T.productID AND USRID=T.USERID)AS MAXPRICE
FROM A,B T WHERE A.ProductID=T.ProductID AND A.USERID=T.USERID
SQL77 2009-11-11
  • 打赏
  • 举报
回复
SELECT 
A.*,
(SELECT MAX(PRICE) FROM B GROUP BY productID,USRID
WHERE productID=T.productID AND USRID=T.USERID)AS MAXPRICE
FROM A,B T WHERE A.ProductID=T.ProductID AND A.USERID=T.USERID
bancxc 2009-11-11
  • 打赏
  • 举报
回复

Select A.*,maxprice
From A
inner join
(Select product_id,maxprice=maxprice(p_price) from b group by product_id
) B on A.product_id=B.product_id
Zoezs 2009-11-11
  • 打赏
  • 举报
回复
没看出来你想要的结果是什么。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧