Subtract today's date with date column in SQL [closed] - sql

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 2 years ago.
Improve this question
I have a column in SQL item_expired, this column stores data in the form of dates from other items. What I want to ask, how do I subtract existing date in item_expired column with the current date, which will then be stored in the remaining_time column.
I've tried the command below, but isn't working:
SELECT
item_description, item_expired,
item_date = DATEDIFF(DAY, CURRENT_TIMESTAMP, item_expired)
FROM Customers;

As GMB has mentioned in the comment above, date functions depend on the database type you are using. Assuming you are asking about MySQL the below query will provide you with the remaining time required.
select
(CURRENT_DATE - item_expired) as remaining_time
from customers;
While this will provide you with the remaining time, you will have to write a separate insert to insert the remaining time for the table column.

Query in sql server :
select DATEDIFF(day, CURRENT_TIMESTAMP,item_expired)
will give you the difference in days between the current date and the item_expired date
Provide more information about you problef for more direct answers

Related

SQL Case Statement or Sub-Query [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
In the application we have a query, we have a situation where we are bringing records back that are open, closed, and archived with a date associated with them. This is a table associated and joined with a main table. The table could have 1 to 3 records associated with the same ID of the main table depending if the record has been opened, closed, and/or archived. The three stages essentially of open, closed, and archived.
What we're looking to do is this: When EStatusID = 1 (Which means open) we need the DateClosed to read as blank (because it's not closed or archived yet)
SELECT
E.EID,
EStatus.EStatusID,
FORMAT (EStatus.DateCreated, 'MM/dd/yyyy') as DateClosed
I won't bore you with the rest of the query because it's long and not useful to the question. So we need some kind of Case statement or sub query or something in the Select to accomplish this task.
You can use a case expression:
CASE WHEN EStatus.EStatusID <> 1 THEN FORMAT (EStatus.DateCreated, 'MM/dd/yyyy') END
AS DateClosed,

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

Find SQL table entry with a check on created at 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 SQL database table that has one of the columns as "created_at". The column contains the date and time on which the entry was created.
The format of the column is as follows:
2019-07-14 11:36:15.000
How can I run a query which checks if this entry was created within the past 15 minutes from the current time?
In Standard SQL -- assuming that created_at has a datetime/timestamp type, you would use something like:
where created_at > current_timestamp - interval '15 minute'
That said, the date/time functions are notoriously database-dependent. So the exact syntax might vary in your database.
Solution in SQL Server will be:
SELECT *
FROM YourTable
WHERE created_at >= DATEADD(minute, -15, created_at );

How can I find duplicate records in clickhouse [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 2 years ago.
Improve this question
I want to know how I can find duplicate data entries within one table in clickhouse.
I am actually investigating on a merge tree table and actually threw optimize statements at my table but that didn't do the trick. The duplicate entries still persist.
Preferred would be to have a universal strategy without referencing individual column names.
I only want to see the duplicate entries, since I am working on very large tables.
The straight forward way would be to run this query.
SELECT
*,
count() AS cnt
FROM myDB.myTable
GROUP BY *
HAVING cnt > 1
ORDER BY date ASC
If that query gets to big you can run it in pieces.
SELECT
*,
count() AS cnt
FROM myDB.myTable
WHERE (date >= '2020-08-01') AND (date < '2020-09-01')
GROUP BY *
HAVING cnt > 1
ORDER BY date ASC

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