Can column values be accessed like an array in SQL server - sql

Can column values be accessed like an array in SQL server. for instance Select x[1] from tableA gives me the value for that field. I'm attempting to do this #OldValue = (SELECT #fieldname FROM #del). But this just returns the actual field name not the field value

No. Data in tables is not ordered, so there is no first, second, etc. record. You access a record by some criteria, e.g. a user by a user ID or login name, an employee by an employee number, etc. Or you use technical IDs to access records.
If you want the fifth record according to some order you can usually use some limit clause. E.g.:
SELECT *
FROM user
ORDER BY userid
OFFSET 4 ROWS FETCH NEXT 1 ROW ONLY;
fetches the 5th record ordered by user ID.
As to columns: They are accessed by their names. E.g.:
SELECT name
FROM user
ORDER BY userid
OFFSET 4 ROWS FETCH NEXT 1 ROW ONLY;
As you see it would make no sense to have some Excel-like access, because you need an order by clause and accessing columns via name rather than by some letter or number is even more readable and less prone to errors.

Related

How to remove duplicate rows and keep one in an Access database?

I need to remove duplicate rows in my Access database, does anyone have generic query to do this? As I have this problem with multiple tables
There are two things you need to do,
Determine what the criteria are for a unique record - what is the list of columns where two, or more, records would be considered duplicates, e.g. JobbID and HisGuid
Decide what you want to do with the duplicate records - do you want to hard delete them, or set the IsDeleted flag that you have on the table
Once you've determined the criteria that you want to use for uniqueness you then need to pick 1 record from each group of duplicates to retain. A query along the lines of:
SELECT MAX(ID)
FROM MyTable
GROUP
BY JobbID, HisGuid
Will give you (and I've assumed that the ID column is an auto-increment/identity column that is unique across all records in the table) the highest value for each group of records where JobbID and HisGuid are both the same. You could use MIN(ID) if you want, it's up to you - you just need to pick ONE record from each group to keep.
Assuming that you want to set the IsDeleted flag on the records you don't want to keep, you can then incorporate this into an update query:
UPDATE MyTable
SET IsDeleted = 1
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP
BY JobbID, HisGuid
)
This takes the result of the query that retrieves the highest IDs and uses it to say set IsDeleted to 1 for all the records where the ID isn't the highest ID for each group of records where JobbID and HisGuid are the same.
The only part I can't help you with is running these queries in Access as I don't have it installed on the PC I'm using right now and my memory is a bit rusty regarding how/where to run arbitrary queries.

Get latest data for all people in a table and then filter based on some criteria

I am attempting to return the row of the highest value for timestamp (an integer) for each person (that has multiple entries) in a table. Additionally, I am only interested in rows with the field containing ABCD, but this should be done after filtering to return the latest (max timestamp) entry for each person.
SELECT table."person", max(table."timestamp")
FROM table
WHERE table."type" = 1
HAVING table."field" LIKE '%ABCD%'
GROUP BY table."person"
For some reason, I am not receiving the data I expect. The returned table is nearly twice the size of expectation. Is there some step here that I am not getting correct?
You can 1st return a table having max(timestamp) and then use it in sub query of another select statement, following is query
SELECT table."person", timestamp FROM
(SELECT table."person",max(table."timestamp") as timestamp, type, field FROM table GROUP BY table."person")
where type = 1 and field LIKE '%ABCD%'
Direct answer: as I understand your end goal, just move the HAVING clause to the WHERE section:
SELECT
table."person", MAX(table."timestamp")
FROM table
WHERE
table."type" = 1
AND table."field" LIKE '%ABCD%'
GROUP BY table."person";
This should return no more than 1 row per table."person", with their associated maximum timestamp.
As an aside, I surprised your query worked at all. Your HAVING clause referenced a column not in your query. From the documentation (and my experience):
The fundamental difference between WHERE and HAVING is this: WHERE selects input rows before groups and aggregates are computed (thus, it controls which rows go into the aggregate computation), whereas HAVING selects group rows after groups and aggregates are computed.

Get next record in dataset from guid

I have a table that has say four fields in it.
guid, date, group, description
So I get a record set based on group and then ordered by date and say that nets me three records.
I select the first record and show it in my web page. I have next and previous buttons.
When I click on the next button, how do I get the next record in the record set given there is no row identifier such as 1,2,3 etc?
Is there a sql way to get the previous and next records or do I need to iterate through my record list looking for the guid and showing the next record?
If you show only one record, you could consider you are paginating your records. In this case you have only one record per page.
Most of SQL engines, except Oracle, afaik, supports LIMIT and OFFSET functions, that are commonly used for paginating purposes.
LIMIT specifies the amount of records you want to fetch.
OFFSET specifies the starting record you want to fetch.
You must put these keys at the end of your SQL, like this:
Select guid, date, group, description
From Your_Table
Where Group = 'YourGroup'
Order By Date
Limit 1 Offset 0
And in your application you could store the current "page", so when the user clicks on Next then you increment the "page" variable and pass it to the Offset keyword of your SQL. Or decrement it when the user clicks on Back.

Assign an ID Value for Every Set of Duplicates

How can i generate an ID value for every set of duplicate records as seen in the second table with ID column? In other words, how can I let the first table to look like the second table using SQL query?
Assume that first name and last name in the first table can appear in duplicates.
Each first name and last name can have one or many purchase yr and cost.
The given image is just a sample. Total records in table 1 can reach thousands.
I'm using Oracle SQL.
Note: I'm working with one table only that is the first one. The second table is what I want.
You can use the DENSE_RANK analytic function to assign ID's as below:
EDIT:
Simplified query to generate ID's.
SELECT
DENSE_RANK() OVER (ORDER BY First_Name, Last_Name) ID,
t.*
FROM Table1 t;
Reference:
DENSE_RANK on Oracle Database SQL Reference

How can I retrieve just the last two rows inserted in an SQLite database?

I want to retrieve the last 2 rows inserted into an SQLite database for display in a label. What would be the proper SQLite statement to retrieve these rows?
If you last inserted data has just inserted (maybe in the same method), you can use:
last_insert_rowid
If you like to read the last insert data after, let's say a app re/start, then you need to do some ordering.
If you have a auto-incrementing id row, you can do the following
"SELECT * from your_table ORDER BY your_autoinc_id DESC LIMIT 2"