日期:2014-05-17  浏览次数:20387 次

版主求助啊
回答下我的问题:标题为: 三表联合查询,按产地排序,并要求每个产地中有一个最低价格的sql语句的帖子

------解决方案--------------------
top 5不清楚是要干什么,就先不管了,全当你select语句对的,直接用了。你试试结果对不对吧。真心看着眼晕。

SQL code

WITH A AS
(SELECT TOP 5 tc.Id,  
  tc.CottonLength,  
  tc.CottonMavalue,  
  tc.CottonStrength,  
  tc.CottonQuote,  
  tc.CottonType,
  tc.CottonSailing,  
  CASE tc.CottonType WHEN '1' THEN '港口寄售' WHEN '0' THEN '远期交易' ELSE '' END AS TypeName,
  tc.CottonCount,  
  tc.CottonRemark,
  tsc2.[SysName] AS CottonArea,
  tsc3.[SysName] AS CottonClass,
  tsc4.[SysName] AS CottonYear,
  tsc5.[SysName] AS CottonWarehouse
  FROM dbo.T_Cotton tc
  LEFT JOIN dbo.T_SysCode tsc2 ON tc.CottonArea =tsc2.Id
  LEFT JOIN dbo.T_SysCode tsc3 ON tc.CottonClass=tsc3.Id
  LEFT JOIN dbo.T_SysCode tsc4 ON tc.CottonYear = tsc4.Id
  LEFT JOIN dbo.T_SysCode tsc5 ON tc.CottonWarehouse = tsc5.Id
  LEFT JOIN dbo.T_Sale ts ON ts.CottonId=tc.Id
  )
  
  SELECT * 
  FROM A AS T
  WHERE (select count(*) from A where CottonArea =t.CottonArea  and CottonQuote>t.CottonQuote )<1