I'm using the SQL Server 2008.
Now I have the scenario as description below:
1 table with 3 columns: ID, Name, Order.
They has 8 records.
5 records have the same data: ID='1', Name='AAA'
3 records have the same data: ID='2', Name='BBB'
Now I want to update Order column with number increasing (start from 1) for each ID,Name:
No Name Order
1 AAA 1
1 AAA 2
1 AAA 3
1 AAA 4
1 AAA 5
2 BBB 1
2 BBB 2
2 BBB 3
How can I get this result without using Cursors?
I'm very appriciated for your help.
Thanks.
You can try the following. I use CTE to drive the update statement
WITH data AS
(
SELECT Order
, ROW_NUMBER() OVER (PARTITION BY ID, NAME
ORDER BY ID, NAME) AS Seq
FROM TableA
)
UPDATE data
SET Order = Seq
Substitute TableA with your table name
Related
I need to count matches in a database.
Input:
id_to id_from
1 2
2 1
1 3
3 1
1 4
5 1
the 5th and 6th row has only one direction so doesn't count
Sample Output:
id_match
1
2
3
So, for 1 (implicit), 2 and 3 there is a reverse match but for 4 and 5 there aren't.
---- EDITED ----
Supposing the table name is "example" and I want to get all matches of id=1 then the SQL query will be:
SELECT count(*) FROM
(SELECT id_to FROM example WHERE id_from = 1) as t1,
(SELECT id_from FROM example WHERE id_to = 1) as t2
WHERE t1.id_to = t2.id_from
but maybe there is a better way to do it
You could try
SELECT DISTINCT id_from AS matched_id
FROM your_table AS data1
WHERE EXISTS (SELECT 1
FROM your_table AS data2
WHERE data1.id_from = data2.id_to
AND data1.id_to = data2.id_from)
I've created a demo here
Need MySql query that should return result by ordering values in particular field.
From below my result set should contain order like parent_id (1,4,6) should come first parent_id(2,3,7) come next and other should come last.
d data parent_id
----------------------
1 a1 1
2 abc 3
3 abcd 4
4 xyz 2
5 zxyy 6
2 abc 8
3 abcd 9
4 xyz 2
5 zxyy 15
Use a CASE expression in your ORDER BY clause:
SELECT d, data, parent_id
FROM yourTable
ORDER BY CASE WHEN parent_id IN (1, 4, 6) THEN 1
WHEN parent_id IN (2, 3, 7) THEN 2
ELSE 3 END,
parent_id
Follow the link below for a running demo:
SQLFiddle
Use below mentioned query
SELECT d, data, parent_id FROM table_name ORDER BY FIELD(parent_id,1,4,6,2,3,7);
id name count
------------
1 abc
2 xyz
3 xyz
4 xyz
The following query "select count(name) from temp group by name;" gives me:
count(name)
--------
1
3
I want this result to be updated to the column 'count'. To be precise I want my table to look like :
id name count
------------
1 abc 1
2 xyz 3
3 xyz 3
4 xyz 3
You can get those values with a COUNT / GROUP BY. You can do an UPDATE statement which joins your table with the sub query:-
UPDATE temp a
INNER JOIN
(
SELECT name, COUNT(*) AS name_count
FROM temp
GROUP BY name
) b
ON a.name = b.name
SET a.name_count = b.name_count;
I have a record in table something like this
ID Name Value
1 abc 123
2 abc 152
3 cde 574
4 def 153
5 abc 777
How to delete row from the above column based on this algorithm,
We have same name for 3 rows (ID: 1,2,5). Delete all these rows except any one selected randomly. Same applied for other Names
Possible using sql or T-SQL?
;with d as (
select *, rowNum = row_number() over (partition by Name order by checksum(newid()))
from TableName
)
delete d
where rowNum > 1
I have table with data like this:
Id sort name
1 1 abc
1 2 kci
2 1 asd
2 2 eww
2 3 der
2 4 acu
3 1 awq
3 2 see
3 3 eee
is it possible to create query that will give me result
Id ConcName
1 abc,kci
2 asd,eww,der,acu
3 awq,see,eee
use GROUP_CONCAT()
SELECT ID,
GROUP_CONCAT(name ORDER BY sort ASC) ConcName
FROM tableName
GROUP BY ID
SQLFiddle Demo
GROUP_CONCAT()
SELECT `id`, GROUP_CONCAT(`name` ORDER BY `sort`)
FROM `myTable`
GROUP BY `Id`