Moving from a calc spreadsheet to base - how to reference cell above?

I am flirting with the idea of moving away from a spreadsheet and into a database but am stumbling with the first steps. Can a database table see what is in the row above and do calculations on that? How do you do that? In Calc I have a sheet that says what the four items before an item are, ignoring repeats of items. In it I have columns using if statements testing if the items are the same or different, and use that to generate the list. Items are added to this list, out of order and then sorted. How could you do this in base? I could not work out how to even start?

Can you do it with some type of calculated fields?
Can you do it using a many to many relationship, using linking table as an intermediate between two tables?
I hear mention of Lag / Previous type of functions, but does libra office or open office have these? And could you use these?

I attach a spead sheet showing what I am trying to translate into base.
z trying to learn base 02.ods (19.4 KB)

Any concept of “previous” and “next” is defined by data, not by position. The order of appearance is reflected by an auto-incrementing ID number. You may also use a time stamp column to distinguish previous from next.
A query that includes a running total is quite a usual thing, however a little bit advanced for a total beginner.

Advantage and disadvantage of databases: You don’t make assumptions on the order of data. The database will take care of this. Usually you don’t sort the database itself, but the rows are accessed via an index. (Much more efficient, when you consider 1.000.000 rows…) So you will need a condition, how to find the “previous” entry

Something like this SQL:SELECT * FROM table ORDER BY column but you need to use existing names for table and column.
.
There is a guide for Base in the documentation area of the LibreOffice website. English documentation | LibreOffice Documentation - LibreOffice User Guides

A book on SQL would be useful too.

It’s what is keeping me in calc, using advanced filters, triggered by macro’s to interogate 9 different variations of a set of data, that is only 421 records long at present but needs to grow, and is making my spread sheet groan!

awesomeness.

PowerFilter_RunningTotals.odb (94.7 KB)
Open the running totals form.
Select a person name.
The running totals for each sale of the selected person appear in the yellow grid, the single sales for a selected running total appear in the blue grid. The yellow grid is based on the running total query. You will never have to copy around any formulas. The sort order is not relevant. The sequence is defined by the auto-ID of table “Data”.

@melvin7020 ,
this is far easier if you use a database with window functions.
although not a fan, I used firebird embedded for this, it can be done using the default HSQLDB but that would be both arduous and unnecessary.
.
I use a single query and although it’s small it’s not simple so I don’t expect you you understand the mechanics of it.
what it demonstrates (I hope) are some of the benefits a database provides versus a spreadsheet.
.
I added a few items in order to confirm integrity.
Cell_Ref_fb.odb (12.0 KB)

Fabulous thank you. Villeroy’s is the way I am going. Just need to work out how to do simple things in base, that I can do easily in calc, like split a field at a comma using a query.

Learning curves!

OMG that is soo much easier than the spread sheet. Still on the learning curve, but just counting and ordering variations, it’s fabulous.

@melvin7020,
the targets of your comments are ambiguous, we do not know to whom you are replying.
you had accepted the post by Villeroy as the solution but now I see that you have changed your mind and accepted my answer.
.
I originally composed the query fairly quickly and checked only for correct duplication.
next day I checked every row and discovered an error, I had failed to skip the previously duplicated data from subsequent rows, so I fixed it.
to avoid creating any confusion I did not post the updated code and awaited feedback.
.
so here is the code which exactly reproduces the contents of your spreadsheet, it requires direct mode:

  1. open “Cell_Ref_fb.odb”
  2. hit Queries icon
  3. hit ‘Create Query in SQL View’
  4. menu:>Edit>Run SQL command directly
  5. paste the code
  6. hit F5 to execute
with t as
(
	select
		r,
		lag(txt,4) over(order by r) four_before,
		lag(txt,3) over(order by r) three_before,
		lag(txt,2) over(order by r) two_before,
		lag(txt) over(order by r) one_before,
		txt
	from
	(
		select
			data.*,
			row_number() over(order by id) r,
			case when lag(txt) over(order by id) = txt then 1 else 0 end s
		from
			data
	)
	where s = 0
)
,
Dupes as
(	select
		r,
		row_number() over(order by r) r2
	from
	(
		select				
			row_number() over(order by id) r,
			case when lag(txt) over(order by id) = txt then 1 else 0 end s
		from
			data
	)
	where s = 1
)
,
Gaps as
(
	select
		min(r) mn, max(r) mx
	from Dupes
	group by r - r2
)
,
Fills as
(
	select t1.* from
	Gaps g
	join
	(select r from Dupes) d
	on d.r between g.mn and g.mx
	join
	(select * from t) t1
	on g.mn -1 = t1.r
)
--select * from t
--select * from Dupes
--select * from Gaps
--select * from Fills

select
	four_before, three_before, two_before, one_before, txt
from
(
	select * from Fills
	union all
	select * from t
)
order by r

when using a spreadsheet you have to show duplicates but using SQL we can easily remove them.
here is a short piece of code which removes the duplicated rows from the result set:

select
	id,
	lag(txt,4) over(order by id) four_before,
	lag(txt,3) over(order by id) three_before,
	lag(txt,2) over(order by id) two_before,
	lag(txt) over(order by id) one_before,
	txt "CURRENT"
from
(
	select
		data.*,
		lag(txt) over(order by id) <> txt flag
	from
		data
)
where coalesce(flag, true)
order by id

Thank you cpb, you star! I was being dizzy and got the databases mixed up. It is this lag and over that can replicate =CellRef in a calc spread sheet, that I needed.

Your help in this matter is greatly appreciated. I am finding SQL 1.8 curious. I found Hsqldb User Guide for it.

Well it is a fun rabbit hole to go down.