Get new Id + 1 for each group in SQL - sql

Please help me to figure out a way of getting from a data set the first number id of each group IF the Id is not already taken yet... I don't even know to explain it, So I will explain down here:
Id | Col1 | Col2 | Value | Number
------+-------+------+----------+-------
17525 | A | B | 1086.00 | 1
17525 | A | B | 1086.00 | 2
17525 | A | B | 1086.00 | 3
17526 | A | B | 1378.00 | 1
17526 | A | B | 1378.00 | 2
17526 | A | B | 1378.00 | 3
17527 | A | B | 1498.00 | 1
17527 | A | B | 1498.00 | 2
17527 | A | B | 1498.00 | 3
And I want to get something like this:
For each Id OR Value (doesn't matter, are equal) the FIRST Number, after the FIRST already taken from the other group.
Something like this:
Id | Col1 | Col2 | Value | Number
------+-------+------+----------+-------
17525 | A | B | 1086.00 | 1
17526 | A | B | 1378.00 | 2
17527 | A | B | 1498.00 | 3
So for the first value, 1086.00 I'll take Number 1, for the 2nd value 1378.00 I'll will take Number 2, because 1 is already taken be the first value.
I tried for 3 hours, with ROW_NUMBER, doesn't work, Recursion CTE could't pass the Max Recursion Limit 100 error.
Please HELP!
Thanks.

Have you considered using dense_rank()?:
select distinct Id, Col1, Col2, Value
, dr = dense_rank() over (order by Id)
from t
returns:
+-------+------+------+---------+----+
| Id | Col1 | Col2 | Value | dr |
+-------+------+------+---------+----+
| 17525 | A | B | 1086,00 | 1 |
| 17526 | A | B | 1378,00 | 2 |
| 17527 | A | B | 1498,00 | 3 |
+-------+------+------+---------+----+

Related

SQL return only rows where value exists multiple times and other value is present

I have a table like this in MS SQL SERVER
+------+------+
| ID | Cust |
+------+------+
| 1 | A |
| 1 | A |
| 1 | B |
| 1 | B |
| 2 | A |
| 2 | A |
| 2 | A |
| 2 | B |
| 3 | A |
| 3 | B |
| 3 | B |
| 3 | C |
| 3 | C |
+------+------+
I don't know the values in column "Cust" and I want to return all rows where the value of "Cust" appears multiple times and where at least one of the "ID" values is "1".
Like this:
+------+------+
| ID | Cust |
+------+------+
| 1 | A |
| 1 | A |
| 1 | B |
| 1 | B |
| 2 | A |
| 2 | A |
| 2 | A |
| 2 | B |
| 3 | A |
| 3 | B |
| 3 | B |
+------+------+
Any ideas? I can't find it.
You may use COUNT window function as the following:
SELECT ID, Cust
FROM
(
SELECT ID, Cust,
COUNT(*) OVER (PARTITION BY Cust) cn,
COUNT(CASE WHEN ID=1 THEN 1 END) OVER (PARTITION BY Cust) cn2
FROM table_name
) T
WHERE cn>1 AND cn2>0
ORDER BY ID, Cust
COUNT(*) OVER (PARTITION BY Cust) to check if the value of "Cust" appears multiple times.
COUNT(CASE WHEN ID=1 THEN 1 END) OVER (PARTITION BY Cust) to check that at least one of the "ID" values is "1".
See a demo.

Hive LIMIT changes GROUP BY result

SELECT `col1`
, `col2`
, count(*)
FROM `tab1`
GROUP BY `col1`
, `col2`
limit 10;
+-------+-------+--------+
| col1 | col2 | _c2 |
+-------+-------+--------+
| A | A | 1 |
| A | B | 34241 |
| A | C | 12345 |
| A | D | 145 |
| A | E | 26 |
| A | F | 224547 |
| B | A | 1429 |
| B | B | 25 |
| B | C | 94 |
| B | D | 1 |
+-------+-------+--------+
If I take one of the results from that, and do a specific query for that combination, the result changes.
SELECT `col1`
, `col2`
, count(*)
FROM `tab1`
WHERE `col1`='A'
AND `col2`='B'
GROUP BY `col1`
, `col2`;
+-------+-------+--------+
| col1 | col2 | _c2 |
+-------+-------+--------+
| A | B | 38944 |
+-------+-------+--------+
If I run set hive.map.aggr=true; then I get a different count, somewhere in between the two.
Any ideas why or how to fix?
If I run the same query with LIMIT 20 then it gives the right count. Or, I should say, the same count as the WHERE query, I haven't counted them myself to check that it is correct!

output difference of two values same column to another column

Can anhone help me out or point me in the right direction? What is simplest way to get from current table to output table??
Current Table
ID | type | amount |
2 | A | 19 |
2 | B | 6 |
3 | A | 5 |
3 | B | 11 |
4 | A | 1 |
4 | B | 23 |
Desires output
ID | type | amount | change |
2 | A | 19 | 13 |
2 | B | 6 | -6 |
3 | A | 5 | -22 |
3 | B | 11 | |
4 | A | 1 | |
4 | B | 23 | |
I don't get how the values are put on rows. You can, for instance, subtract the "B" value from the "A" value for any given id. For instance:
select t.*,
(case when type = 'A'
then amount - max(amount) filter (type = 'B') over (partition by id)
end) as diff_a_b
from t;

Limit a sorted number of rows joined

I have two tables, A and B, and a join table M. I want to, for each A.id, get the top 2 B.id's sorting on the value in table M, producing the results below. This is running on an Azure SQL database
Table A Table M Table B
+-----+ +-----+-----+-------+ +-----+
| Id | | AId | BId | Value | | Id |
+-----+ +-----+-----+-------+ +-----+
| 1 | | 1 | 3 | 4 | | 1 |
| 2 | | 1 | 2 | 3 | | 2 |
| 3 | | 3 | 2 | 3 | | 3 |
| 4 | | 3 | 5 | 6 | | 4 |
+-----+ | 3 | 3 | 4 | | 5 |
| 4 | 1 | 2 | +-----+
| 4 | 2 | 1 |
| 4 | 4 | 3 |
+-----+-----+-------+
Result
+-----+-----+-------+
| AId | BId | Value |
+-----+-----+-------+
| 1 | 3 | 4 |
| 1 | 2 | 3 |
| 3 | 5 | 6 |
| 3 | 3 | 4 |
| 4 | 1 | 2 |
| 4 | 4 | 3 |
+-----+-----+-------+
I know that I can select all the M.AId rows where they equal 1, sort it, and limit by 2, but I need to do this for every row in Table A. I've made an attempt to use group by, but I wasn't sure how to sort and limit it. I've also tried to search for resources associated with this issue but I couldn't find any resources.
(I also wasn't sure how to word the title for this issue)
You can just use ROW_NUMBER:
SELECT
AId, BId, Value
FROM (
SELECT *,
Rn = ROW_NUMBER() OVER(PARTITION BY AId ORDER BY Value DESC)
FROM M
) t
WHERE Rn <= 2

Hive Find Start and End of Group or Changing point

Here is the table:
+------+------+
| Name | Time |
+------+------+
| A | 1 |
| A | 2 |
| A | 3 |
| A | 4 |
| B | 5 |
| B | 6 |
| A | 7 |
| B | 8 |
| B | 9 |
| B | 10 |
+------+------+
I want to write a query to get:
+-------+--------+-----+
| Name | Start | End |
+-------+--------+-----+
| A | 1 | 4 |
| B | 5 | 6 |
| A | 7 | 7 |
| B | 8 | 10 |
+-------+--------+-----+
Does anyone know how to do it?
This is not the most efficient way, but it this works.
SELECT name, min(time) AS start,max(time) As end
FROM (
SELECT name,time, time- DENSE_RANK() OVER (partition by name ORDER BY
time) AS diff
FROM foo
) t
GROUP BY name,diff;
I would suggest try the following query and build a GenericUDF to identify the gaps, much more easier :)
SELECT name, sort_array(collect_list(time)) FROM foo GROUP BY name;