Replace blank with another column value in SSIS derived column Transformation - ssis

I need to replace the blank value with the column Data2
Original Table
+-------+--------+
| data1 | Data2 |
+-------+--------+
| 45789 | 45789 |
| 56897 | 56897 |
| | 56897 |
| 56485 | 56485 |
| | 897458 |
+-------+--------+
Expected Table
+-------+--------+
| data1 | Data2 |
+-------+--------+
| 45789 | 45789 |
| 56897 | 56897 |
| 56897 | 56897 |
| 56485 | 56485 |
| 897458| 897458 |
+-------+--------+
I tried this not working
data1 == "" ? data1 : data2

Your expression is true if two columns have the same data type. If data types are different, you need to cast the data2 column.
Enable data viewer to check your result, It works properly:
You can also use another way. Write a TSQL query to replace the Data1 column with Data2 Column
SELECT
CASE WHEN Data1 = '' THEN Data2 ELSE Data1 END Data1
,Data2
FROM YourTable

Related

How to rename a column name of a select statement with a result from another table

I've been searching for an answer to this for a while and am just not successful. Sorry if this is a duplicate.
I have two tables:
table1
------------------------
| id | name |
------------------------
| c1 | name1 |
| c2 | name2 |
| c3 | name3 |
| c4 | name4 |
------------------------
table2
------------------------------------------------
| id | c1 | c2 | c3 |
------------------------------------------------
| 1 | data11 | data21 | data31 |
| 2 | data12 | data22 | data32 |
------------------------------------------------
I need data from the second table, renaming the columns with the contents of the first table like this. The number of columns might not match.
result
------------------------------------
| id | name1 | name3 |
------------------------------------
| 1 | data11 | data31 |
| 2 | data12 | data32 |
------------------------------------
[Edit] I am selecting data from the second table like this:
"SELECT c1,c3 FROM table2 WHERE id ..." so I don't "know" the names when I do the select.
First I was thinking it would be a simple JOIN, but obviously it's not. I'm kind of stuck right now wondering how to approach this.
Thanks

Compare values of two database tables -> MySQL

I looked through various topics here on StackOverflow but I was unable to find the right solution for my problem. Here is what I´ve got:
I have two database tables. One table should be some kind of a reference model and looks similar to this example:
+----+----------------+----------------+----------------+-----------------+
| ID | FIELD1 | FIELD2 | FIELD3 | FIELD 4 |
+----+----------------+----------------+----------------+-----------------+
| 1 | Value1_Field_1 | Value1_Field_2 | Value1_Field_3 | Value1_Field_4 |
| 2 | Value2_Field_1 | Value2_Field_2 | Value2_Field_3 | Value2_Field_4 |
| 3 | Value3_Field_1 | Value3_Field_2 | Value3_Field_3 | Value3_Field_4 |
| 4 | Value4_Field_1 | Value4_Field_2 | Value4_Field_3 | Value4_Field_4 |
| 5 | Value5_Field_1 | Value5_Field_2 | Value5_Field_3 | Value5_Field_4 |
+----+----------------+----------------+----------------+-----------------+
Now I enter new data to a second table. Those data can have the same values and also the same amount of rows. But it can also happen, that some data have more rows or the values inside the rows are different. Here is another table example where I have one row more and two values are different:
+----+----------------+-----------------+-----------------+----------------+
| ID | FIELD1 | FIELD2 | FIELD3 | FIELD 4 |
+----+----------------+-----------------+-----------------+----------------+
| 1 | Value1_Field_1 | Value1_Field_2 | Value1_Field_3 | Value1_Field_4 |
| 2 | Value2_Field_1 | Value2_Field_2 | Value2_Field_3 | Value2_Field_4 |
| 3 | Value3_Field_1 | Value3_Field_2 | Value3_NEWVALUE | Value3_Field_4 |
| 4 | Value4_Field_1 | Value4_Field_2 | Value4_Field_3 | Value4_Field_4 |
| 5 | Value5_Field_1 | Value5_NEWVALUE | Value5_Field_3 | Value5_Field_4 |
| 6 | Value6_Field_1 | Value6_Field_2 | Value6_Field_3 | Value6_Field_4 |
+----+----------------+-----------------+-----------------+----------------+
I am looking for a SQL-Statement which compares those two tables and list all different records. In my example above the SQL-Statement should retourn those informations:
+----+----------------+-----------------+-----------------+----------------+
| ID | FIELD1 | FIELD2 | FIELD3 | FIELD 4 |
+----+----------------+-----------------+-----------------+----------------+
| 3 | | | Value3_NEWVALUE | |
| 5 | | Value5_NEWVALUE | | |
| 6 | Value6_Field_1 | Value6_Field_2 | Value6_Field_3 | Value6_Field_4 |
+----+----------------+-----------------+-----------------+----------------+
Here is what I´ve tried so far:
SELECT distinct FIELD1, FIELD2, FIELD3, FIELD4 from table_references
union
SELECT distinct FIELD1, FIELD2, FIELD3, FIELD4 from table_new_data
That statement returns all rows but only once. This is not what I am looking for. I also tried it with this code:
SELECT FIELD1, FIELD2, FIELD3, FIELD4
FROM (
SELECT FIELD1, FIELD2, FIELD3, FIELD4 FROM table_references
UNION ALL
SELECT FIELD1, FIELD2, FIELD3, FIELD4 FROM table_new_data
) tbl
GROUP BY FIELD1
HAVING count(*) = 1
ORDER BY FIELD1
That code only returns row 6 but does not show me the new values inside ID 3 & 5.
Any help would be really appreciated. Thanks in advance!
EDIT:
According to #Madhur Bhaiya solution I made a mistake. The output should look like this:
+----+----------------+-----------------+-----------------+----------------+
| ID | FIELD1 | FIELD2 | FIELD3 | FIELD 4 |
+----+----------------+-----------------+-----------------+----------------+
| 3 | Value3_Field1 | Value3_Field2 | Value3_NEWVALUE | Value3_Field4 |
| 5 | Value5_Field1 | Value5_NEWVALUE | Value5_Field3 | Value5_Field4 |
| 6 | Value6_Field_1 | Value6_Field_2 | Value6_Field_3 | Value6_Field_4 |
+----+----------------+-----------------+-----------------+----------------+
So I also need all values from the affected row where a value is different.
We can use LEFT JOIN to compare the two tables and then use conditional functions like If() to get values accordingly:
Query
SELECT tnew.*
FROM table_new_data AS tnew
LEFT JOIN table_references AS told
ON told.ID = tnew.ID
WHERE (told.ID IS NOT NULL AND
(tnew.FIELD1 <> told.FIELD1 OR
tnew.FIELD2 <> told.FIELD2 OR
tnew.FIELD3 <> told.FIELD3 OR
tnew.FIELD4 <> told.FIELD4)
) OR
told.ID IS NULL;
Result
| ID | FIELD1 | FIELD2 | FIELD3 | FIELD4 |
| --- | -------------- | --------------- | --------------- | -------------- |
| 3 | Value3_Field_1 | Value3_Field_2 | Value3_NEWVALUE | Value3_Field_4 |
| 5 | Value5_Field_1 | Value5_NEWVALUE | Value5_Field_3 | Value5_Field_4 |
| 6 | Value6_Field_1 | Value6_Field_2 | Value6_Field_3 | Value6_Field_4 |
View on DB Fiddle
I believe instead of a Union Clause, you are looking for for the Intersect Clause. Union will report everything in the two tables, once(per the distict modifier). Intersect will only report the commonalities.

Transpose column data to rows based on common ID in one column using SQL

How can I transpose column data to rows based on common ID in one column using SQL.
for example
I have the given data to be,
| ID | data |
|----|------|
| 1 | d1 |
| 2 | d2 |
| 3 | d3 |
| 1 | d4 |
| 2 | d5 |
I need,
| ID | data |
|----|---------|
| 1 | d1 | d4 |
| 2 | d2 | d5 |
| 3 | d3 |
How can I achieve this in SQL
You would use group_concat():
select id, group_concat(data separator ' | ') as data
from t
group by id;

Get output based on the values of one column

I have a table where the column and data is like this
| ID | Prop | Value | String |
| 1 | C | 1 | NULL |
| 1 | Notes | NULL | TestNote |
| 1 | E | 59 | NULL |
| 1 | Notes | NULL | Note2 |
| 2 | C | 1 | NULL |
What I'm trying to do is get both the Notes and C rows on the Prop column
I've tried
SELECT * FROM table WHERE Prop IN ("C", "Notes")
The output of this is
| ID | Prop | Value | String |
| 1 | C | 1 | NULL |
| 2 | C | 1 | NULL |
It only outputted one column. This is the same output when I use 'OR'
Then I tried
SELECT * FROM table WHERE Prop = "C" AND Prop = "Notes"
But i got an empty result, If i do separate select statements like
SELECT * FROM table WHERE Prop = "C"
And then do another one on a separate window
SELECT * FROM table WHERE Prop = "Notes"
It works but not how I need it to work.
The output I'm hoping to get is like this:
| ID | Prop | Value | String |
| 1 | C | 1 | NULL |
| 1 | Notes | NULL | TestNote |
| 1 | Notes | NULL | Note2 |
| 2 | C | 1 | NULL |
Any idea how I can get it to work?
why are you not using "OR" in Between ?
SELECT * FROM table WHERE Prop = 'C' OR Prop = 'Notes'
The condition Prop = "C" AND Prop = "Notes" will never be true and so will not work.
The below query as posted should work just fine
SELECT * FROM table WHERE Prop IN ("C", "Notes")
If it's not working then it may be the case that the values are case sensitive.
Use LOWER() function along in your query like
SELECT * FROM table WHERE LOWER(Prop) in ('c','notes');
(OR) as a alternative use UNION
SELECT * FROM table WHERE Prop = 'C'
UNION
SELECT * FROM table WHERE Prop = 'Notes'

Fill a partially filled table MYSQL

I'm trying to fill a partially filled table in mysql, I don't know if it possible though, I couldn't find a way to do it without losing data.
Since it's always easier to explain with an example. I have something like this:
+-------+-------+----------+
| Names | total | elements |
+-------+-------+----------+
| data1 | 2 | elem1 |
| | | elem2 |
| data2 | 3 | elem3 |
| | | elem4 |
| | | elem5 |
+-------+-------+----------+
And I'm trying to get something like this :
+-------+----------+
| Names | elements |
+-------+----------+
| data1 | elem1 |
| data1 | elem2 |
| data2 | elem3 |
| data2 | elem4 |
| data2 | elem5 |
+-------+----------+
Is it possible?
You won't be able to do this nicely with just a basic query, but this is the sort of thing procedure logic can be used for.
Check out the docs on WHILE loops. What you'll want to do is loop through that table ordered by the elements column. If the Names column is set, set a variable to the value of names, and if names is empty, set the column to that variable.
The example here should get you started:
http://dev.mysql.com/doc/refman/5.0/en/while.html