如何按某些字段分组并在oracle中显示另一个字段的第一个值?(How to group by some fields and display the first value of another field in oracle?)
这是我在分组后看到的表格:
DATE: TEST: BUILD: NUM: 17-FEB-14 testA BUILDA 100 18-FEB-14 testA BUILDA 20 20-FEB-14 testA BUILDB 120
我按TEST,BUILD AND DATE进行分组。 这里的问题是,测试A在BUILDA上的一些子测试恰好是在2014年1月17日,有些是在2014年1月18日。 这会将与record1和record2相同的记录拆分。
我需要的只是第一次约会的总数:
DATE: TEST: BUILD: NUM: 17-FEB-14 testA BUILDA 120 20-FEB-14 testA BUILDB 120
有什么办法可以实现吗?
查询很长,因此不会发布完整的查询。 但它看起来像:
SELECT r.RN_EXECUTION_DATE, t.TESTSETNAME, r.RN_BUILD_VERSION, r.rn_status, COUNT(r.rn_run_id) AS run_count FROM table t, table r, (.........) tlf WHERE (........) GROUP BY t.TESTSETNAME, r.RN_BUILD_VERSION, r.RN_EXECUTION_DATE, r.rn_status ORDER BY t.TESTSETNAME, r.RN_BUILD_VERSION ;
提前感谢,Manoj
Here is the table which I am looking at after grouping:
DATE: TEST: BUILD: NUM: 17-FEB-14 testA BUILDA 100 18-FEB-14 testA BUILDA 20 20-FEB-14 testA BUILDB 120
I am grouping by TEST, BUILD AND DATE. Problem here is, some of the subtests on testA on BUILDA, happened to be on 17Feb14, where as some are on 18Feb14. This splits up the same record as record1 and record2.
What I need is just the total with the first date:
DATE: TEST: BUILD: NUM: 17-FEB-14 testA BUILDA 120 20-FEB-14 testA BUILDB 120
Is there any way I could achieve this?
The query is a very long one, hence not posting the complete query. But it looks like:
SELECT r.RN_EXECUTION_DATE, t.TESTSETNAME, r.RN_BUILD_VERSION, r.rn_status, COUNT(r.rn_run_id) AS run_count FROM table t, table r, (.........) tlf WHERE (........) GROUP BY t.TESTSETNAME, r.RN_BUILD_VERSION, r.RN_EXECUTION_DATE, r.rn_status ORDER BY t.TESTSETNAME, r.RN_BUILD_VERSION ;
Thanking in advance, Manoj
原文:https://stackoverflow.com/questions/27207191
最满意答案
分解:你想要的第一件事是产品名称:
SELECT Product_Name FROM Products
我们如何获得该类别? 加入吧!
SELECT Products.Product_Name, Categories.Category_Name FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id
越来越接近! 是时候引入订单历史了:
SELECT Products.Product_Name, Categories.Category_Name, Sells.Sells_Date FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id JOIN Sells ON Sells.Sells_Id = Products.Product_Id
现在我们可以引入名称,并过滤到John:
SELECT Products.Product_Name, Categories.Category_Name, Sells.Sells_Date FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id JOIN Sells ON Sells.Sells_Id = Products.Product_Id JOIN Customers ON Sells.Customer_Id = Customers.Customer_Id WHERE Customers.Customer_Name = 'John'
只要在脑海中一步一步。 随时随地构建每个部分的查询。
既然你提到了......
LEFT JOIN
是您处理缺失数据的方式。 它返回“left”表中的行,如果ON
子句不匹配,则将NULL
放在“right”表的字段中。例如,如果John购买了没有类别的Product(
Product.Category_Id
为NULL
),则使用JOIN
将不会返回该购买记录,因为没有与Category_Id
Null
匹配的Category。 在这种情况下,您可以LEFT JOIN Categories
,在结果集中,Category_Name
也将为NULL
。Break it down: THe first thing you want is a Product Name:
SELECT Product_Name FROM Products
How do we get the category? Join it in!
SELECT Products.Product_Name, Categories.Category_Name FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id
Getting close! Time to bring in the order history:
SELECT Products.Product_Name, Categories.Category_Name, Sells.Sells_Date FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id JOIN Sells ON Sells.Sells_Id = Products.Product_Id
Now we can bring in the name, and filter to John:
SELECT Products.Product_Name, Categories.Category_Name, Sells.Sells_Date FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id JOIN Sells ON Sells.Sells_Id = Products.Product_Id JOIN Customers ON Sells.Customer_Id = Customers.Customer_Id WHERE Customers.Customer_Name = 'John'
Just take it one step at a time in your mind. Build the query from each part as you go.
Since you mentioned it...
LEFT JOIN
is how you deal with missing data. It returns rows from the "left" table, and putsNULL
in the fields of the "right" table if there is no match for theON
clause.For example, if John bought a Product with no category (
Product.Category_Id
wasNULL
), using aJOIN
would not return that record of purchase because there was no matching Category withCategory_Id
Null
. In that case, you couldLEFT JOIN Categories
and in the result set,Category_Name
would beNULL
as well.
相关问答
更多-
加 group by a.order_id order by前的查询! :) add group by a.order_id to your query before order by! :)
-
您需要左连接并合并,例如: select table1.product , coalesce(sum(table2.quantity) ,0) from table1 left join table2 on table1.col1key = table2.col2key group by table1.product You need left join and coalesce eg: select table1.product , coalesce(sum(table2.quantit ...
-
我需要做同样的事情。 以下是几个例子。 我还需要创建包含存储过程和函数的表,因此在示例的最后我将展示如何创建包含过程和函数的表。 /*______________________________________ Quick Version only showing tables. No where clause, No table_schema . . . . . . . . . . . . . . . . . . .*/ DROP TABLE IF EXISTS tmp_tables; ...
-
在第二个表上使用条件进行SELECT,显示来自3个表的信息(SELECT with condition on second table, show information from 3 tables)[2021-04-25]
分解:你想要的第一件事是产品名称: SELECT Product_Name FROM Products 我们如何获得该类别? 加入吧! SELECT Products.Product_Name, Categories.Category_Name FROM Products JOIN Categories ON Products.Category_Id = Categories.Category_Id 越来越接近! 是时候引入订单历史了: SELECT Products.Product_Name, Ca ... -
mysql + show tables并为每个表插入一个行号(mysql + show tables and insert a row number for each table)[2022-07-01]
您可以使用information_schema.tables : select (@rn := @rn + 1) as id, table_name from information_schema.tables cross join (select @rn := 0) params where table_name like '%xyz%'; You could use information_schema.tables: select (@rn := @rn + 1) as id, table ... -
是的,您正在使用此函数taglib尝试从JSTL调用Java函数 另请参阅http://www.tutorialspoint.com/jsp/jstl_function_endswith.htm Yes, you can not call Java functions from JSTL as you are doing try using this functions taglib Also have a look at http://www.tutorialspoint.com/jsp/jstl_fun ...
-
即使在5.7中,有关表的信息也分散在磁盘上的文件中。 读取200K文件需要花费大量时间。 这就是200K表不是一个好设计的原因之一。 其他原因与缓存有关 - 这方面存在实际限制。 由于缓存,您将看到I_S查询的时序变化。 建议:重新考虑您的架构设计。 8.0在InnoDB表中存储所有信息,因此速度会非常快。 Even in 5.7, the information about the tables is scattered in files on disk. Reading 200K files takes ...
-
最简单的方法是从SELECT删除类别: SELECT DISTINCT l.* 另一种方法是保留单例的类别名称,并为倍数显示'Multiple' : SELECT l.company, l.LID, . . ., (CASE WHEN MIN(c.CategoryName) = MAX(c.CategoryName) THEN MIN(c.CategoryName) ELSE 'Multiple' END) as CategoryName FROM ...
-
根据条件连接两个表以及第二个表中的记录数(join two tables along with count of records from second table based on condition)[2023-07-24]
使用LEFT JOIN而不是INNER JOIN : SELECT c.id, c.name, c.unit, COUNT(p.category_id) as active_count FROM `categories` c LEFT JOIN `products` p ON c.id = p.category_id AND p.is_active = 1 GROUP BY c.id; 将谓词p.is_active = 1从WHERE子句移动到ON也很重要,这样查询就会返回所有 catego ... -
尝试这个, SELECT c.* FROM categories a INNER JOIN products_to_categories b ON a.category_id = b.category_id INNER JOIN products c ON b.product_id = c.product_id WHERE a.parent_id = 1 这将显示parent_id = 1的products表中的所有 ...