SQL: How to append IDs to the rows with duplicate values - sql

I have a table with some duplicate rows. I want to modify only the duplicate rows as follows.
Before:
id col1
------------
1 vvvv
2 vvvv
3 vvvv
After:
id col1
------------
1 vvvv
2 vvvv-2
3 vvvv-3
Col1 is appended with a hyphen and the value of id column.

This SQL will only update duplicates, but not the one with the lowest id :
update tbl
set col1 = col1 + '-' + convert(varchar, id)
where exists(select * from tbl t where t.col1 = tbl.col1 and t.id < tbl.id)

Check IN out in Oracle Syntax. The query is not tested
update table1 set
col1 = col1 || id
where
id not in (
select min(id) from table1
groupby col1
)

You might be able to do that with a sproc and cursors. I don't think it's possible in any reasonable select query.

You can accomplish this with a 2 step process, although an SQL wizard could probably modify this to give you a solution in one step.
First you need to get all the duplicate values. Here is an SQL query that will do that:
SELECT COUNT(*) AS NumberOfDuplicates, col1
FROM Table1
GROUP BY col1
HAVING (COUNT(*) > 1)
This will give you a resultset listing the number of duplicates and the duplicate value.
In step 2 you would loop through this resultset, fetch the col1 value, return all the records containing that value and (possibly using a loop counter variable) alter the value as per your example.
Note: you don't really need to return the number of duplicates to achieve your goal, but it will help you to test the query and be satisfied that it works.

Related

sql query help, don't know how to term this

I'm not too sure how to term this question. Stay with me.
I want to create a query to retrieve the rows with x's in the row column in the table(this is a dummy table):
row col1 col2 col3
x 1 a c
x 2 b c
x 3 a c
4 b d
x 5 f g
So the way I want my query to work is to retrieve all rows where the value for col2 doesn't have a row in col3 where the value is d. Ie. value 'a' will be retrieved because it only has c's for col3, but value 'b' wont be retrieved because it has a d in col3 on the 4th row down.
I hope this is easy to understand.
Ps. Once I know how to do the query I expect I'll know how to phrase the title and will redo it. (although now I think about it, maybe this title is best for all those with questions like mine)
Based on everything you've provided, I'll have to make a few assumptions. Short of doing a self-join on the table, you could take advantage of an identity key on the table to use a simple query as such:
SELECT * FROM TABLE_NAME
WHERE id NOT IN (SELECT id FROM TABLE_NAME WHERE col3 = 'd');
If you don't have an identity key on the table, you could do something more like this:
SELECT * FROM TABLE_NAME
WHERE col2 NOT IN (SELECT col2 FROM TABLE_NAME WHERE col3 = 'd');
Both of these queries will return all tuples in the relation that have elements of col2 that are not in tuples where col3 contains 'd'.
Think of how you might do this manually, and put that into your query.
To get all the col3 values you'd write:
SELECT DISTINCT col3 FROM TABLE_NAME
Use that to filter rows from your selection via a not in clause:
SELECT
col1, col2, col3 FROM TABLE_NAME
WHERE col2 not in (SELECT DISTINCT col3 FROM TABLE_NAME);

Concatenating multiple values into a single string as a subquery without using a variable

I have two row values from table C:
Select Name FROM Table C Where AccountID = 123
COL1
Row 1 |Ricky|
Row 2 |Roxy |
I want to be able to select both of these two values in a SubQuery that will be used in a larger query. So that it displays "Ricky, Roxy"
How can this be done without declaring a variable?
SELECT COL1 = STUFF ((SELECT ',' + COL1 FROM tableC WHERE AccountID=123
FOR XML PATH(''), Type).value('.[1]','nvarchar(max)'),
1,1,'')
This will return all account 123 COL1 values as one column, with commas separating values.
Here is a SQL Fiddle

How to add leading spaces to output column

Here is my SQL statement:
SELECT col1 AS MYCOL FROM table 1
UNION
SELECT col2 AS MYCOL FROM table 2
I need to add some spaces to col2 of table2 in output results so it looks like a tree:
MYCOL
row 1
row 2
row 2.1
row 2.2
row 3
row 3.1
row 3.2
note: just ignore rows sort/order.. Tell me how to add spaces..
Thanks
You can use the Concat(...) Function:
SELECT col1 AS MYCOL FROM table 1
UNION
SELECT CONCAT(" ", col2) AS MYCOL FROM table 2
SQL's job is to produce the required dataset. Beautification should be done at the front-end.
You can create an extra column to indicate the level of a row and use it for the appropriate formatting.
SELECT col1 AS MYCOL, 1 AS LEVEL FROM table 1
UNION
SELECT col2 AS MYCOL, 2 AS LEVEL FROM table 2

sql query to subtract two table values and save them in first table?

How do I subtract one table column value from another table column value, and save the value in the first table column in the process?
Thanks for your help.
Where tablename is the table and col1 is the first column and col2 is the second column.
UPDATE tablename SET col1 = (col2 - col1)
If you are meaning that the values are coming from different tables then the query is much the same:
UPDATE tablename SET col1 = ((SELECT col2 from tablename2) - col1)
It's hard to be sure without details about your tables or how they're related, but you might want something like
UPDATE t1
SET col = t1.col - t2.col
FROM mytable t1
JOIN myothertable t2 ON t2.pkCol = t1.pkCol
Not sure what you are looking for, but pick your example:
update CarBrand
set SortIndex = (select SortIndex from CarBrand where CarBrandId = 3615) + 7
where CarBrandId = 3619
update CarBrand
set SortIndex = SortIndex - OtherColumn
where ...

Help needed with sql query

I have a table which have an integer coloumn. Let table name is Table1 and ColName is Col1.
I want to add number 15 to every value in col1. what will be the sql query for this.
If you're just trying to do that via a select:
select col1 + 15 from Table1
or if you need to update the actual rows in the table:
update Table1 set col1 = col1 + 15
This query updates the Col1 values in your table:
UPDATE TABLE Table1
SET Col1=Col1+15
Be carefull - if your column name contains numbers you should use this syntax:
UPDATE TABLE [Table1]
SET [Col1]=[Col1]+15
UPDATE Table1 SET Col1 = (Col1 + 15)