Sql 比较同一表中的两行
我想比较一个表中两行的值, 该表如下所示。Sql 比较同一表中的两行,sql,Sql,我想比较一个表中两行的值, 该表如下所示。 我想找到-F sku大于正常sku的记录 SKU ASIN Price CT0144 B013VNZNYU 20.99 CT0144-F B013VNZNYU 17.64 试试这个 select t1.SKU,t1.ASIN,t1.Price from table as t1 inner join ( select ASIN, max(price) as price from table gr
我想找到-F sku大于正常sku的记录
SKU ASIN Price
CT0144 B013VNZNYU 20.99
CT0144-F B013VNZNYU 17.64
试试这个
select t1.SKU,t1.ASIN,t1.Price from table as t1 inner join
(
select ASIN, max(price) as price from table group by ASIN
) as t2
on t1.ASIN=t2.ASIN and t1.price=t2.price
where t1.SKU like '%-F'
试着这样,
DECLARE @table TABLE (
SKU VARCHAR(50)
,ASIN VARCHAR(50)
,Price FLOAT
)
INSERT INTO @table
VALUES (
'CT0144'
,'B013VNZNYU'
,'20.99'
)
,(
'CT0144-F'
,'B013VNZNYU'
,'17.64'
)
,(
'CT0144'
,'B013VNZNU'
,'10.99'
)
,(
'CT0144-F'
,'B013VNZNU'
,'18.64'
)
SELECT *
FROM @table
SELECT A.ASIN
,A.FPrice
,B.Normal
FROM (
SELECT ASIN
,MAX(PRICE) AS FPrice
FROM @table t1
WHERE SKU LIKE '%F'
GROUP BY ASIN
) A
INNER JOIN (
SELECT ASIN
,MAX(PRICE) AS Normal
FROM @table t1
WHERE SKU NOT LIKE '%F'
GROUP BY ASIN
) B ON A.ASIN = B.ASIN
WHERE A.FPrice > B.Normal
内部使用不同的别名连接表,神奇的是doneMySql?PlSql?您使用的是哪个数据库?我知道,但我想要两个比较两个sku onw是正常的,另一个是正常的+'-FAre ASIN值与正常值和-F相同?为什么不将-F移到一个新列?像一面旗帜!