This topic is locked
[SOLVED]

Serial number (not PK)

9/13/2021 5:19:49 PM
PHPRunner General questions
S
SteveA author

Hi Everyone

I dont think this is a support issue.

I have two tables

  1. Premises ( premises_id) etc
  2. Reports ( report_id, report_number) etc

I want to generate a sequential number from 1 to 12 ( and then resetting to 1) to use as a report number for financial periods.

I cant use AI, has anyone come across this? I cant seem to find anything here or on the net

Kind regards

Steve

A
acpan 9/13/2021

You have not mentioned about the relation between the 2 tables.

Assuming you are only interested in Reports table, below are two ways to generate "re-cycling sequential number"

First Method: Use Stored Procedure
// Set variable
SET @a:=0;

// Select SQL
SELECT ( CASE WHEN @a = 12 THEN @a:=1 ELSE @a:=@a+1 END ) AS report_number,
report_id FROM Reports
ORDER BY report_id ASC;

2nd Method: Use a single SQL Query (which may be what you prefer):
SELECT ( CASE WHEN @a = 12 THEN @a:=1 ELSE @a:=@a+1 END ) AS report_number,
report_id FROM Reports, (SELECT @a:= 0) AS temp_table
ORDER BY report_id ASC;

Both give the same results:

report_number report_id
1 1
2 78
3 115
4 116
5 118
6 129
7 142
8 145
9 159
10 179
11 194
12 202
1 207
2 208
3 222
..... ......

Maybe the best way is to extract the report number from the reports dates itself, if it is also part of the Report table.

admin 9/14/2021

I would just do it a combination of SQL and PHP code:

$c = DBLookup("select report_id+1 from FROM Reports ORDER BY report_id ASC limi 0,1 ");
if ($c==13) $c=1;

And now $c variable holds your next sequence number and you can do whatever you want to do with it.

A
acpan 9/14/2021

I think @Sergey's method is better, it is a standard PHPR way (that's why we love PHPR!), do it in view as. and you can manipuate as you desire, and simple to understand :D

S
SteveA author 9/16/2021

Excellent thank you both for your contributions, solved my problem

kind regards
Steve