本帖最后由 lianyi 于 2021-7-30 12:46 编辑
根据你的要求,可以试试用ROW_NUMBER PARTITION BY来排列和筛选数据,由于没有sqlserver环境,只有mysql环境,所以只给出mysql的语句,sqlserver的大同小异
ROW_NUMBER
[SQL] 纯文本查看 复制代码 SELECT *
FROM (
SELECT *, ROW_NUMBER() over(PARTITION BY `Fnumber` ORDER BY IF(`Fname` = '厂家A', 0, 1) ASC, `date` DESC) AS `num`
FROM `table`
) AS t
WHERE t.`num` = 1
GROUP_CONCAT
[SQL] 纯文本查看 复制代码 SELECT `Fnumber`, SUBSTRING_INDEX(GROUP_CONCAT(`Fprice` ORDER BY IF(`Fname` = '厂家A', 0, 1) ASC, `date` DESC), ',', 1) AS `Fprice`
FROM `table`
GROUP BY `Fnumber` |