I have requirement to create an auto incremented id based on certain join condition. I have two tables each having identity column. Primary table would have relationship with the secondary table on "trxid" column. Below is the sample data
declare #t1 table (trxid int,data1 varchar(100), data2 varchar(100))
declare #m table (mid int, trxid int, mname varchar(100), mdate date)
insert into #t1
select 1,'Test1','Test1'
union all
select 2,'Test2','Test2'
insert into #m
select 1,1,'Stage Gate1',GETDATE()
union all
select 2,1,'Stage Gate2',GETDATE()
union all
select 3,1,'Stage Gate3',GETDATE()
union all
select 4,1,'Stage Gate4',GETDATE()
union all
select 5,1,'Stage Gate5',GETDATE()
union all
select 6,1,'Stage Gate6',GETDATE()
union all
select 7,2,'Stage Gate1',GETDATE()
union all
select 8,2,'Stage Gate2',GETDATE()
union all
select 9,2,'Stage Gate3',GETDATE()
union all
select 10,2,'Stage Gate4',GETDATE()
union all
select 11,2,'Stage Gate5',GETDATE()
union all
select 12,2,'Stage Gate6',GETDATE()
union all
select 13,2,'Stage Gate7',GETDATE()
Result with below statement
select t.trxid,m.mid,t.data1,t.data2,m.mname,m.mdate from #t1 t inner join #m m on(t.trxid=m.trxid)
trxid mid data1 data2 mname mdate
----------- ----------- ------- ------- --------------- ----------
1 1 Test1 Test1 Stage Gate1 2018-06-07
1 2 Test1 Test1 Stage Gate2 2018-06-07
1 3 Test1 Test1 Stage Gate3 2018-06-07
1 4 Test1 Test1 Stage Gate4 2018-06-07
1 5 Test1 Test1 Stage Gate5 2018-06-07
1 6 Test1 Test1 Stage Gate6 2018-06-07
2 7 Test2 Test2 Stage Gate1 2018-06-07
2 8 Test2 Test2 Stage Gate2 2018-06-07
2 9 Test2 Test2 Stage Gate3 2018-06-07
2 10 Test2 Test2 Stage Gate4 2018-06-07
2 11 Test2 Test2 Stage Gate5 2018-06-07
2 12 Test2 Test2 Stage Gate6 2018-06-07
2 13 Test2 Test2 Stage Gate7 2018-06-07
Expected result is
trxid id mid data1 data2 mname mdate
----------- ----------- ----------- ------- ------- --------------- ----------
1 1 1 Test1 Test1 Stage Gate1 2018-06-07
1 2 2 Test1 Test1 Stage Gate2 2018-06-07
1 3 3 Test1 Test1 Stage Gate3 2018-06-07
1 4 4 Test1 Test1 Stage Gate4 2018-06-07
1 5 5 Test1 Test1 Stage Gate5 2018-06-07
1 6 6 Test1 Test1 Stage Gate6 2018-06-07
2 1 7 Test2 Test2 Stage Gate1 2018-06-07
2 2 8 Test2 Test2 Stage Gate2 2018-06-07
2 3 9 Test2 Test2 Stage Gate3 2018-06-07
2 4 10 Test2 Test2 Stage Gate4 2018-06-07
2 5 11 Test2 Test2 Stage Gate5 2018-06-07
2 6 12 Test2 Test2 Stage Gate6 2018-06-07
2 7 13 Test2 Test2 Stage Gate7 2018-06-07
Assuming you don't need to persist this ID and then generate new unique incremental numbers based on the persisted data, you can use window functions to generate a row_number for each group of your data:
select t.trxid
,m.mid
,row_number() over (partition by t.trxid order by m.mid) as id
,t.data1
,t.data2
,m.mname
,m.mdate
from #t1 t
inner join #m m
on(t.trxid = m.trxid)
If you need to base this on data that you have already saved to a table elsewhere, your query will need to be more complex.
Let's say I have a table called Build and a table called Test. Now, for every build there are 3 tests. And the table tests has build_id as foreign key. Also, when a test fails my test framework retries the test again. Given this, there would be atleast 3 entries in the Test table + more based on how many tests failed and how many times it was retries (max = 3). Also, after retries, if the test passes, then the test is marked as passed.
Sample:
Build Table:
Id No of Tests Failure Count
1 3 0
2 3 0
3 3 1
Tests Table:
build_id test_id test_name test_result
1 1 test1 p
1 2 test2 p
1 3 test3 f
1 3 test3 f
1 3 test3 f
1 3 test3 p
2 1 test1 p
2 2 test2 p
2 3 test3 p
3 1 test1 p
3 2 test2 f
3 3 test3 p
2 2 test2 f
2 2 test2 f
2 2 test2 f
Now, based on that I want to print
test_name retries
test3 3
test2 3
If I understand correctly, you want the number of times that a test is "f":
select test_name, count(*) as retries
from tests
where test_result = 'f'
group by test_name;
I need to count how many times an variable id value is repeated in field B of table 2, and update the value in field A of table 1 corresponding to the line which that variable id value is in the field B and the date.
UPDATE Table_1
SET Field_A = ( SELECT COUNT(*)
FROM Table_2
WHERE Table_2.Field_B = 1 AND Table_2.Field_3='2015-04-04')
WHERE Table_1.Field_B = 1 AND Table_3.Field_3='2015-04-04'
Something like this
UPDATE Table_1
SET Field_A = ( SELECT COUNT(*)
FROM Table_2
WHERE Table_2.Field_B = X AND Table_2.Field_3='xxxx-xx-xx')
WHERE Table_1.Field_B = X AND Table_3.Field_3='xxxx-xx-xx'
Table 1 Table 2
Field_3 Field_1 Field_3 Field_4 Field_1 Field_3
1 2 04-04-2015 200,00 1 04-04-2015
2 3 04-04-2015 300,00 1 04-04-2015
3 1 04-04-2015 150,00 2 04-04-2015
1 1 05-04-2015 853,00 2 04-04-2015
2 2 05-04-2015 200,00 2 04-04-2015
3 1 05-04-2015 200,00 3 04-04-2015
4 2 05-04-2015 3,00 1 05-04-2015
40,00 2 05-04-2015
Field 1 - TransactionCount 900,00 2 05-04-2015
Field 2 - SessionID 35,00 3 05-04-2015
Field 3 - Date 25,00 4 05-04-2015
Field 4 - TransactionSales 100,00 4 05-04-2015
I have table like this
id buildingId order title
--------------------------
1 2 null test1
2 2 null test2
3 2 null test3
4 3 null test4
5 3 null test5
6 5 null test6
I need calculate order value for every row. I need after execute sql query get this table
id buildingId order title
1 2 0 test1
2 2 1 test2
3 2 2 test3
4 3 0 test4
5 3 1 test5
6 5 0 test6
How can I do that?
WITH recordList
AS
(
SELECT ID, buildingID, [order], title,
ROW_NUMBER() OVER (PARTITION BY buildingID
ORDER BY ID) rn
FROM tableName
)
UPDATE recordList
SET [order] = rn - 1
SQLFiddle Demo
here is the table
and data like:
id name
1 test1
2 test2
3 test3
4 test4
5 test5
6 test6
From above data i want the data like
if i pass the id as parameter and return the data from from up and gown by order
Example if i pass the id as parameter = 4 then it should be return
upline 2 row and downline 2 row for particular id, and it should be like this
id name
2 test2
3 test3
4 test4
5 test5
6 test6
and same for the id = 3
id name
1 test1
2 test2
3 test3
4 test4
5 test5
SELECT TOP 3 id, name
FROM table
WHERE id =< #id
ORDER BY id DESC
UNION
SELECT TOP 2 id, name
FROM table
WHERE id > #id
ORDER BY id ACS