[英]Select Rows with a Distinct First Column in Excel ODBC SQL
Given the table below, how can I select only rows that have a unique animal? 根据下表,如何只选择具有独特动物的行?
animal age location
-------------------------
zebra 4 africa --> return this row
owl 50 america --> return this row
owl 50 u.s.a
panda 9 china --> return this row
My understanding is that the following won't work due to location
having unique rows: 我的理解是,由于location
具有唯一的行,因此以下操作无效:
SELECT DISTINCT animal, age, location FROM table
Short answer - it is impossible, because condition is not strict, how you can determine which row for "owl" should be returned? 简短的回答-这是不可能的,因为条件不严格,如何确定应返回“猫头鹰”的哪一行?
Long answer, as soon as you add some conditions, you can select something close to your question, like this: 长答案,一旦添加了一些条件,您就可以选择接近您的问题的内容,例如:
SELECT animal, MIN(age), MIN(location) FROM table
GROUP BY animal
in this query we're selecting unique animals and minimal values for age and location, note - it doesn't mean that there will be row with such values, like here: 在此查询中,我们正在选择独特的动物以及年龄和位置的最小值,请注意-这并不意味着会有带有此类值的行,例如:
animal1, 2, location1
animal1, 1, location2
this query will select: 该查询将选择:
animal1, 1, location1
Short answer - it is possible with a subquery to a partition table. 简短的回答-可以通过子查询访问分区表。 I find it best to create a View in your MS SQL that defines the partition query and then call that view in your SELECT
statement. 我发现最好在你的MS SQL定义分区查询,然后调用你的这个观点创建视图 SELECT
语句。
The MS SQL View (eg vw_First_Animals) would be: MS SQL 视图 (例如vw_First_Animals)为:
SELECT * FROM (
SELECT *, rn=row_number() OVER (PARTITION BY [animal] ORDER by [location]) FROM [dbo].[tblAnimals]
) x where rn = 1;
This makes your XL query, 这使您的XL查询,
SELECT * FROM vw_First_Animals
If you reshape the ORDER BY
clause to the View , you can change the order that the unique returns are delivered. 如果将ORDER BY
子句重塑为View ,则可以更改唯一返回的传递顺序。
I would be remiss without nodding toward this thread that has helped me with PARTITION table subqueries through its simplistic demonstration. 如果不对这个线程(它通过简单的演示帮助我使用PARTITION表子查询)有所帮助,我将不知所措。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.