I would like to know how we can select the two last year in more the current year. For example, If I have a table like it : Then I want it : I tried to use dateadd like it : But I just got the 2 last year since now, I would like to got 2019, 2020 and 2021
Tag: sql
How to update several tables with a result query?
I am working with SQL Server 2017, and I need to clean up duplicate rows and update all rows in other tables that contain my field. I’ve got one table which contains my customers Then I have 7 tables that contains the userid column and 1 table with another name column My other tables: I want to clean th…
DELETE By Procedure With String Input – Oracle
I am writing a procedure to delete some records of z_names table by IDs from the input as string (and I need to seprate them by special character (comma ,)). Sample Input: ‘1,4’ z_names table: ID NAME 1 jim 2 john 3 jack 4 alex After running this procedure, the record with ID 1 and 4 (jim and alex…
how can i loop insert query?
I have a problem like this: I need to optimize the application, with db (postgreSQL), the table looks like this: I have more than a thousand such voters, and I need to put all of them in the database, but among them there are several duplicates that could vote several times (from 2 to infinity), and I need to…
SQL help – Trying to tag values which complement the same number with opposite sign
I am trying to flag the numbers in a column which has both positive and negative numbers and adds to zero if summed up. I would like to do this by partitioning one table into multiple chunks based on Account column in the table. I have posted a picture to further explain how my result (Flag column) is expecte…
Sql Join selecting records from A table and selecting matching records with a condition from B table
We have Table A and Table B, Data we need is in table A and we use table B to validate case where we have matching Ids. If ids do not match we can select it without validation, but if they match we need to check if date is in between date1 and date2. We only check records in B
Sql Query To Sum Amount Before the Date And After Between Dates
I am looking for a SQL query to sum the amount before the date and after between dates. I have the following table in my database. Now what I want to find out is the total sum (DebitAmount) – Sum (CreditAmount) that is before 2021 and then from 2021-01-01 to 2021-03-01: Table Script The Query I Have tri…
How to print a variable in SQL stored procedure?
I want to print the a variable in the above code. But the code returns How to fix this? Answer You can use an OUT variable ✓ ✓ | answer | | :—– | | yes | db<>fiddle here* Or simply don_’t use INTO or := | count(amount) | | ————: | | 2 | ✓ | answer | |
Bring a row for each specific column that is not empty, with the column name
I’ve this table with the following data: ID Data1 Data2 Data3 Data4 Data5 Data6 RandomInformation 1 Test1 Test2 Test3 Information1 2 Test5 Test6 Test7 Information2 3 Test8 Test9 Test10 Test11 Information3 The resultant table i want should be as below: ID Data RandomInformation 1 Data1 Information1 1 Dat…
How to define INNER JOIN in SQL
I have a problem how to define INNER JOIN in SQL. The problem is that I have a table Names with columns ID and Name. The other table has column PersonID. Sometimes value in column PersonID is 0, and in Names table there is no row with a column value of 0. I have created a view, and I am