Oracle reduce result set on field duplication - oracle

I have a result set of a select in Oracle (12c) as the following:
GROUP_ID NAME ORDERING
1 AA 0
1 AA 1
1 AB 2
1 AC 3
2 BA 1
2 BA 2
2 BB 3
2 BC 4
I do not know how I could reduce the result set to remove rows based on one column while keeping the other fields. The expected outcome looks like the following:
GROUP_ID NAME ORDERING
1 AA 1
1 AB 2
1 AC 3
2 BA 2
2 BB 3
2 BC 4
I tried to solve it using group by but it got rid of the required field ordering. I am not an expert on window functions but I think it could be a valid attempt to use one.

From your data, it seems that you only need:
select group_id, name, max(ordering)
from yourTable
group by group_id, name

Related

Group by datatable using integer range using Linq

I'm trying to group a set of data based on the range of an age(interger) using linq,
e.g. I have datatable -
id name age
1 abc 20
2 pqr 45
3 jkl 34
5 xyz 39
6 lmn 65
I want result as -
age range count
18-29 1
30-39 2
40-49 1
50-59 0
60-69 1
.
.
.
I would like to group datatable based on the age with appropriate age range and display the count.

Distinct on two columns with same data type

In my game application I have a combats table:
id player_one_id player_two_id
---- --------------- ---------------
1 1 2
2 1 3
3 3 4
4 4 1
Now I need to know hoy many unique users played the game. How can I apply distinct, count on both columns player_one_id and player_two_id?
Many thanks.
By using union you can get unique distinct value.
$playerone = DB::table("combats")
->select("combats.player_one_id");
$playertwo = DB::table("combats")
->select("combats.player_two_id")
->union($playerone)
->count();

Complex Networks in Hive - Optimization Code

I have a problem with how to get my Hive code optimized.
I have a huge table as follows:
Customer_id Product_id Date Value
1 1 02/28 100.0
1 2 02/02 120.0
1 3 02/10 144.0
2 2 02/15 120.0
2 3 02/28 144.0
... ... ... ...
I want to create a complex network where I link the products through the buyers. The graph does not have to be directed and I have to count the number of links between them.
In the end I need this:
Product_x Product_y amount
1 2 1
1 3 1
2 3 2
Can anyone help me with this?
I need an optimized way to do this. The join of the table with itself is not the solution. I really need an optimum way on this =/
CREATE TABLE X AS
SELECT
a.product_id as product_x,
b.product_id as product_y,
count(*) as amout
FROM table as a
JOIN table as b
ON a.customer_id = b.customer_id
WHERE a.product_id < b.product_id
GROUP BY product_x, product_y;

oracle left outer joins not showing null values but displays same value

Problem is in left outer join, when there are no rows in right side table then it does not display null values, it displays previous values....
Like this....
1 st Table contains
PGMTX_CODE PGMTX_MARKS PGMTX_TOTQSTN
-------------------------------------------
EE 1 5
EE 2 5
EE 3 0
EE 4 0
2 nd Table contains
PGMTX_CODE PGMTX_MARKS PGMTX_ACTUSEDQST
-------------------------------------------
EE 1 5
So I want result like...
PGMTX_MARKS PGMTX_TOTQSTN PGMTX_ACTUSEDQST
--------------------------------------------------
1 5 5
2 5 blank
3 0 blank
4 0 blank
I use query like this...
SELECT m.PGMTX_MARKS,
m.PGMTX_TOTQSTN,
tlm.PGMTX_ACTUSEDQST,
from PAPERGEN_MTL_OEX m
left OUTER JOIN PAPERGEN_TLMTL_OEX tlm
ON m.PGMTX_CODE=tlm.PGMTX_CODE
where m.PGMTX_CODE='EE'
order by m.PGMTX_MARKS
But I got result like
PGMTX_MARKS PGMTX_TOTQSTN PGMTX_ACTUSEDQST
--------------------------------------------------
1 5 5
2 5 5
3 0 5
4 0 5
Your join condition is wrong, should be
ON m.PGMTX_CODE=tlm.PGMTX_CODE AND m.PGMTX_MARKS = tlm.PGMTX_MARKS

How do I get the next record in table with linq?

I have a table Orders like this:
ID
CustomerID
name
ID CustomerID name
1 4 aa
5 6 bbb
4 9 ccc
8 10 ddd
first ordering the table,then get the next row..... How to do?
if current row id is 4 ,i want to get the row where id==5
I think you want this:
Orders.OrderBy(x=>x.ID).Skip(1).Take(1)
Edit: If I understand your question now:
Orders.OrderBy(x=>x.ID).Where(x=>x.ID>4).FirstOrDefault();

Resources