编辑以前的答案基于额外信息
已经为您解决在MySQL和SQL Server,如果你需要它
的MySQL实例(使用用户变量/子查询)
CREATE TABLE SomeTable (company VARCHAR(20), x INT, y INT, price FLOAT, numOfEmploy INT);
INSERT INTO SomeTable
(company, x, y, price, numOfEmploy)
VALUES
('one', 1, 5, 1.3, 15),
('one', 1, 6, 1.4, 15),
('two', 1, 7, 1.5, 16),
('three', 1, 8, 1.6, 17);
SET @count = NULL, @value = NULL;
SELECT company, x, y, price, numOfEmploy FROM (
SELECT
company, x, y, price, numOfEmploy,
@count := IF(@value = company, @count + 1, 1) AS rc,
@value := company
FROM SomeTable
) AS grouped_companies WHERE rc = 1
SQL Server的实施例(使用CTE)
--Create the table
declare @sometable table (company varchar(10), x int, y int, price float, numOfEmploy int)
--insert the data
insert into @sometable values ('one', 1, 5, 1.3, 15)
insert into @sometable values ('one', 2, 6, 1.4, 15)
insert into @sometable values ('two', 3, 7, 1.5, 16)
insert into @sometable values ('three', 4, 8, 1.6, 17)
--WITH Common Table Expression
;WITH grouped_companies AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY company
ORDER BY company) AS rc
FROM @sometable)
SELECT gc.company, gc.x, gc.y, gc.price, gc.numOfEmploy
FROM grouped_companies gc
WHERE gc.rc = 1
你看了GROUP BY – RiggsFolly
@RiggsFolly其实我不知道它是如何与群组合作的 –
然后[这里是你需要开始你的研究的地方](https://dev.mysql.com/doc /refman/5.7/en/group-by-handling.html) – RiggsFolly