Merge statement in Teradata - teradata

Is it the right merge statement? If it is not correct, please advise me.
MERGE INTO Table1 tgt
USING Table2 src
ON (src.ec tgt.ec
AND src.yyyymm = tgt.yyyymm)
WHEN MATCHED THEN
UPDATE
SET
column3 = src.column3,
column5 = src.column5
WHEN NOT MATCHED THEN
INSERT
( ec, yyyymm, column1, column2, column3, column4, column5, column6)
VALUES (
src.ec, src.yyyymm, column1 = null, column2 = 0,
src.column3, column4 = 0, src.column5, column6 = null
);

Related

Update table if any value =

I'm trying to update my table if any value from table1 = value from table2
Table1 as 1 column with data
Table2 as many columns with data
If table2(data) = table1(data) update
But isn't working
I had one code that was working if i set table2 with 1 column
This one is working but table2 needs to have 1 column only
UPDATE table1
SET column1 = 'correct'
WHERE column2 in (SELECT column1 from table2);
I want to be able to do having more column
maybe something like this:
UPDATE
SET column1 = 'correct'
WHERE column2 in (SELECT * from table2);
The error:
Result: sub-select returns 11 columns - expected 1
How should I do it?
Maybe you can do it with EXISTS:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn = table2.someothercolumn
);
If you want to check table1.someothercolumn against multiple columns of table2:
UPDATE table1
SET column1 = 'correct'
WHERE EXISTS (
SELECT 1 FROM table2
WHERE table1.somecolumn in (table2.col1, table2.col2, ...)
);

SQLITE UPDATE query replicating rows

I have an update query that when I run it instead of matching each unique row it is replicating the same row through the entire column.
Any help would be massively appreciated.
I have got
UPDATE Table1
SET Column1 = (SELECT Column1 FROM [Table2] WHERE Column2 = [Table2].Column2)
[Table2].Column2 referes to the column in Table2.
Column2 refers to the column in some table that has such a column. The innermost such table is Table2.
So this ends up being the same as Table2.Column2 = Table2.Column2.
To refer to the column in Table1, specify that table:
UPDATE Table1
SET Column1 = (SELECT Column1
FROM Table2
WHERE Table2.Column2 = Table1.Column2);

SQLite select where all columns are not null

I have column1, column2, column3, column4, column5 and so on.
If I know that one column is not null, I can use this query:
SELECT *
FROM table
WHERE columnA IS NOT NULL;
But now I want to select rows where all the columns are not null.
How to write is query easily?
Use the query look like this
SELECT *
FROM table_name
WHERE NOT column1 IS NULL AND NOT column2 IS NULL...

SQL subtraction code works in SQL but not in sqldatasource

I'm scratching my head over this one as I can get this code to work in SQL but when I transfer it to a simple gridview the code errors out stating 'Invalid Object name tablename'.
What I'm attempting to do is take a static number and subtract it from three different columns in the same table.
This is what I have that works within SQL:
SELECT
(3000)
-
(SELECT COUNT(column1) from table1 where column1 = 'Agreed')
+
(SELECT COUNT(column2) from table1 where column2 = 'Agreed')
+
(SELECT COUNT(column3) from table1 where column3 = 'Agreed')
AS subtract
I've tried moving the static total around as such
(3000)
-
SELECT
(SELECT COUNT(column1) from table1 where column1 = 'Agreed')
+
(SELECT COUNT(column2) from table1 where column2 = 'Agreed')
+
(SELECT COUNT(column3) from table1 where column3 = 'Agreed')
AS subtract
And....
'3000'
-
SELECT
(SELECT COUNT(column1) from table1 where column1 = 'Agreed')
+
(SELECT COUNT(column2) from table1 where column2 = 'Agreed')
+
(SELECT COUNT(column3) from table1 where column3 = 'Agreed')
AS subtract
But both return syntax errors in SQL.
What I'm hoping to get is the remainder of taking the totals of the three select statements from the static number.
Am I missing something simple here? I'm stumped as to why this would work within SQL but not when I transfer the code to a gridview.
::edit::
See Answer below for the solution. Had to re-write the code to get it to work.
SELECT
(3000)
-
a.c
+
b.c
+
c.c
from (SELECT COUNT(*) AS c from table1 where column1 = 'Agreed') a
left join (SELECT COUNT(*) AS c from table1 where column2 = 'Agreed') b on 1=1
left join (SELECT COUNT(*) AS c from table1 where column3 = 'Agreed') c on 1=1
The first and second queries are not the same at all :
SELECT 3000 - 10
Is not the same that :
3000 - SELECT 10
The second one is not valid because it do not begin by the SELECT statement (as you did in the first). It is not a ASP.NET specifiec issue.
EDIT :
What about this query :
SELECT
(3000)
-
a.c
+
b.c
+
c.c
from (SELECT COUNT(*) AS c from table1 where column1 = 'Agreed') AS a
left join (SELECT COUNT(*) AS c from table1 where column2 = 'Agreed') AS b on 1=1
left join (SELECT COUNT(*) AS c from table1 where column3 = 'Agreed') AS c on 1=1

Copy a subset of column data from one table to another

I have two tables with identical schema. Let's name them TestTable and TestTableTemp. I need to copy just two columns from TestTableTemp to TestTable without disrupting other data. The rows in TestTable are a subset of those in TestTableTemp. Let's say the columns that I need to copy are named Column1 and Column2 and that they have identical primary keys reference by column primaryKey.
In mysql I believe this could be done as such or something similar:
UPDATE TestTable, TestTableTemp
SET TestTable.Column1 = TestTableTemp.Column1, TestTable.Column2 = TestTableTemp.Column2
WHERE TestTable.primaryKey = TestTableTemp.primaryKey
Sqlite does not allow for multiple tables to be defined on the update statement as can been seen in their reference data here: http://www.sqlite.org/lang_update.html
The best I could come up with is such:
UPDATE TestTable SET
Column1 = (select TestTableTemp.Column1 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey),
Column2 = (select TestTableTemp.Column2 from TestTableTemp, TestTable where TestTable.primaryKey = TestTableTemp.primaryKey)
WHERE EXISTS(select * from TestTableTemp where TestTable.primaryKey = TestTableTemp.primaryKey"
This gives me a syntax error near "." I am guessing this is because I cannot reference TestTable in the scalar expressions.
Can anyone point me in the right direction? Any help is much appreciated.
EDIT:
I cleaned up the second query a bit. It seems to just set the Column1 and Column2 to the first row from that column from TestTableTemp.
Your original query for comparison:
UPDATE TestTable, TestTableTemp
SET TestTable.Column1 = TestTableTemp.Column1
, TestTable.Column2 = TestTableTemp.Column2
WHERE TestTable.primaryKey = TestTableTemp.primaryKey
Here is the working query (I just slightly changed your version):
http://sqlfiddle.com/#!5/f3a19/9
UPDATE TestTable
SET
Column1 = ( SELECT TestTableTemp.Column1
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
,Column2 = ( SELECT TestTableTemp.Column2
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
WHERE EXISTS( SELECT NULL
FROM TestTableTemp
WHERE TestTableTemp.primaryKey = TestTable.primaryKey )
;

Resources