Picking unique records in SQL [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Say I have a table with multiple records of peoples name, I draw out a prize winner every month. What is a query in SQL that I can use so that I pick up a unique record every month and the person does not get picked on the next month. I do not want to delete the record of that person.

create a new column as a flag named anything like 'prizeFlag' make it boolean take only 0 and 1 or anything as you like, make it's default value is 0 means not get a prize yet
when you select a random column update this filed with 1 means take a prize
when you select a random column next month, Add a condition in WHERE Clause say the prizeFlag not equal 1 to avoid duplication

One should store whether a person has already won. A date would make sense to allow people after say 10 years to win again.
ALTER TABLE ADD won DATE;
A portable way would be to use a random number function outside the SQL.
SELECT MIN(id), MAX(id), COUNT(*) FROM persons
With a random number one can get the next valid ID.
SELECT MIN(ID) FROM persons WHERE won NOT IS NULL AND ID >= ?
int randomno = minId + new Random().nextInt(maxId - minId);
preparedStatement.setInt(1, randomno)
UPDATE persons SET won = CURRENT_DATE WHERE ID = ?

Related

Sql instance number in new column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a table with userid in one column, purchase date in second column, and purchase item in the third. I ordered the table by purchase date and want to make a fourth column to record the count instance number of the user. See below for example.
enter image description here
Eventually I want to create a table that shows how much each user bought during the purchase. For example their first purchase they spent 10 second purchase 20 third purchase 30.
What you're looking for is the window function row_number.
select
*,
row_number() over(
partition by userid
order by purchasedate
) as instance
from that_table

Invalid Column name 'Price' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to do a SQL If-Else Statement in SQL Server but my query displayed a error which is Invalid Column Name 'Price' but I have that column on my table. Do I need to declare it in the If-Else Statement? Here is my query:
IF Price > 2000000
BEGIN
PRINT 'Wow! The Sales amount which is 3,000,000 is much higher than the 2,000,000 amount of Year 2018';
END
ELSE
BEGIN
PRINT 'The Sales amount did not surpass the 2,000,000 of 2018';
END
I have two columns. The column names are Year and Price. I have 2018 and 2019 for my Year column. I have 2,000,000 and 3,000,000 for my Price Column.
If Price is a column in your table, you need to reference that table in some way. I'm not sure how you're looking to use Price specifically but here are a couple of possibilities.
If you're just looking for at least one record in that table where Price>2000000 then you could do something like this.
IF EXISTS (SELECT 1 from tbl WHERE Price>2000000)
BEGIN
...
Or if you want to sum that column, you could add the sum to a variable and then check the variable.
declare #price INT
select #price=sum(Price) from tbl
IF #price>2000000
BEGIN
...

Calculate number of exercises in each category in access 2016 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have two tables such as: Category and Exercise. In the first of them I have field names such as: CategoryID and CategoryName. In the second table there are field names such as: ExerciseID Exercise and CategoryID. I have to calculate the number of exercises in each category. For the exercises with provided data I have:
addition, subtraction, multiplication, division, enhancemen,
running, jumping, pole vault, shot put, literature, grammar.
The query has to output me:
Maths = 5
English = 2
PE = 4
How would I be able to calculate this within query in ms-access?
You should have a key to join the tables.
In addition to ExerciseID and Exercise, have a CategoryKey column which you can join to the Category table through CategoryID. Then you'd just query SELECT COUNT(*) as count FROM Exercise WHERE CategoryKey = 1 where 1 is the CategoryID you want to count. You can also do more complex joins in the future if you have new requirements.
Otherwise, you'll have to write a giant goofy sql switch statement, which I don't have much experience with, because it's usually not something you need to use if your table is structured correctly.

SQL Server : find rows that specific column value not change in last 24 hour [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is my sample table
I want get last row of BB because money column of BB not changed in last 24 hour ...
What is the right query for this in SQL Server ?
SELECT Early.*
FROM sampleTable Early
JOIN sampleTable Older
ON Early.Name = Older.Name
AND Early.Money = Older.Money
AND DATEDIFF ( day , Older.time, Early.time) >= 1
Take note if only one record, there is nothing to compare and wont appear on the query.
You have to make a "after update" trigger and in the trigger insert the records to temp_table including a update datetime column. This will allow to track each update for each row.
This is MySQL trigger example
MySQL
create trigger ai_table_trigger for each row
begin
insert into temp_table(record_id,update_datetime)values(old.id,now());
end

Need to Create a Table that has references another table and has variables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to create a table that has the following features:
An employee number that references the employee number on the employee table
I need to add a salary field
the salary is based upon hire date which is also in the employee table
if employee is hired less then 3 years then salary would be 30k
less than 5 then it is 45 k
and 5-10 55 k more than 10 60 k
it needs to have tax info where if the employee lived in Alaska, there is no tax otherwise tax by state
this is also a field on the employee table
the net pay is 90% if the person has no tax and 75 % if they have a tax
pay date must be a future date
check number must not be null or duplicate
Any help will be appreciated as I'm not sure where to begin. A UDF function? Copy the employee table somehow and add the fields I need?
First, locate the period (.) key on your keyboard. You will need it for this solution (as well as for posting future questions at Stackoverflow).
To solve your problem you should create a VIEW that JOINs together your employee and tax info tables and includes calculated columns in the output to get determine the salary, tax status, and net pay. I would attempt to give you an approximate version of the CREATE VIEW statement you need, but you've given no information about your table structures or column names.