SQL query to obtain rows around a specific row [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have table structure for competition:
user_id | score
and I need get information 2 rows before (for example) user_id=20 and 2 rows after for show rank table.
I want this result of score table:
order | user_id | score
23 | XY1 | 240
24 | XY1 | 247
25 | 20 | 250 (my specific row)
26 | XY1 | 252
27 | XY1 | 290

with aaa as
(
SELECT
ROW_NUMBER() OVER (ORDER BY score ) AS 'ROW_NUMBER', score, user_id
)
select * from aaa where ROW_NUMBER between
(select ROW_NUMBER-2 from aaa where user_id = 25) AND
(select ROW_NUMBER+2 from aaa where user_id = 25)

Related

How to split a group by query? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I have a table like this:
Id 1
Id 2
Amount
001
AAA
10
001
AAA
10
001
AAA
10
001
AAA
10
001
AAA
10
001
AAA
10
001
AAA
10
001
AAA
10
001
AAA
10
001
AAA
10
If I do a select query like
SELECT id1,id2,sum(amount),count(*) from table group by id1,id2;
I get the answer like;
001 |AAA |100 |10
What I want to do is split this into two aggregates, so that the first 8 rows will have one aggregate sum and the next 2 should have the sum of next two rows [8 is the cut off].
For example, the answer should be like:
001 |AAA |80 |8
001 |AAA |20 |2
Is it possible to achieve this?
As mentioned in comments, the order of 8 rows+2 rows doesn't matter just that the records in both batches be mutually exclusive. Thanks #SadlyFullStack for the answer!
Since order doesn't matter, we can use ceil and row_number with a null order clause in a CTE to flag every 8 rows with a number. Then, we have another query to aggregate those based on the group number we created:
with grouped as
(
select tbl.*
, ceil(row_number() over (order by null) / 8) grp_num
from tbl
)
select id1
, id2
, sum(amount)
, count(*)
from grouped
group by id1, id2, grp_num

How can I count number of appearance of a value and make it into a new column when querying? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a table like this
ProductID | SalesOrderNumber
1 | SO0001
2 | SO0002
3 | SO0001
4 | SO0001
5 | SO0002
I want to query the table into this
ProductID | SalesOrderNumber | SalesOrderLineNumber
1 | SO0001 | 1
2 | SO0002 | 1
3 | SO0001 | 2
4 | SO0001 | 3
5 | SO0002 | 2
Basically, the SalesOrderLineNumber will count the number of time SalesOrderNumber appear. Everytime a same value of SalesOrderNumber show up, SalesOrderLineNumber increase.
How can I do it?
You should use:
SELECT ProductID,
SalesOrderNumber,
ROW_NUMBER() OVER (PARTITION BY SalesOrderNumber ORDER BY ProductId) as SalesOrderLineNumber
FROM yourTable;
The difference between this and the other answer is the use of ProductId in the ORDER BY clause. You seem to want the line numbers ordered by product id, so this seems to capture the intention.
More importantly: If the order has no duplicate products, then this always produces the same results -- that is, the results are stable. If you repeat the partitioning key, then you could get different results each time you run the query.
You can use a window function, ROW_NUMBER(), for this:
SELECT
ProductID,
SalesOrderNumber,
ROW_NUMBER() OVER (PARTITION BY SalesOrderNumber ORDER BY SalesOrderNumber) as SalesOrderLineNumber
FROM yourTable
The ROW_NUMBER() will count the number of times SalesOrderNumber occurs, as we partition by that.

How do I output rating of teams? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have 2 tables: team(team_id, name), performance(team_id, stage, place, date). Teams should earn points: for 1st place - 10 points, for 2nd 5, for 3rd - 3 points and 1 point for 4-7 places. I need to output the rating of teams.
I think it should be like:
SELECT team.name, CASE place
WHEN 1 points + 10
WHEN 2 points + 5
...
Expected reasult:
|---------------------|------------------|
| team.name | Points |
|---------------------|------------------|
| Rockers | 34 |
|---------------------|------------------|
| Batmans | 23 |
|---------------------|------------------|
| ... | ... |
|---------------------|------------------|
First aggregate inside the table performance to calculate the total points of each team with conditional aggregation and then join the table team to the results and rank the teams with RANK() or maybe DENSE_RANK() analytical functions:
select t.team_id, t.name,
coalesce(p.points, 0) points,
rank() over (order by coalesce(p.points, 0) desc) rnk
from team t left join (
select team_id,
sum(
case
when place = 1 then 10
when place = 2 then 5
when place = 3 then 3
when place <= 7 then 1
else 0
end
) points
from performance
group by team_id
) p on p.team_id = t.team_id

SQL - Summing hours in a table grouped by work type [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Using a database, I have a table with 2 columns: amount of hours worked, and type of work. I want to sum the number of hours for each work type.
For example, my "work" table has 2 columns:
+---------+------------+
| hours | type |
+---------+------------+
| 5 | ABC |
| 7 | DEF |
| 3 | GHJ |
| 9 | ABC |
+---------+------------+
And I need it to return 14 hours for type ABC
I need to use it in a select statement as part of other columns I am pulling.
Any help would be greatly appreciated.
Select type, sum(hours) total_hours from work group by type
SELECT SUM(hours), type FROM work GROUP BY type
Select type, sum(hours) as "amount of hours worked" from work group by type
You can use group by to achieve your result
Select type, sum(hours) total_hours from work group by type
group by class is used to find the sum based on the columns specified after the group by class. You can specify more than one column after the group by.
For more detail: Group By

Oracle SQL - Inner Join & Count [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have the following Oracle database, and I need to return the following:
d.directdomain, d.domaindisplayname, r.lastdate (The latest), and count(how many times, r.directdomain = d.directdomain)
basically, I have lots of "people" in r db, and "domains" in the d. I need to return how many times a person in R visited each "domain", and also return the last time they visited the domain.
I tried a few things, but it seems by using count, i need to GROUP BY the date, so that's confusing me.
Example return:
1, Site1, 21/05/13, 5
2, Site2, 20/05/13, 2
d
directdomain (PK)
domaindisplayname
r
rsld (PK)
lastdate
directdomain (FK)
Are you looking for something like this?
SELECT d.directdomain,
d.domaindisplayname,
MAX(r.lastdate) lastdate,
COUNT(*) rcount
FROM d JOIN r
ON d.directdomain = r.directdomain
GROUP BY d.directdomain, d.domaindisplayname
Sample output:
| DIRECTDOMAIN | DOMAINDISPLAYNAME | LASTDATE | RCOUNT |
|--------------|-------------------|-------------------------------|--------|
| 1 | Site1 | August, 15 2013 00:00:00+0000 | 4 |
| 2 | Site2 | August, 18 2013 00:00:00+0000 | 3 |
Here is SQLFiddle demo