greatest-n-per-group

select rows how to query in sql

微笑、不失礼 提交于 2019-12-25 06:36:10
问题 I have a use case like a 3 a 4 a 5 a 6 a 5 b 3 b 5 b 3 how to get the output like a 4 a 5 b 5 b 3 choose the hight number for a and b, but just 2 rows that's the query I wrote now, seems it is not working SELECT id, barcode, actualsku, inventorycount FROM ( SELECT pallet.id AS id, pallet.barcode AS barcode, inventoryunit.sku AS actualsku, Count(inventoryunit.id) AS inventorycount FROM (SELECT * FROM mft.asset WHERE container_type = 'PALLET' AND location_type = 'PRIME' :: mft.location_type AND

Mysql - Need to get latest of table A when referenced from table B

六眼飞鱼酱① 提交于 2019-12-25 04:47:21
问题 I'm building a bug tracker type tool for kicks. I'm having probs with a small prob relating to version control of my data. I have a table 'action' where I store all the data about the action (desription, who entered it, status etc). I also have a action_status table where each time the status is changed (from not asigned, in progress, complete etc) it is logged here.. What I can't seem to do is list the actions with their latest status value. You'll note that the status table has two rows,

How can I take two rows for one Distinct value in postgresql?

独自空忆成欢 提交于 2019-12-25 04:13:38
问题 I want to get two rows of distinct column2 from following table. demo_table id | column1 | column2 1 | 1 | 3 2 | 2 | 3 3 | 3 | 4 4 | 4 | 3 5 | 5 | 4 6 | 6 | 3 When I preform this query select distinct on (column2) * from demo_table it gives me out put as this id | column1 | column2 5 | 5 | 4 6 | 6 | 3 I am looking for query where I can get output as id | column1 | column2 3 | 3 | 4 5 | 5 | 4 6 | 6 | 3 4 | 4 | 3 it should give two row where column2 is distinct. 回答1: Use row_number() for that:

Summing and ordering at once

做~自己de王妃 提交于 2019-12-25 02:55:51
问题 I have a table of orders. There I need to find out which 3 partner_id's have made the largest sum of amount_totals, and sort those 3 from biggest to smallest. testdb=# SELECT amount_total, partner_id FROM sale_order; amount_total | partner_id --------------+------------ 1244.00 | 9 3065.90 | 12 3600.00 | 3 2263.00 | 25 3000.00 | 10 3263.00 | 3 123.00 | 25 5400.00 | 12 (8 rows) Just starting SQL, I find it confusing ... 回答1: Aggregated amounts If you want to list aggregated amounts, it can be

Return rows with the highest value on one column corresponding to a given value in another

我的梦境 提交于 2019-12-25 02:53:48
问题 There's a MySQL table named raw_contacts with the following structure: ID (primary auto-increment, int) PHONE (composite unique with NAME, varchar) NAME (composite unique with PHONE, varchar) FREQUENCY (int) Composite unique key is named PHONENUM I am trying to write a query to return the row with the highest corresponding value in the FREQUENCY column for any given value for PHONE. I did find a related post but the accepted answer doesn't work for my case since I have a composite key of two

Get MAX row for GROUP in MySQL

百般思念 提交于 2019-12-25 02:53:21
问题 I have the following data: +---------+----------+----------+--------+ | id | someId | number | data | +---------+----------+----------+--------+ | 27 | 123 | 1 | abcde1 | | 28 | 123 | 3 | abcde2 | | 29 | 123 | 1 | abcde3 | | 30 | 123 | 5 | abcde4 | | 31 | 124 | 4 | abcde1 | | 32 | 124 | 8 | abcde2 | | 33 | 124 | 1 | abcde3 | | 34 | 124 | 2 | abcde4 | | 35 | 123 | 16 | abcde1 | | 245 | 123 | 3 | abcde2 | | 250 | 125 | 0 | abcde3 | | 251 | 125 | 1 | abcde4 | | 252 | 125 | 7 | abcde1 | | 264 |

postgres query to get first row based on multiple copies of some columns

会有一股神秘感。 提交于 2019-12-25 01:53:42
问题 Suppose I have a table - A B C 1 3 5 1 3 7 1 3 9 2 4 3 2 4 6 2 4 1 here there are multiple copies for the same combination of A and B. for each combination I want back the first entry of it. so the result for this table i want to be- A B C 1 3 5 2 4 3 How can I do this in postgres sql? 回答1: Assuming you can define "first" in terms of a sort on a, b, and c you want DISTINCT ON for this. SELECT DISTINCT ON ("A", "B") "A", "B", "C" FROM Table1 ORDER BY "A", "B", "C"; E.g. http://sqlfiddle.com/#

Select EMP with max SAL from each DEPT

╄→гoц情女王★ 提交于 2019-12-25 00:19:57
问题 I´m having a bad time with a SQL query. I´m using oracle default tables: 'EMP' TABLE http://imageshack.us/photo/my-images/850/sinttuloxps.png/ AND 'DEPT' TABLE http://imageshack.us/photo/my-images/19/assayp.png/ I wan´t to get the employee with the highest salary in each department. I´m using SQLPLUS. Thanks in advance! ALSO CODE: CREATE TABLE dept ( deptno numeric(2,0) NOT NULL, dname character varying(14), loc character varying(13), CONSTRAINT dept_pkey PRIMARY KEY (deptno) ); CREATE TABLE

How do I sum only the 3 best scores from this query?

坚强是说给别人听的谎言 提交于 2019-12-24 20:43:57
问题 I need to sum only the 5 best scores for each player from 11 events. The query below creates a leaderboard which sums all the scores but I'm stuck as to how i can only sum the 5 best scores. I've abbreviated the query to show only 4 events and getting best 3 results to shorten the post, but hope it gets across, what I need. SELECT playerID AS Player, SUM(CASE WHEN championshipleaderboard.eventID = 1 THEN championshipleaderboard.points ELSE 0 END) AS Event1, SUM(CASE WHEN

Find the first key by date field using sql and output also have other fields

偶尔善良 提交于 2019-12-24 19:24:14
问题 I want to query the first occurrence of every name according to the earliest date. The output should have the complete row. Please help me to write the query in sql. Input: Name | ID | payment_date | Pack ------+-------+-----------------+------- A | 11 | 31-Jan | P C | 13 | 31-Jan | Q B | 2 | 31-Jan | R C | 3 | 28-Jan | P D | 23 | 29-Jan | Q B | 11 | 30-Jan | R A | 17 | 25-Jan | P C | 13 | 26-Jan | Q D | 17 | 2-Feb | R B | 23 | 3-Feb | P A | 45 | 4-Feb | Q B | 3 | 5-Feb | R Output: Name | ID