Update null table values with value from previous record - vba

There's a table named sample, and it has two columns id, and id2. Some records on id2 are filled with numbers, but some are null. So I need to fill them up with the same number as their closest record. That is, if each record on id2 is not null, move on to the next one, and if each record on id2 is null, fill it in with the previous one. How can I do this with vba?
sample
id id2
1 100
2
3 500
4 600
5
6 800
sample_result
id id2
1 100
2 100
3 500
4 600
5 600
6 800

In Access I'm not sure it can be done in pure SQL, but I think this should get you close to what you want:
UPDATE sample AS s
SET s.id2 = Dmax("id2", "sample", "id <" & [s].[id])
WHERE (( ( s.id2 ) IS NULL ));

Related

MSAccess - query to return result set of earliest rows with a unique combination of 2 columns

I have a table with the following columns.
ID (auto-inc)
When (datetime)
id1 (number)
id2 (number)
The combination of id1 and id2 can be unique or duplicated many times.
I need a query that returns the earliest record (by When) for each unique combination of id1+id2.
Example data:
ID
When
id1
id2
1
1-Jan-2020
4
5
2
1-Jan-2019
4
5
3
1-Jan-2021
4
5
4
1-Jan-2020
4
4
5
1-Jan-2019
4
4
6
1-Jan-2021
4
6
I need this to return rows 2, 5 and 6
I cannot figure out how to do this with an SQL query.
I have tried Group By on the concatenation of id1 & id2, and I have tried "Distinct id1, id2", but neither return the entire row of the record with the earliest When value.
If the result set can just return the ID that is fine also, I just need to know the rows that match these two requirements.
Okay, I had a few minutes to kill:
SELECT Data.* FROM Data WHERE ID IN (
SELECT TOP 1 ID FROM Data AS D
WHERE D.id1=Data.id1 AND D.id2=Data.id2 ORDER BY When);
or
SELECT Data.* FROM Data INNER JOIN (
SELECT id1, id2, Min(When) AS MW FROM Data
GROUP BY id1, id2) AS D
ON Data.When = D.MW AND Data.id1=D.id1 AND Data.id2=D.id2;
ID
When
id1
id2
2
1/1/2019
4
5
5
1/1/2019
4
4
6
1/1/2021
4
6

How To Get Top N Rows per Each Group - MS Access [duplicate]

I have a table with the following columns.
ID (auto-inc)
When (datetime)
id1 (number)
id2 (number)
The combination of id1 and id2 can be unique or duplicated many times.
I need a query that returns the earliest record (by When) for each unique combination of id1+id2.
Example data:
ID
When
id1
id2
1
1-Jan-2020
4
5
2
1-Jan-2019
4
5
3
1-Jan-2021
4
5
4
1-Jan-2020
4
4
5
1-Jan-2019
4
4
6
1-Jan-2021
4
6
I need this to return rows 2, 5 and 6
I cannot figure out how to do this with an SQL query.
I have tried Group By on the concatenation of id1 & id2, and I have tried "Distinct id1, id2", but neither return the entire row of the record with the earliest When value.
If the result set can just return the ID that is fine also, I just need to know the rows that match these two requirements.
Okay, I had a few minutes to kill:
SELECT Data.* FROM Data WHERE ID IN (
SELECT TOP 1 ID FROM Data AS D
WHERE D.id1=Data.id1 AND D.id2=Data.id2 ORDER BY When);
or
SELECT Data.* FROM Data INNER JOIN (
SELECT id1, id2, Min(When) AS MW FROM Data
GROUP BY id1, id2) AS D
ON Data.When = D.MW AND Data.id1=D.id1 AND Data.id2=D.id2;
ID
When
id1
id2
2
1/1/2019
4
5
5
1/1/2019
4
4
6
1/1/2021
4
6

Get previous value from column A when column B is not null in Hive

I have a table tableA below
ID number Estimate Client
---- ------
1 3 8 A
1 NULL 10 Null
1 5 11 A
1 NULL 19 Null
2 NULL 20 Null
2 2 70 A
.......
I would like to select previous row of Estimate column when number column is not null. For instance, when number = 3, then pre_estimate = NULL, when number = 5, then pre_estimate = 10, and when number = 2, then pre_estimate = 20.
The query below does not seem to return the correct answer in Hive. What should be correct way to do it?
select lag(Estimate, 1) OVER (partition by ID) as prev_estimate
from tableA
where number is not null
Consider the table with following structure:
number - int
estimate - int
order_column - int
order_column is taken as a column on which you want to sort your table rows.
Data in table:
number estimate order_column
3 8 1
NULL 10 2
5 11 3
NULL 19 4
NULL 20 5
2 70 6
I used the following query and got the result you have mentioned.
SELECT * FROM (SELECT number, estimate, lag(estimate,1) over(order by order_column) as prev_estimate from tableA) tbl where tbl.number is not null;
As per my understanding, I didn't find the reason to partition by id, that's why I haven't considered ID in the table.
The reason you were getting wrong results is due to the reason that where clause in main query will select only the records with number as not null and then it computes lag function, but you need to consider all the rows when computing the lag function and then you should select rows with number as not null.

Find all ids which lie between Range and are present in a common foreign key id

I want all the specific Ids which have a common other Id. I am sending the data through an user-defined table type.
CREATE TYPE rangeType AS TABLE (
ID2 int NOT NULL,
StartRange int NULL,
EndRange int NULL
);
The table is Like the following
ID1 ID2 Value
11 2 3
12 2 4
12 3 8.9
15 3 10
15 2 4
The value I will send will be of the form
DECLARE #temp_table rangeType
Insert INTO #temp_table values (2,4,10)
INSERT INTO #temp_table values (3,5,10)
So I want Output to be all those ID1's which have both the value of ID2 as 2 and 3 and the rows which have ID2 as 2 should have a value between 4 and 10 and all those rows which have ID2 as 3 should have a value between 5 and 10.
So my output, in this case, should be
ID1
12
15
as the ID1 12 and 15 maps both 2 and 3 and have the ranges between the specified respective ranges.
I Tried an inner join on the table followed by a BETWEEN operator. Which is giving me a correct value the operation which is performed is OR operation rather than an AND operation which I want.
You can use below query
SELECT ID1 FROM TABLE
WHERE ID2 IN (2,3)
AND
CASE WHEN ID2 = 2 AND VALUE >= 4 AND VALUE >=10 THEN 1
WHEN ID2 = 3 AND VALUE >= 5 AND VALUE >=10 THEN 1
ELSE 0
END = 1;

How to update a field's value by an incremental value without using loop in SQL Server 2008?

I have a table #temp in SQL Server 2008 like this:
Id p_id h_no f_id
------------------
1 100 A01 null
2 200 A02 null
3 300 A02 null
4 400 null null
5 500 null null
6 600 A03 null
7 700 A01 null
8 400 null null
So basically, every record has a p_id, but may or may not have h_no.
What I want is to replace f_id values with a dummy incremental number based on:
if h_no value of a record matches another(s), this (those) ones will have same f_id (check ids:1 & 7 or ids:2 & 3 in the example)
if h_no is null but p_id values are equal for some cases, they will have same f_id (check ids: 4 & 8 in the example)
For example, the sample table above should be:
Id p_id h_no f_id
-----------------
1 100 A01 1
2 200 A02 2
3 300 A02 2
4 400 null 3
5 500 null 4
6 600 A03 5
7 700 A01 1
8 400 null 3
I do not want to use a loop for this process. I am trying to find a more optimal solution for this. I need a query something like below, could not find the correct syntax.
declare #tempFID int = 1;
update t
set t.f_id = #tempFID++ --syntax error
from #temp t
inner join #temp t2 on t.Id = t2.Id
where (t.h_no is not null and t.h_no = t2.h_no)
or (t.h_no is null and t.p_id = t2.p_id)
I also tried but had syntax error:
update t
set t.f_id = (set #tempFID = #tempFID + 1) --syntax error
...
Any help would be so appreciated!
;WITH cte AS (
SELECT *
,CASE WHEN h_no IS NULL THEN p_id ELSE MIN(p_id) OVER (PARTITION BY h_no) END as PIdGroup
FROM
#Table
)
, cteFIdValue AS (
SELECT
Id
,DENSE_RANK() OVER (ORDER BY PIdGroup) as f_id
FROM
cte
)
UPDATE t
SET f_id = u.f_id
FROM
Table t
INNER JOIN cteFIdValue u
ON t.ID = u.ID
Find the minimum p_id for each h_no and just leave it as the assigned p_id if h_no is null
Then create a dense rank on the PidGroup
Update the Table
so you have problems besides a syntax error in your code above. First your join will only get the exact same record, you would have to change to t.ID <> t2.ID as left join and still need some sort of ranking. honestly I am not positive what you are attempting there.
This approach might be simpler:
update #temp
set f_id = isnull(f_id, 0) +
case when condition1 is met then value 1
etc
when final condition is met then 0
else null
end