繁体   English   中英

在T-SQL中分组

[英]Grouping in T-SQL

您能建议从该表中如何在SQL中进行这种特殊分组吗

id  FromDate    ToDate          UPC         price   IsGroupSpecial  
3   2013-12-27  2013-12-30  6400000087492   315.00  1           
2   2013-12-27  2013-12-31  6400000087492   405.00  0

需要选择所有具有最低价格的商品,但ID不一定是最小值-ID应该从IsGroupSpecial = 0的那一行中获取

我认为您正在寻找这种类型的查询。 如果您只查找价格低于group by子句的所有记录,则没有必要。

select *
from table
where price = (select min(price) from table))

如果我理解正确

select UPC, min(price), x.id
from table t1
cross apply (select id from table t2 where t2.IsGroupSpecial =0 and t1.UPC=t2.UPC) X 
group by UPC, x.id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM