greatest-n-per-group

how to get second highest salary department wise without using analytical functions?

▼魔方 西西 提交于 2019-12-03 22:22:39
问题 Suppose we have 3 employees in each department.we have total 3 departments . Below is the sample source table Emp deptno salary A 10 1000 B 10 2000 C 10 3000 D 20 7000 E 20 9000 F 20 8000 G 30 17000 H 30 15000 I 30 30000 Output B 10 2000 F 20 8000 G 30 17000 With using analytic function dense_rank we can achive the second highest salary dept wise. Can we achieve this without using ANY analytic function ??? Is Max() is also analytic function ?? 回答1: It is a pain, but you can do it. The

SQL command for finding the second highest salary

假如想象 提交于 2019-12-03 21:52:55
HI, Can u tell me the syntax of the SQL command which gives as output the second highest salary from a range of salaries stored in the employee table. A description of the SQL commnd will be welcomed... Please help!!! select min(salary) from (select top 2 salary from SalariesTable order by salary desc) as ax This should work: select * from ( select t.*, dense_rank() over (order by salary desc) rnk from employee t ) a where rnk = 2; This returns the second highest salary. dense_rank() over is a window function, and it gives you the rank of a specific row within the specified set. It is standard

sql select earliest date for multiple rows

雨燕双飞 提交于 2019-12-03 20:10:58
I have a database that looks like the following; circuit_uid | customer_name | location | reading_date | reading_time | amps | volts | kw | kwh | kva | pf | key -------------------------------------------------------------------------------------------------------------------------------------- cu1.cb1.r1 | Customer 1 | 12.01.a1 | 2012-01-02 | 00:01:01 | 4.51 | 229.32 | 1.03 | 87 | 1.03 | 0.85 | 15 cu1.cb1.r1 | Customer 1 | 12.01.a1 | 2012-01-02 | 01:01:01 | 4.18 | 230.3 | 0.96 | 90 | 0.96 | 0.84 | 16 cu1.cb1.s2 | Customer 2 | 10.01.a1 | 2012-01-02 | 00:01:01 | 7.34 | 228.14 | 1.67 | 179 | 1

Need help with a complex Join statement in SQL

帅比萌擦擦* 提交于 2019-12-03 17:06:43
How can you join between a table with a sparse number of dates and another table with an exhaustive number of dates such that the gaps between the sparse dates take the values of the previous sparse date? Illustrative example: PRICE table (sparse dates): date itemid price 2008-12-04 1 $1 2008-12-11 1 $3 2008-12-15 1 $7 VOLUME table (exhaustive dates): date itemid volume_amt 2008-12-04 1 12345 2008-12-05 1 23456 2008-12-08 1 34567 2008-12-09 1 ... 2008-12-10 1 2008-12-11 1 2008-12-12 1 2008-12-15 1 2008-12-16 1 2008-12-17 1 2008-12-18 1 Desired result: date price volume_amt 2008-12-04 $1 12345

SQL Server - SELECT TOP 5 rows for each FK

懵懂的女人 提交于 2019-12-03 15:46:58
I've got the following query, that looks up the TOP 5 Products matching the search. Each Product is associated with a Shop SELECT TOP 5 * FROM Products p, Shops s WHERE p.ShopId = s.ShopId AND p.ProductName LIKE '%christmas%' I need to extend this so that it returns me the TOP 5 Products in each Shop . Could anyone let me know how the query could be modified to achieve this? - i.e. choose the TOP 5 products matching "%christmas%" in each shop (rather than the current which shows the TOP 5 products matching "%chrismas%" across all shops). gbn You're actually missing an ORDER BY to make the TOP

How do I write a join with this unusual matching criteria?

泪湿孤枕 提交于 2019-12-03 13:01:20
I want to "left join" a table so that a value is joined not just to a matching row, but also to any subsequent non-matching rows, up to the next matching row. To put it another way, I want to fill in nulls with the previous non-null value. Sample data and desired result: Table x : id ---- 1 2 3 4 5 Table y : id | val ----+----- 1 | a 4 | b Result of select x.id, y.val from x left join y on x.id=y.id order by x.id; : id | val ----+----- 1 | a 2 | 3 | 4 | b 5 | Desired result: id | val ----+----- 1 | a 2 | a 3 | a 4 | b 5 | b Erwin Brandstetter Indices Create indices on x.id and y.id - which you

Select the top 1 row from each group

吃可爱长大的小学妹 提交于 2019-12-03 12:13:50
I have a table that lists the versions of software that are installed: id | userid | version | datetime ----+--------+---------+------------------------ 111 | 75 | 10075 | 2013-03-12 13:40:58.770 112 | 75 | 10079 | 2013-03-12 13:41:01.583 113 | 78 | 10065 | 2013-03-12 14:18:24.463 114 | 78 | 10079 | 2013-03-12 14:22:20.437 115 | 78 | 10079 | 2013-03-12 14:24:01.830 116 | 78 | 10080 | 2013-03-12 14:24:06.893 117 | 74 | 10080 | 2013-03-12 15:31:42.797 118 | 75 | 10079 | 2013-03-13 07:03:56.157 119 | 75 | 10080 | 2013-03-13 07:05:23.137 120 | 65 | 10080 | 2013-03-13 07:24:33.323 121 | 68 | 10080

FORCE INDEX mySQL …where do I put it?

限于喜欢 提交于 2019-12-03 10:30:33
问题 I have the following mySQL query that works perfectly fine. Except that I need to add a "FORCE INDEX" and I'm unsure on where I have to do this. I tried just about every location and always receive a mySQL error. What am I doing wrong? Here is the original query: $sql_select_recent_items = $db->query("SELECT * FROM (SELECT owner_id, product_id, start_time, price, currency, name, closed, active, approved, deleted, creation_in_progress FROM db_products ORDER BY start_time DESC) as resultstable

What is the best way to select the first two records of each group by a “SELECT” command?

主宰稳场 提交于 2019-12-03 08:01:17
For instance I have the following table: id group data 1 1 aaa 2 1 aaa 3 2 aaa 4 2 aaa 5 2 aaa 6 3 aaa 7 3 aaa 8 3 aaa What is the best way to select the first two records of each group by a "SELECT" command? If there is no good way to do so, what routine do you suggest?(in PHP) (model outcome) 1 1 aaa 2 1 aaa 3 2 aaa 4 2 aaa 6 3 aaa 7 3 aaa I knew that cross-joining by a.id >= b.id in a sub-query can be working but I am looking for a more scalable solution that can be applied on a table with millions of records. Thanks select a.* from Tablename a where ( select count(*) from Tablename as b

Select records based on last date

偶尔善良 提交于 2019-12-03 05:02:17
Based on the table called Course below: How can I select records which have course name with latest date? I mean if I have two same course names for one ID, I should only show the latest one as the below result. Simply, I want only to show the latest row per ("ID", "Course Name"). And what if I have two date columns in Course table, which are StartDate & EndDate and I want to show the same based on EndDate only.? I am using PostgreSQL. In PostgreSQL, to get unique rows for a defined set of columns , the preferable technique is generally DISTINCT ON : SELECT DISTINCT ON ("ID") * FROM "Course"