SQL Server query to get a pre-position No - sql

I am trying to write SQL query, that will display in a new column a 'pre-position number' with values displayed based on the value on the previous position number column row.
I would appreciate any assistance.
Thank you.

You seem to be looking for LAG(). For this to work, you need a column that can be used to order the data, so your RDBMS can assess which record is the previous record to the current one. Assuming that this column is called id, then:
SELECT
id,
position_nr,
LAG(position_nr) OVER(ORDER BY id) pre_position_nr
FROM mytable

You want to use LAG(). To get the 0, use the three argument form:
SELECT position_no,
LAG(position_no, 1, 0) OVER (ORDER BY position_no) as pre_position_no
FROM mytable;
This assumes that you are ordering by position_no, as suggested by your sample code.

Related

OVER clause for VARCHAR

I can use over clause for numeric and date columns using an aggregate function. But, I'm stuck with being unable to use over clause for the varchar column. In the example below, I can reproduce the FIRST_FILL_DT column using the following lines:
MIN(FILL_DATE) OVER(PARTITION BY ID) AS FIRST_FILL_DT
However, when trying to produce the FIRST_BP_MED column, I am not sure if I can use similar syntax because I don't know if the aggregate function works correctly with VARCHAR Columns.
Can anyone please offer insights or guidance on how to solve this?
My data is like this:
My desired data should like this:
If your database supports the FIRST_VALUE window function, you can use something like this:
FIRST_VALUE(BP_MED) OVER (PARTITION BY ID ORDER BY FILL_DATE) AS first_bp_med
Docs for FIRST_VALUE:
MySQL, SQL Server,
Postgresql,
SQLite
This is pretty straight forward. Use 'FIRST_VALUE' over your window clause to pick the first value omitted by your partition irrespective of the condition.
https://learn.microsoft.com/en-us/sql/t-sql/functions/first-value-transact-sql?view=sql-server-ver15
SELECT
ID, FILL_DATE, BP_MED,
MIN (FILL_DATE) OVER (PARTITION BY ID ORDER BY FILL_DATE) AS FIRST_FILL_DT,
FIRST_VALUE (BP_MED) OVER (PARTITION BY ID ORDER BY FILL_DATE) AS FIRST_BP_MED
FROM
YOURTABLE;

SQL query put nth match in new column

I am using SQL in Microsoft access. The table may contain more than one value for a bar code. I want to a query that returns one row with each unique bar code and columns for each of the first 5 values. Right now, I have a query returning the last value or the first value with min or max. How can I show the second result in the next column? or the nth result in that column? This is in Access but any other SQL help would be appreciated.
Current table:
Current query:
SELECT table.barcode, MIN(table.value)
FROM table
GROUP BY table.barcode
Current output:
Goal query output:
You can use aggregation:
SELECT table.barcode,
MIN(table.value),
IIF(MIN(table.value) = MAX(table.value), MAX(table.VALUE), NULL)
FROM table
GROUP BY table.barcode

Counting Values in a repeated field in BigQuery

I want to select rows that have more thank k values in a repeated field. (consider for example selecting user that have more than 3 email addresses)
In Standard SQL I know I can use
SELECT * FROM dataset.users
WHERE array_length(email_address) > 3
But what is the way to do this in BigQuery legacy SQL?
No need for a subquery; you should be able to filter with OMIT RECORD IF directly:
SELECT *
FROM dataset.users
OMIT RECORD IF COUNT(email_address) <= 3;
Do you mind commenting on why you want to use legacy SQL, though? If you encountered a problem with standard SQL I'd like to understand what it was so that we can fix it. Thanks!
Counting Values in a repeated field in BigQuery
BigQuery Legacy SQL
SELECT COUNT(email_address) WITHIN RECORD AS address_count
FROM [dataset.users]
If you want then to count output rows - you can use below
SELECT COUNT(1) AS rows_count
FROM (
SELECT COUNT(email_address) WITHIN RECORD AS address_count
FROM [dataset.users]
)
WHERE address_count> 3

select single value into multi rows oracle sql

I want to select one value and retrieve it into multi rows i tried to search about this case but i didn't find the write way to sole it and
and finaly sorry for my English
try select your_value, t.* from table t

SQL statement to get largest ID from database column

I have an ID column which it supposed to set to auto-increment but I forgot to set in when creating the database. Let's say the ID is from 1 - 20. I used the select Max() Sql statement to get the largest ID:
SELECT MAX(id) FROM Table_Name;
It supposed to return me 20. However, it returns me 9. I also realized that the id column in database is jumbled up. It starts from 1,2 then skips to 9,10 - 20 then back to 3 - 8. And 8 appears to be the last row and I think that's where the 9 comes from. My id in database is varchar() data type.
So, is there any way to amend my Sql statement to get the largest id in a list of sorted id?
Thanks in advance.
The issue is likely that the ID column is a varchar field, so 9 is greater than 10.
select max(convert(int, id)) from Table
Your column is a character type, not a numeric type, which explains everything you're seeing.
Try casting it to numeric:
select max(cast(id as signed)) from table
You haven't said which database you are using, so the syntax may vary to achieve the cast - consult online docs for your database.
Try this:
SELECT TOP 1 Id FROM Table ORDER BY Convert(INT, id) DESC