Delete duplicate row in Teradata? - sql

This is a tricky one:
How can I delete 1 instance of a duplicate row from a table with only 1 column.
Eg.. Input Table: temp
myColumn
1
2
2
3
4
I want to delete one instance of 2 without using a temporary table.
Output Table:
myColumn
1
2
3
4
NOTE: You cannot use temporary table and SQL should be written in Teradata syntax only.

Related

How can a table column's rows be updated beginning from the first row?

How can an SQL query be written to update a table column's rows beginning from the first row, using a txt or csv file?
For example, before the update, the table might look like
column_1 column_2
1 null
2 null
3 null
then after the update, the desired result would be like
column_1 column_2
1 this
2 that
3 those
I've tried inserting the data, but this only appends it to the bottom of the row, rather than replacing the exiting null values.
With the same example input, this output looks like
column_1 column_2
1 null
2 null
3 null
null this
null that
null those
Load the data into a table.
Then run an update with a join:
update original o
set column_2 = t.column_2
from csv_table t
where o.column_1 = t.column_1;
For your example, though, it might make more sense to truncate the original table and just copy the new data into it.
First, create a file_fdw foreign table for the text file.
Then use INSERT ... ON CONFLICT to update the table. This will update rows already present in the table and add the new ones.

SQL Command to copy data from 1 column in table and 1 column in another table into a new table?

I had to make a new table to get the Include statement working in Entity Framework since EF was looking for a table called be_PostTagbe_Posts. I was using EF Code First from DB. But now the question is about SQL. I added one row of data and now the include works. But what I am looking for is a SQL command that can copy data from 1 column in 1 table and 1 column in another into the new be_PostTagbe_Posts table. In the be_Posts table I need the data in PostRowID to go into be_Posts_PostRowID and PostTagId to go into be_PostTag_PostTagID. Both be_PostTag_PostTagID and be_Posts_PostRowID are in the new be_PostTagbe_Posts table. I am not very good with SQL so not sure how to do this.
Edit: Thanks for the answers. I tried 2 separate queries but only data was inserted into the the be_PostTag_PostTagID while be_PostTag_PostRowID remained null.
And I tried this query which returned The multi-part identifier "be_PostTag.PostID" could not be bound.
INSERT INTO be_PostTagbe_Posts(be_PostTag_PostTagID, be_Posts_PostRowID)
SELECT be_PostTag.PostTagID, be_Posts.PostRowID
WHERE be_PostTag.PostID = be_Posts.PostID
EDIT:
This only inserted half the data - even 2 inserts leave one column null
INSERT INTO be_PostTagbe_Posts (be_Posts_PostRowID)
SELECT PostRowID FROM be_Posts;
INSERT INTO be_PostTagbe_Posts (be_PostTag_PostTagID)
SELECT PostTagID FROM be_PostTag;
And yet management studio tells me the query executed successfully but one column is still null. Weird.
Here are screenshots of the tables:
SELECT PostTagID AS be_PostTag_PostTagID, PostRowID AS be_Posts_PostRowID
INTO be_PostTagbe_Posts
FROM be_PostTag
Inner JOIN be_Posts
ON be_PostTag.PostID=be_Posts.PostID
That command created the new table with the 2 columns populated.
If i understand you ,you want to Copy Table Z's Column A to Table X And Table Z's Column B to Table Y.
If it is so, According to your question it is not clear about Table Structure of TableX and TableY
Assuming TableX And TableY to single ColumnTable [Apart from IdentityColumn] our query will be
INSERT INTO TableX
SELECT ColumnA FROM TableZ
INSERT INTO TableY
SELECT ColumnB FROM TableZ
Rest put your Entire Structure of Table To Get More Help Because These query are on Assumptions
There's not enough information in your question to give you a working example, but this would be the general syntax for INSERTing into a different table using a query SELECTing from two other tables.
INSERT INTO destination_table(wanted_value_1, wanted_value_2)
SELECT table_1.source_field_1, table_2.source_field_1
WHERE table_1.matching_field = table_2.matching_field
There has to be some sort of relationship between the two tables for the WHERE clause to work in that statement. I'm guessing based the little information you provided that there is a PostRowID field somewhere in the table that contains the tags such that your data would look similar to this in the tag table:
PostRowID PostTagID
--------- ---------
1 1
1 2
1 3
1 4
2 1
2 2
3 3
4 4
It sounds like you should use two sql statements:
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`)
select `PostTagID` from POSTTAGIDTABLE
and
Insert into `be_PostTagbe_Posts` (`be_Posts_PostRowID`)
select `PostRowID` from POSTTAGIDTABLE
unless the items have some sort of relationship, then if you have a select statement that will select the merged data in two columns you can just do
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`,`be_Posts_PostRowID`)
(select statement that selects the two items)

Search for value in another table if can not be find in table 1 sql server

I have two tables
Table A
Column 1 Column 2 Column 3 Column 4
12134 3525 KeyNo RefNo
Table B
Column 1 Column 2 Column 4
12134 3525 RefNo
Need to search the value of Column 1 and Column 2 from Table A based on the Column 3
in some cases, the value of Column 1 and Column 2 in Table A is "BLANK" and can be found the same value in Table B based on the Column 4
Please advise with example.
Thank you in advance.
So you are trying to select "column 1" and "column 2" based on "column 4" key either from Table A or Table B?
Use UNION, or 2 consecutive SELECTs.

Update foreign key values in foreign table?

Hi I want to update foreign key values in a table in the following manner:
FK
1 to 5
2 to 6
3 to 7
4 to 1
5 to 2
6 to 3
7 to 4
However if I go through the above sequentially as so:
update table set fk = 5 where fk=1
When I want to update value 5 then id be mixing values with the earlier update statement result which had changed value 1 to 5.
How can I update these values without mixing them with the earlier results.
You could temporarily add a column Updated BIT to the table, and when you update a row's value, set this bit to 1 (True - already updated) - and avoid updating those rows that have already been updated!
Step 1: add new column with values 0 (false - not updated yet):
ALTER TABLE dbo.YourTable
ADD Updated BIT NOT NULL DEFAULT(0) WITH VALUES
Step 2: do your updates - respect the Updated flag!
UPDATE dbo.YourTable
SET fk = 5, Updated = 1
WHERE fk = 1 AND Updated = 0
Step 3 : once everything is updated - drop the column again
ALTER TABLE dbo.YourTable
DROP COLUMN Updated
You could use an additional column as FreshPrince suggested. Add new column, set it to the appropriate values, and change the key to the new column.
Another approach may be to use a set of values outside that range. This would eliminate the need to add and remove columns. So...
update table set fk = 15 where fk=1
1 to 15
2 to 16
3 to 17
4 to 11
5 to 12
6 to 13
7 to 14
And then a straight update to subtract 10 from all rows.
update table set fk = fk - 10
You will need to remove and reinstate the foreign key constraint as well.
You could also use a CASE expression to specify the new values in a single UPDATE statement:
UPDATE YourTable
SET fk=CASE fk
WHEN 1 THEN 5
WHEN 2 THEN 6
WHEN 3 THEN 7
WHEN 4 THEN 1
WHEN 5 THEN 2
WHEN 6 THEN 3
WHEN 7 THEN 4
END
WHERE fk IN (1,2,3,4,5,6,7)
In this particular case, you could also use:
UPDATE YourTable
SET fk=(fk+3)%7+1
WHERE fk BETWEEN 1 AND 7
Razvan

Only the latest data show on table

How to create table or maybe trigger that doing like this below?
Date Data
2/6/2013 2
2/6/2013 1
2/6/2013 3
2/6/2013 1
2/6/2013 0
The table at top will alternately as input for next table below, but only the last input will show at table below. The input will alternately from 2 until 0
Just say the input start from 2 - 1 - 3 - 1 - 0, and only 0 will show on table. How to make the trigger? 0 only data, not something unique. Only the latest input will show on table. Remember the table below triggered table at top to take the data.
I just want every time new input inserted, the data that already existed delete and only the new input will show
Data
0
I think you just want a VIEW that shows the latest value.
The following would be the query you'd use for your view...
SELECT TOP 1 Data FROM Table ORDER BY Date DESC
EDIT To do exactly what you want you'd just need a stored procedure for inserting.
-- Copy old value into history
INSERT INTO History SELECT * From Table WHERE yourCondition = True
-- Delete old value
DELETE FROM Table WHERE yourCondition = True
-- Insert new value
INSERT INTO Table VALUES(#Date, #Data)
Now Table will contain only 1 row with the latest value, while History will contain all the old values. This is completely non-standard and irrational, but should be exactly what you want. If you don't need history you can just remove the first INSERT statement.