SQL incremental data [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 1 year ago.
Improve this question
i am trying to get only incremental records on one of the table SQL.
Example Table1--need to insert data 10 times per day from some source data. Suppose I have inserted 10 records in the morning. Another new records came after one hour. SO total will be 20 records. And it keep going more and more through all day. And I need to run script to show only increment part. SO whatever I show in the morning should not include next run. Basically each time main table updated , i need to run script to show only updated new rows not old exiting rows.

Add a batch number to to the data and then you can query the data only returning the latest batch. Something like this.
During Insert
DECLARE #Batch INT = (SELECT MAX(BatchNo)+1 as NextBatchNo FROM myTable)
IF #Batch IS NULL
SET #Batch = 1
INSERT INTO myTable (firstColumn, secondColumn, anotherColumn, BatchNo)
SELECT firstColumn, secondColumn, anotherColumn, #Batch
FROM mySourceDataTable
To get the latest rows inserted
SELECT * FROM myTable
WHERE Batch = (SELECT MAX(Batch) FROM myTable)

Query the table to display output for the last 24 days, using the Date function.

If you have oracle database then you can use flashback feature to get this data.
Link for more details
https://docs.oracle.com/cd/B19306_01/backup.102/b14192/flashptr002.htm

Related

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
...

Delete two physical tables and insert those to separate two temp tables at once using SQL [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'm having a SQL query which deletes records from a physical tables (header, line) and insert those records to a 2 separate temp table using OUTPUT DELETED.*.
DELETE FROM [dbo].[PartnerFilteredLines]
OUTPUT DELETED.*
INTO #PartnerFilteredLines
WHERE SettingsFileId=#SettingsSplitId
DELETE FROM [dbo].[PartnerFilteredHeader]
OUTPUT DELETED.*
INTO #PartnerFilteredHeader
WHERE SettingsFileId=#SettingsSplitId
Both 2 queries are working independently. My requirement is to somehow merge those 2 queries and do the delete part at one time (one execution) because for a development requirement this query is scheduled for 1 second. Therefore, sometimes #PartnerFilteredLines records are there and #PartnerFilteredHeader are not because they were executed in 2 executions.
Can somebody help me to achieve this?
Thank you
You can use transactions to hold lock for table PartnerFilteredHeader
CREATE PROCEDURE ...
AS
BEGIN
BEGIN TRANSACTION
-- lock table "PartnerFilteredHeader" till end of transaction
SELECT ...
FROM PartnerFilteredHeader
WITH (TABLOCK, HOLDLOCK)
DELETE FROM [dbo].[PartnerFilteredLines]
OUTPUT DELETED.*
INTO #PartnerFilteredLines
WHERE SettingsFileId=#SettingsSplitId
DELETE FROM [dbo].[PartnerFilteredHeader]
OUTPUT DELETED.*
INTO #PartnerFilteredHeader
WHERE SettingsFileId=#SettingsSplitId
-- release lock
COMMIT TRANSACTION
END

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

procedure to delete data from table when data in a row exceeds than 30 thousand in ms sql [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 6 years ago.
Improve this question
I want to create a stored procedure to delete data from table when data in a row exceeds than 30 thousand and before deleting the data it will create a backup of database and then it will delete the data.Any help will be greatly appreciated.
Try this below trigger it may help you.
Below trigger will delete the top 1000 old records in your table whenever
the row count exceeds 30000 (Change the delete count in trigger as per your need)
create trigger Records_delete on Test_table
after insert
as begin
declare #count bigint
select #count= COUNT(*) from Test_table
--select #count
IF #count>30000
BEGIN
;WITH CTE AS(
SELECT TOP (1000) * FROM Test_table ORDER BY Test_table_nbr
)
DELETE FROM CTE
END
end
Note: Your table count never exceeds more than 30000 after creating this trigger on your table
You need Partitioned Tables
Also check this Partitioned Table and Index Strategies Using SQL Server

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