SQL: add string to field when other value changes - sql

I have 2 columns (ColA, ColB) both are datatype Nvarchar.
I need col B to be unique for the values in ColA
Some rows have 2 different values in ColA with the same value (non-unique) in ColB.
I want to write a query to display the ColB values along with the letter "D" (as duplicate) at the end, where the value in ColA is changing to something else.

As far as I can understand, you want to mark the rows where the ColB value needs to be changed.
If so try something like this
SELECT t1.COLA,
t1.COLB + 'D'
FROM TABLE1 t1
INNER JOIN TABLE1 t2
ON t1.COLB = t2.COLB
AND t1.COLA != t2.COLA
This will only show the "double" rows. If you want to show all the rows, just add a UNION to the end:
SELECT t1.COLA,
t1.COLB + 'D'
FROM TABLE1 t1
INNER JOIN TABLE1 t2
ON t1.COLB = t2.COLB
AND t1.COLA != t2.COLA
UNION
SELECT *
FROM TABLE1
WHERE COLA NOT IN (SELECT t1.COLA
FROM TABLE1 t1
INNER JOIN TABLE1 t2
ON t1.COLB = t2.COLB
AND t1.COLA != t2.COLA)
See the full example at SQL Fiddle.

Related

sql server compare two column from different table

I have two table containing one columns in both table
table1
item
table2
item
I want a boolean result true/false if all the values in item column of table1 and table2 are exact match.
e.g columns values for both table
item item
apple apple
boy boy
cat cat
this should return true
item item
apple apple
boy boy
cat dog
should return false
You could check if records exist on one side or the other only by joining the two tables left-to-right and right-to-left. If there are any records not matched, it's false, otherwise true.
select case when exists (
select top 1 1
from Table1 t1
left join Table2 t2 on t2.item = t1.item
where t2.item is null
union all
select top 1 1
from Table2 t2
left join Table1 t1 on t1.item = t2.item
where t1.item is null
)
THEN 'false'
ELSE 'true'
END as result
You can use full join and aggregation:
select (case when count(*) = 0 then 'true' else 'false' end)
from t1 full join
t2
on t1.item = t2.item
where t1.item is null or t2.item is null;
This counts misses. If there are none, then the tables are the same.
Or without the where clause:
select (case when count(t1.item) = count(t2.item) then 'true' else 'false' end)
from t1 full join
t2
on t1.item = t2.item
where t1.item is null or t2.item is null;
Note: Like the data in your question, and the question in general, this assumes that the items are unique.

sql query join multiple tables on a case when column

i have sql like this:
select case when.....end as colA, table2.col3
from table1
join table2 on table2.colB = colA
in the case when statement, there are some other tables columns, is this doable? I have got error saying that colA is not valid.
Column aliases are not understood in the where or on clauses.
You can do this with a subquery, assuming the case only involves columns from table1:
select case when.....end as colA, t2.col3
from (select table1.*, (case when.....end) as colA
from table1
) t1 join
table2 t2
on t2.colB = t1.colA;
Otherwise, you could put the case statement in the on clause:
select case when.....end as colA, t2.col3
from (select table1.*, (case when.....end) as colA
from table1
) t1 join
table2 t2
on t2.colB = ( case when.....end );

Troubleshoot - Ambiguous column name xyz

I run the following query and I get the ambiguous error. Why is it happening ?
select *
from dbo.tableA as table1, dbo.tableZ as table2
where (columnB = dbo.tableZ.columnB)
Ambiguous column name columnB
tableA also has a column named columnB.
Both tables have a columnB but the first part of your condition doesn't specify from which table to use columnB.
Change it to this:
select *
from dbo.tableA as table1, dbo.tableZ as table2
where (table1.columnB = table2.columnB)
You need to use the alias for the field names. It's a good rule to reference your fields using the alias when creating joins so that others can read the SQL easier...
e.g.
SELECT t1.ColA ,
t1.ColB ,
t2.ColA
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.Id = t2.FId
instead of...
SELECT ColA ,
ColB ,
ColA
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.Id = t2.FId
I know the second SQL query would not parse, but it's just an example.

SQL Server query get rows table 1 - rows table 2 without null value

select
id, amount - (select sum(amount) as amount
from table2
where column = 'value' and table1.id = table2.id
group by table2.id) as amount
from
table1
order by
table1.id
The result is all values from table1 - amount from table2 except the table1.id's that are not in table2.id's they get null value.
So it's all good except that I have the null values because I need these to be the table1.amount value
Also tried stuff like this but still not working
select
id, amount - isnull((select sum(amount) as amount
from table2
where column = 'value' and table1.id = table2.id
group by table2.id), table1.amount) as amount
from
table1
order by
table1.id
Then I get 0.00 but the table1.id's that have null in result set do have a real amount value in table1.amount
Overview of what I need
table 1 values (1,12 ; 2,27 ; 3,9) table 2 values (1,9 ; 3,12) result table (1,3 ; 2,27 ; 3,-3)
so I need table1 - values from table 2 to get result table
better do this
select
t1.id, t1.amount - isnull(t2.amountTable2,0) as 'amount'
from table1 T1
left join (select id, sum(ammunt) as 'amountTable2' from table2 group by Id) T2
on t1.id = t2.id
order by t1.id
this will get the total Sum of each id in table2
then will join by id, and do the difference.
If there is no id in table2, will use 0
That answer consider (I thougth that at first) that t2.Id could be repeated
If it's unique, remove the group by
select
t1.id, t1.amount - isnull(t2.amount,0) as 'Totalamount'
from table1 T1
left join table2 T2
on t1.id = t2.id
order by t1.id

How to add a row to the result of a SQL query with INNER JOIN?

In times past, when I need to add a row to the result of a SQL statement, I write a statement like this:
SELECT colA, colB FROM my_table
UNION
SELECT 'foo' AS colA, 'bar' as colB;
However, suppose I've written the following SQL:
SELECT t1.colA, t1.colB, t2.colC FROM my_table t1 INNER JOIN my_other_table t2
How can I add my extra row to my_table when it is INNER JOINed to another table like this?
Update: Wow, I just goofed. It's almost going-home time. I forgot my where clause!
SELECT t1.colA, t1.colB, t2.colC
FROM my_table t1
INNER JOIN my_other_table t2
ON t1.colB = t2.colC
SELECT
(t1.colA, t1.colB FROM my_table
UNION
SELECT 'foo' AS colA, 'bar' as colB) as t1
INNER JOIN
my_other_table t2 ON . . .
SELECT t1.colA, t1.colB, t2.colC FROM my_table t1 INNER JOIN my_other_table t2
UNION
SELECT 'foo' as colA, 'bar' as colB, 'baz' as colC
Just replace mytable in your second query with (SELECT ...), the whole first query (in parenthesis):
SELECT t1.colA, t1.colB, t2.colC
FROM
( SELECT colA, colB
FROM my_table
UNION
SELECT 'foo' AS colA, 'bar' as colB
) AS t1
INNER JOIN my_other_table t2
ON t1.colB = t2.colC
;