group-by

SQL to get 2 adjacent actions from the flag

百般思念 提交于 2021-02-11 12:50:26
问题 hope you are doing well! I have an dummy data as below. I want to get 2 adjacent actions from the flag by each user. Here's the chart to describe my thought. Here's what I want: How can I implement SQL(I use Google Bigquery)? Hope someone can light me up. Thanks a million! 回答1: You seem to want lag() . I would leave the "action sequence" as two separate columns: select user, prev_action, action, flag from (select t.*, lag(action) over (partition by user order by sequence) as prev_action from

Joining three table then group

自作多情 提交于 2021-02-11 07:36:09
问题 I am going to join three tables then sum one of the column multiplied with an value from another. SELECT t1.column, t2.column, SUM(t1.column * t2.column) FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id JOIN table3 t3 ON t2.id = t3.id GROUP BY t1.column, t2.column; This query does what I want, BUT I do not understand why the GROUP BY works? If I add columns to the select must I also add columns to the group by? 回答1: Do you actually know what you are doing here? SELECT t1.column, t2.column

query data in the order of IN clause

痴心易碎 提交于 2021-02-11 06:12:53
问题 select osmid,ST_X(shape),ST_Y(shape) from osmpoints where (osmpoints.osmid, osmpoints.osmtimestamp) IN (select osmid,MAX(osmtimestamp) from osmPoints GROUP BY osmid Having MAX(osmtimestamp) <= '2019-09-16T01:23:55Z' AND osmid in ('4426786454','1861591896','1861591869','1861591895', '4426786455','2038185115','1861591853','6797739995', '2299605892','6797739994','1861591898','2038185111','4426786454')); when I run this query, I get sorted rows based on osmid column. but my question is how can I

Can MySQL be convinced of functional dependency when HAVING COUNT(*) = 1?

与世无争的帅哥 提交于 2021-02-11 04:59:39
问题 I'm trying to find orders with only one item in a database running on MySQL 5.7.23 on Ubuntu 18.04 LTS. But somehow MySQL can't infer that COUNT(*) = 1 implies a functional dependence. The following 2-table database of orders with order items illustrates the failure: DROP TABLE IF EXISTS t_o, t_oi; CREATE TABLE t_o ( order_id INTEGER UNSIGNED PRIMARY KEY, placed_on DATE NOT NULL, INDEX (placed_on) ); INSERT INTO t_o (order_id, placed_on) VALUES (1, '2018-10-01'), (2, '2018-10-02'); CREATE

Can MySQL be convinced of functional dependency when HAVING COUNT(*) = 1?

可紊 提交于 2021-02-11 04:56:48
问题 I'm trying to find orders with only one item in a database running on MySQL 5.7.23 on Ubuntu 18.04 LTS. But somehow MySQL can't infer that COUNT(*) = 1 implies a functional dependence. The following 2-table database of orders with order items illustrates the failure: DROP TABLE IF EXISTS t_o, t_oi; CREATE TABLE t_o ( order_id INTEGER UNSIGNED PRIMARY KEY, placed_on DATE NOT NULL, INDEX (placed_on) ); INSERT INTO t_o (order_id, placed_on) VALUES (1, '2018-10-01'), (2, '2018-10-02'); CREATE

Can MySQL be convinced of functional dependency when HAVING COUNT(*) = 1?

拥有回忆 提交于 2021-02-11 04:56:21
问题 I'm trying to find orders with only one item in a database running on MySQL 5.7.23 on Ubuntu 18.04 LTS. But somehow MySQL can't infer that COUNT(*) = 1 implies a functional dependence. The following 2-table database of orders with order items illustrates the failure: DROP TABLE IF EXISTS t_o, t_oi; CREATE TABLE t_o ( order_id INTEGER UNSIGNED PRIMARY KEY, placed_on DATE NOT NULL, INDEX (placed_on) ); INSERT INTO t_o (order_id, placed_on) VALUES (1, '2018-10-01'), (2, '2018-10-02'); CREATE

Difference between consecutive dates in pandas groupby [duplicate]

时光总嘲笑我的痴心妄想 提交于 2021-02-10 13:22:09
问题 This question already has an answer here : Pandas find duration between dates where a condition is met? (1 answer) Closed 2 years ago . I have a data-frame as follows: df_raw_dates = pd.DataFrame({"id": [102, 102, 102, 103, 103, 103, 104], "val": [9,2,4,7,6,3,2], "dates": [pd.Timestamp(2002, 1, 1), pd.Timestamp(2002, 3, 3), pd.Timestamp(2003, 4, 4), pd.Timestamp(2003, 8, 9), pd.Timestamp(2005, 2, 3), pd.Timestamp(2005, 2, 8), pd.Timestamp(2005, 2, 3)]}) id val dates 0 102 9 2002-01-01 1 102 2

Pandas GroupBy : How to get top n values based on a column

ぐ巨炮叔叔 提交于 2021-02-10 03:56:34
问题 forgive me if this is a basic question but i am new to pandas. I have a dataframe with with a column A and i would like to get the top n rows based on the count in Column A. For instance the raw data looks like A B C x 12 ere x 34 bfhg z 6 bgn z 8 rty y 567 hmmu,,u x 545 fghfgj x 44 zxcbv Note that this is just a small sample of the data that i am actually working with. So if we look at Column A, value x appears 4 times,y appears 2 times and z appears 1 time. How can i get the top n values

Oracle select mutual sub string

喜你入骨 提交于 2021-02-08 11:19:29
问题 I would like to return new table in ORACLE where all rows that have same the values in 'col' columns group together and the 'description' column will contain only the mutual sub strings when the different characters will replaced by '...' how can I do that? May i get your help please? Basic code to start with: SELECT col,description FROM table group by col; Example 1: col description 1 Today is 1 Today is a good day 1 Today is perfect day 2 Hello world 2 Hello results: col description 1 Today

PHP: Merge multi dimensional arrays, grouping by a certain key

♀尐吖头ヾ 提交于 2021-02-08 10:42:26
问题 I have some arrays like this: array( 'id' => 1, 'title' => 'title1', 'name' => 'name1', 'count' => 2 ) array( 'id' => 1, 'title' => 'title1', 'name' => 'name2', 'count' => 3 ) array( 'id' => 2, 'title' => 'title2', 'name' => 'name1', 'count' => 2 ) I want to merge them so that arrays with same id and title would be merged. The result should be like: array( 'id' => 1, 'title' => 'title1', 'name' => array('name1', 'name2'), 'count' => array(2, 3) ) array( 'id' => 2, 'title' => 'title2', 'name'