SQL Server : Identity column by group - sql

How can I add an identity number so that when a row is inserted an incremental number is assigned as below by a trigger? I am using SQL Server.
1 AAA
2 AAA
3 BBB
4 CCC
5 CCC
6 CCC
7 DDD
8 DDD
9 EEE
....
And I want to convert it to:
1 AAA 1
2 AAA 2
4 CCC 1
5 CCC 2
6 CCC 3
7 DDD 1
8 DDD 2

You could create a FUNCTION which get a name and gives MAX identity for given parameter:
CREATE FUNCTION [dbo].[GetIdentityForName] (#Name VARCHAR(MAX))
RETURNS INT
AS
BEGIN
RETURN
(SELECT ISNULL(MAX(NameId),0)+1
FROM YourTable
WHERE Name = #Name);
END
and then set DefaultValue for NameId for call the function when a record has been inserted like this:
ALTER TABLE YourTable ADD CONSTRAINT
DF_Identity_NameId DEFAULT ([dbo].[GetIdentityForName](Name)) FOR NameId
Assuming that YourTable is (Id, Name, NameId).
I hope to be helpful for you :)

There is no reason why you have to store the value. You could calculate it when you need it:
select t.*, row_number() over (partition by name order by id) as nameId
from t;

Related

How to transform data rows into new column?

table
id text
1 aaa
121 bbb
4 ccc
1 ddd
new table
id text2
1 aaaddd
121 bbb
4 ccc
I do not think I can use PIVOT since I never know how many and what id and text values would be so I cannot hardcode them in a PIVOT instruction.
use group by with string_agg
select id,string_agg(text,'') as text2
from table
group by id

Update Duplicate Records

I have a column store table, TABLE_HANA with the below data in HANA DB:
T_ID1 T_ID2 T_DESC T_RN
1 1 AAA ?
1 1 BBB ?
1 2 CCC ?
1 3 DDD ?
1 3 EEE ?
I need to group them by T_ID1 and T_ID2 and update the T_RN column of that group with a sequential value as shown below:
T_ID1 T_ID2 T_DESC T_RN
1 1 AAA 1
1 1 BBB 2
1 2 CCC 1
1 3 DDD 1
1 3 EEE 2
Since the table is very huge(millions of records), I am looking for a solution with good performance. Please suggest.
TIA
The first question is whether you really need to do the update. The following query returns what you want:
select h.*,
row_number() over (partition by t_id1, t_id2 order by t_desc) as t_rn
from table_hana h;
This may be sufficient.
The update will probably be expensive:
update table_hana h
set t_rn = (select count(*)
from table_hana h2
where h2.t_id1 = h.t_id1 and h2.t_id2 = h.t_id2 and h2.desc <= h.desc
);
An index on table_hana(t_id1, t_id2, t_desc) should help both these queries.

filter column + insert

I have two tables:
TABLE1:
field1 | field2 | field3
1 5 aaa
2 10 bbb
3 10 ccc
4 10 ddd
5 10 eee
6 6 fff
7 7 ggg
TABLE2:
will have the insert of all values that contain in field2 >= 2 equals value
so in this case it should be like this:
TABLE2:
field1 | field2 | field3
2 10 bbb
3 10 ccc
4 10 ddd
5 10 eee
how can I know whats values have the >= 2 same name? and make this insert?
If I understand, you want the second table to have all duplicates (relative to field2) in the first table.
select field1, field2, field3
into table2
from (select t.*, count(*) over (partition by field2) as cnt
from table1 t
) t
where cnt >= 2;
This creates the second table using select into. If it already exists, use insert . . . select instead.

SQL SERVER - Change column value if value exists

I have the following problem.
Imagine I get the following return table from a select statement
Column A Column B
100 aaa
100 bbb
100 ccc
200 ddd
300 eee
So the question is, how can I change my SQL Select statement to add a new column that shows the numbers of times the Column A has a repeat value. The problem is that I need to get some subgrups with an order.
For example, it should return something like:
Column A Column B Column C
100 aaa 1
100 bbb 2
100 ccc 3
200 ddd 1
300 eee 1
Thank you very much for your support!
This is the classic usecase for the analytic RANK() function:
SELECT a, b, RANK() OVER (PARTITION BY a ORDER BY b) AS c
FROM my_table
Add ROW_NUMBER() OVER (PARTITION BY ColA ORDER BY SomethingElse) as ColC. That gives you a sequential row number per "group" in ColA.

Retrieve data from Table

I have a table as follows,
Id Form_Id Form_Layout_Txt
-------------- ----------- -----------------
2 1 aa
3 1 bb
5 2 dddd
6 2 eeeee
7 3 fffff
8 3 gggg
i need to retreive the rows which has same Form_id as follows,
2 1 aa
3 1 bb
How to write a query for the above data?
Thanks
Here is what you need at most basic level, unless there's a hidden immunity to find out more :)
SELECT * FROM YOUR TABLE
WHERE FORM_ID = 1
Select *
from nameoftable
where Form_Id = 1;