MS Access SQL, SELECT last five highest values in a column
For example I have table named games, I want top five games with highest likes.
Something like this:
select top 5 *
from games
order by likes desc, id desc
I have added a "tie-breaker" to the order by clause to prevent access returning more than 5 records, i.e. the primary key.
Thanks to #Fionnuala for pointing out this issue with Access. For more info, see here
Related
The database stores the currency exchange rate on a given day. Each day, one currency exchange value is collected and stored in the database as:
ID (int, AI)
VALUE
DATE
1
2.5
20.01.2021
2
2.7
21.01.2021
3
2.6
22.01.2021
If I would like to calculate the average exchange rate from the last 10 days, should I first sort the data by date and only retrieve the last 10 records when downloading the data, or is it enough to download the last 10 records from the database without sorting?
You can simply do in SQL Server database
SELECT TOP 10 AVG(VALUE) AS AverageRate
FROM YourTable
ORDER BY Id DESC
Concept should be same in other relational databases.
Tables (and table expressions such as views and CTEs) in SQL represent unordered sets. To get data in a particular order, you must specify an ORDER BY clause.
In fairly generic SQL you can do
SELECT AVG(VALUE) AS AverageRate
FROM (
SELECT VALUE
FROM YourTable AS t
ORDER BY Id DESC
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
) AS t
In some RDBMSs, instead of OFFSET FETCH, you use either LIMIT or TOP to achieve the same effect. You still need ORDER BY.
You can do it in both ways.
If you're using SQL with Dapper or ADO.NET, then you can write a query.
It should be sorted if you need the last 10 values
SELECT TOP 10 AVG(your value) AS average
FROM YourCurrencyExchangeTable
ORDER BY DATE DESC
If you're using EntityFrameWorkCore, you can write
var avg = db.yourDbContext
.Where(c => c.Date >= tenDaysAgoDate)
.Average(c => c.yourValue)
I hope my answer helps :)
Basically you have to sort first ( on date) and then get the last 10 values, so you're on the right track.
Betting houses function like this: every week there are two rounds, and in each round when someone wants to make a ticket he gets a number for that round (first ticket of that round(1), second (2)...). I have a ticket table of all entered tickets of a betting-house company.
This table has 30 columns of all the details of each ticket. The primary key is (location, round, number) I explained this number as being the number of the ticket within that round, and the location is the location of the bet-house (a betting company has many bet-houses in different locations). I want to select each ticket one by one, but I do not know how to have some sort of organized way. If the a single column with auto-increment integer type that would be easy. I just want to some how index this triplet primary key to go through a loop to select each entry incrementally. I am using C# to do this?
As you did not provide the real purpose of your task, I can think of 2 situations.
Firstly, you want to read the table in some smaller batches for processing/evaluation. In this case you have to make sure the table is immutable and run SELECT ... ORDER BY number, round, bethouse OFFSET 0 LIMIT 1000, OFFSET 1000 LIMIT 1000, OFFSET 2000 LIMIT 1000 and so on until you come to the last row.
Secondly, you want to process the new incoming rows. In that case there is no way you can solve that with only the multicolumn PK. You have to add a column with an autoincrement value or a date column (i.e. when the row was created). Then you order the results by this column and watch for new rows with WHERE mycolumn > {LastReadValue}.
Hope that helps.
May be the Question's title is not correctly defined what i actually wanted to ask. Here is the more specific description of my question
I have a following table User in my database which has a column i.e. Category which contains multiple values but separated by commas
S.no. Name Category
1 Ankit Ex Soldier, Senior Citizen
2 Ritu Widow, Senior Citizen
3 Akash Ex soldier
I wanted to search the record on the basis of category
for eg. If i search
select * from User where Category='Senior Citizen'
Then it must show Ankit and Ritu record.
How to do this.
plz help
try this:
select * from User where Category like '%Senior Citizen%'
You need LIKE operator:-
select * from User where Category LIKE '%Senior Citizen%'
select * from User where Category LIKE '%Senior Citizen%'
But you should use a separate table for Category.
Like Kiss László wrote you should separate the information in two tables. The professional term for this is called "Normalization". Most important to know are the 1NF, 2NF and 3NF (read this for detailed information).
So it should look like the following:
Table Persons
PersonId Name
1 Ankit
2 Ritu
3 Akash
Table Categories
CategoryId Name
1 Ex. Soldier
2 Senior Citizen
3 Widow
Table PersonCategories
PersonId CategoryId
1 1
1 2
2 2
2 3
3 3
Why should you do this?
In my opinion the biggest reason is performance. I made some test table with your current approach with a data set of 20k entries. The execution of the query took about ~200ms to return. With the schema above the following query executed in about ~1ms
SELECT
*
FROM
Persons AS p
JOIN
PersonCategories AS pc ON p.PersonId = pc.PersonId
JOIN
Categories AS c on pc.CategoryId = c.CategoryId
WHERE
c.Name = 'Senior Citizen'
Why is this query so much faster?
Because we can easily use indices on our columns. In the schema above the Persons.PersonId and Categories.CategoryId are the PRIMARY KEY columns of their tables. So to use them as a column for a JOIN operation has minimal costs. Both columns of the PersonCategories table are FOREIGN KEYS (ensures a valid database state and improves performance). Finally the Categories.Name column has an INDEX too.
Could this approach be bad?
In most cases this is the way to go. One reason not to do it this way, is if you have to handle lots of INSERTS. INSERTS in this schema have a much higher cost because all indices need to be updated after the INSERTS.
I am designing this database and c# app, that a record gets saved to database. now say we have three Sales Person and each should be assigned a record in strict rotation so they get to work on equal amount of records.
What I have done so far was to create one table called Records and one SalesPerson, the Records would have salesperson id as foreign key and another column that would say which agent it is assigned to and will increment this column.
Do you think this is a good design, if not can you give any ideas?
To do this I would use the analytical functions ROW_NUMBER and NTILE (assuming your RDBMS supports them). This way you can allocate each available sales person a pseudo id incrementing upwards from 1, then randomly allocate each unassigned record one of these pseudo ids to assign them equally between sales people. Using pseudo ids rather than actual ids allows for the SalesPersonID field not being continuous. e.g.
-- CREATE SOME SAMPLE DATA
DECLARE #SalesPerson TABLE (SalesPersonID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY, Name VARCHAR(50) NOT NULL, Active BIT NOT NULL)
DECLARE #Record TABLE (RecordID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY, SalesPersonFK INT NULL, SomeOtherInfo VARCHAR(100))
INSERT #SalesPerson VALUES ('TEST1', 1), ('TEST2', 0), ('TEST3', 1), ('TEST4', 1);
INSERT #Record (SomeOtherInfo)
SELECT Name
FROM Sys.all_Objects
With this sample data the first step is to find the number of available sales people to allocate records to:
DECLARE #Count INT = (SELECT COUNT(*) FROM #SalesPerson WHERE Active = 1)
Next using CTEs to contain the window functions (as they can't be used in join clauses)
;WITH Records AS
( SELECT *,
NTILE(#Count) OVER(ORDER BY NEWID()) [PseudoSalesPersonID]
FROM #Record
WHERE SalesPersonFK IS NULL -- UNALLOCATED RECORDS
), SalesPeople AS
( SELECT SalesPersonID,
ROW_NUMBER() OVER (ORDER BY SalesPersonID) [RowNumber]
FROM #SalesPerson
WHERE Active = 1 -- ACTIVE SALES PEOPLE
)
Finally update the records CTE with the actual sales personID rather than a pseudo id
UPDATE Records
SET SalesPersonFK = SalesPeople.SalesPersonID
FROM Records
INNER JOIN SalesPeople
ON PseudoSalesPersonID = RowNumber
ALL COMBINED IN AN SQL FIDDLE
This is quite confusing as I suspect you're using the database term 'record' aswell as an object/entity 'Record'.
The simple concept of having a unique identifier in one table that also features as a foreign key in another table is fine though, yes. It avoids redundancy.
Basics of normalisation
Its mostly as DeeMac said. But if your Record is an object (i.e. it has all the work details or its a sale or a transaction) then you need to separate that table. Have a table Record with all the details to that particular object. Have another table `Salesman' with all the details about the Sales Person. (In a good design, you would only add particular business related attributes of the position in this table. All the personal detail will go in a different table)
Now for your problem, you can build two separate tables. One would be Record_Assignment where you will assign a Record to a Salesman. This table will hold all the active jobs. Another table will be Archived_Record_Assignment which will hold all the past jobs. You move all the completed jobs here.
For equal assignment of work, you said you want circular assignment. I am not sure if you want to spread work amongst all sales person available or only certain number. Usually assignments are given by team. Create a table (say table SalesTeam)with the Salesman ids of the sales persons you want to assign the jobs (add team id, if you have multiple teams working on their own assigned work areas or customers. That's usually the case). When you want to assign new job, query the Record_Assignment table for last record, get the Salesman id and assign the job to the next salesman in the SalesTeam table. The assignment will be done through business logic (coding).
I am not fully aware of your scenario. These are all my speculations so if you see something off according to your scenario, let me know.
Good Luck!
I am receiving a large list of current account numbers daily, and storing them in a database. My task is to find added and released accounts from each file. Right now, I have 4 SQL tables, (AccountsCurrent, AccountsNew, AccountsAdded, AccountsRemoved). When I receive a file, I am adding it entirely to AccountsNew. Then running the below queries to find which we added and removed.
INSERT AccountsAdded(AccountNum, Name) SELECT AccountNum, Name FROM AccountsNew WHERE AccountNumber not in (SELECT AccountNum FROM AccountsCurrent)
INSERT AccountsRemoved(AccountNum, Name) SELECT AccountNum, Name FROM AccountsCurrent WHERE AccountNumber not in (SELECT AccountNum FROM AccountsNew)
TRUNCATE TABLE AccountsCurrent
INSERT AccountsCurrent(AccountNum, Name) SELECT AccountNum, Name FROM AccountsNew
TRUNCATE TABLE AccountsNew
Right now, I am differencing about 250,000 accounts, but this is going to keep growing. Is this the best method, do you have any other ideas?
EDIT:
This is an MSSQL 2000 database. I'm using c# to process the file.
The only data I am focused on is the accounts that were added and removed between the last and current files. The AccountsCurrent, is only used to determine what accounts were added or removed.
To be honest, I think that I'd follow something like your approach. One thing is that you could remove the truncate, do a rename of the "new" to "current" and re-create "new".
Sounds like a history/audit process that might be better done using triggers. Have a separate history table that captures changes (e.g., timestamp, operation, who performed the change, etc.)
New and deleted accounts are easy to understand. "Current" accounts implies that there's an intermediate state between being new and deleted. I don't see any difference between "new" and "added".
I wouldn't have four tables. I'd have a STATUS table that would have the different possible states, and ACCOUNTS or the HISTORY table would have a foreign key to it.
Using IN clauses on long lists can be slow.
If the tables are indexed, using a LEFT JOIN can prove to be faster...
INSERT INTO [table] (
[fields]
)
SELECT
[fields]
FROM
[table1]
LEFT JOIN
[table2]
ON [join condition]
WHERE
[table2].[id] IS NULL
This assumes 1:1 relationships and not 1:many. If you have 1:many you can do any of...
1. SELECT DISTINCT
2. Use a GROUP BY clause
3. Use a different query, see below...
INSERT INTO [table] (
[fields]
)
SELECT
[fields]
FROM
[table1]
WHERE
EXISTS (SELECT * FROM [table2] WHERE [condition to match tables 1 and 2])
-- # This is quick provided that all fields to match the two tables are
-- # indexed in both tables. Should then be much faster than the IN clause.
You could also subtract the intersection to get the differences in one table.
If the initial file is ordered in a sensible and consistent way (big IF!), it would run considerably faster as a C# program which logically compared the files.