使用SQL Developer为每个唯一字段值选择单行

时间:2013-07-15 14:55:15

标签: oracle11g oracle-sqldeveloper

我有数千行数据,其中一段看起来像:

+-------------+-----------+-------+  
| Customer ID |  Company  | Sales |  
+-------------+-----------+-------+  
|    45678293 | Sears     |    45 |  
|    01928573 | Walmart   |     6 |  
|    29385068 | Fortinoes |     2 |  
|    49582015 | Walmart   |     1 |  
|    49582015 | Joe's     |     1 |  
|    19285740 | Target    |    56 |  
|    39506783 | Target    |     4 |  
|    39506783 | H&M       |     4 |  
+-------------+-----------+-------+  

在每种情况下,客户ID不止一次出现,“Sales”中的值也相同,但“Company”中的值不同(整个表中都是如此)。我需要'客户ID中的每个值只显示一次,所以每个客户ID需要一行。

换句话说,我希望上表看起来像:

+-------------+-----------+-------+  
| Customer ID |  Company  | Sales |  
+-------------+-----------+-------+  
|    45678293 | Sears     |    45 |  
|    01928573 | Walmart   |     6 |  
|    29385068 | Fortinoes |     2 |  
|    49582015 | Walmart   |     1 |  
|    19285740 | Target    |    56 |  
|    39506783 | Target    |     4 |    
+-------------+-----------+-------+

如果有人知道如何做到这一点,我会非常感谢一些帮助。 谢谢!

1 个答案:

答案 0 :(得分:0)

如果你把你的sql生成那些数据,那将会有所帮助。

但它可能会像;

SELECT customer_id, Max(Company) as company, Count(sales.*) From Customers <your joins and where clause> GROUP BY customer_id

假设;有很多公司,并将最多的出现和销售数据选择在不同的表格中。

希望这有帮助。

相关问题