Reputation: 317
OriData
+-----------------+-----------+-------+-------+------+
| selected_RowNum | V6_RowNum | SeqNo | Name | IDNo |
+-----------------+-----------+-------+-------+------+
| 1 | 1 | A1234 | Yummy | 1234 |
| 1 | 2 | A1234 | Yummy | 1234 |
| 1 | 3 | A1234 | Yummy | 1234 |
| 1 | 4 | A1234 | Yummy | 1234 |
| 1 | 1 | B123 | Yummy | 1234 | << I want this
| 1 | 1 | C123 | Yummy | 1234 | << I want this
+-----------------+-----------+-------+-------+------+
Result I want
+-----------------+-----------+-------+-------+------+
| selected_RowNum | V6_RowNum | SeqNo | Name | IDNo |
+-----------------+-----------+-------+-------+------+
| 1 | 1 | B123 | Yummy | 1234 |
| 1 | 1 | C123 | Yummy | 1234 |
+-----------------+-----------+-------+-------+------+
Here is my query:
select
case
when selected_rownum=V6_RowNum and V6_RowNum=1 then 'updateonetime'
when selected_rownum=V6_RowNum and V6_RowNum>1 then 'updatemanytimes'
else '0'
end as NewColumnA,
*
from Table #A
I inner join V6 and Selected table and into #A
I want to check any update between 2 tables, so I inner join both table and created rowNum for 2 tables named Selected_rowNum and v6_rownum (that sort by date). Selected_RowNum = 1 and V6_rowNum = 1 (and this V6_rownum is not repeating for same SeqNo, IDNo)
If I update 1 time, it will triggered in V6 table. If I update many times, it will triggered V6 table many time as you can see in SeqNo=A1234. As you can see, even the IDNo is repeating but it may created many applications. So, it need filter based on IDNo and SeqNo and Selected_RowNum=1 and V6_RowNum=1.
Any idea, how to get the result I want?
Upvotes: 1
Views: 91
Reputation: 1012
Please try this.
If select updateonlyone time then
Select selected_RowNum,seqNo,IDNo
,'updateonetime' As OneTime
from @tbl
group by selected_RowNum,seqNo,IDNO
having count(*) = 1
If select updateonlyone and updatemanytimes time then
Select selected_RowNum,seqNo,IDNo
,CASE WHEN Count(*) > 1 THEN 'updatemanytimes' ELSE 'updateonetime' END
from @tbl
group by selected_RowNum,seqNo,IDNO
Upvotes: 0
Reputation: 276
From the looks of it, you only want to display the results for rows where there aren't any entries with updates (V6_RowNum > 1 only?). To do this, you'd need to check that those rows don't exist in the table, like this:
SELECT CASE
WHEN selected_rownum=V6_RowNum and V6_RowNum=1 THEN 'updateonetime'
WHEN selected_rownum=V6_RowNum and V6_RowNum>1 THEN 'updatemanytimes'
ELSE '0'
END as NewColumnA,
*
FROM #A a
WHERE selected_RowNum = 1
AND V6_RowNum = 1
AND NOT EXISTS
(
SELECT 1 FROM #A a2
WHERE a2.SeqNo = a.SeqNo
AND a2.V6_RowNum > 1
)
Upvotes: 1