Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 T-sql Qurey that returns and replaces values

Author  Topic 

nmaruti
Starting Member

10 Posts

Posted - 2009-04-09 : 14:38:36
Hi Team,

I am looking for a qurey that outputs set of 4 columns when thre is a change in any of that 4 column values. It should return "***" for the duplicate value set of 4 columns. For example take the 4 columns as Addr1,Addr2,Addr3,Addr4 as below:

Srl# idcode1 Idcode2 Addr1 Addr2 Addr3 Addr4
1 5623 235 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #127 PO BOX 5067042 200 S 92231
2 252 765 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #127 PO BOX 5067042 200 S 92231
3 245 5678 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #128 PO BOX 5067042 200 S 92231
4 587 7456 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #128 PO BOX 5067042 200 S 92231
5 78 7967 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #128 PO BOX 5067042 200 S 92238
6 785 5678 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #129 PO BOX 5067042 200 S 92231
7 578 3563 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #129 PO BOX 5067042 200 S 92231

The output should be :
Srl# idcode1 Idcode2 Addr1 Addr2 Addr3 Addr4
1 5623 235 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #127 PO BOX 5067042 200 S 92231
2 252 765 ### ### ### ###
3 245 5678 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #128 PO BOX 5067042 200 S 92231
4 587 7456 ### ### ### ###
5 78 7967 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #128 PO BOX 5067042 200 S 92238
6 785 5678 277 RANCHEROS DR, STE 920 740 NORDAHL ROAD #129 PO BOX 5067042 200 S 92231
7 578 3563 ### ### ### ###


Please help.


Thanks,
Nmaruti

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2009-04-10 : 02:25:16
try like this
SET NOCOUNT ON

DECLARE @T_TABLE TABLE
(
[Srl#] INT,
idcode1 INT,
Idcode2 INT,
Addr1 VARCHAR(100),
Addr2 VARCHAR(100),
Addr3 VARCHAR(100),
Addr4 VARCHAR(100)
)
INSERT INTO @T_TABLE
SELECT 1 AS ID, 5623, 235, '277 RANCHEROS DR, STE 920 740', 'NORDAHL ROAD #127', 'PO BOX 5067042 200 S', 92231 UNION ALL
SELECT 2, 252, 765, '277 RANCHEROS DR, STE 920 740', 'NORDAHL ROAD #127', 'PO BOX 5067042 200 S', 92231 UNION ALL
SELECT 3, 245, 5678, '277 RANCHEROS DR, STE 920 740', 'NORDAHL ROAD #128', 'PO BOX 5067042 200 S', 92231 UNION ALL
SELECT 4, 587, 7456, '277 RANCHEROS DR, STE 920 740', 'NORDAHL ROAD #128', 'PO BOX 5067042 200 S', 92231 UNION ALL
SELECT 5, 78, 7967, '277 RANCHEROS DR, STE 920 740', 'NORDAHL ROAD #128', 'PO BOX 5067042 200 S', 92238 UNION ALL
SELECT 6, 785, 5678, '277 RANCHEROS DR, STE 920 740', 'NORDAHL ROAD #129', 'PO BOX 5067042 200 S', 92231 UNION ALL
SELECT 7, 578, 3563, '277 RANCHEROS DR, STE 920 740', 'NORDAHL ROAD #129', 'PO BOX 5067042 200 S', 92231

SELECT [Srl#],
idcode1,
Idcode2,
CASE WHEN RowNo <> 1 then '####' ELSE Addr1 END AS Addr1,
CASE WHEN RowNo <> 1 then '####' ELSE Addr2 END AS Addr2,
CASE WHEN RowNo <> 1 then '####' ELSE Addr3 END AS Addr3,
CASE WHEN RowNo <> 1 then '####' ELSE Addr4 END AS Addr4
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Addr1, Addr2, Addr3, Addr4 order by [Srl#]) AS 'RowNo'
FROM @T_TABLE
) A
ORDER BY [Srl#]


"There is only one difference between a dream and an aim.
A dream requires soundless sleep to see,
whereas an aim requires sleepless efforts to achieve..!!"
Go to Top of Page

nmaruti
Starting Member

10 Posts

Posted - 2009-04-10 : 12:10:21
Hi PeterNeo,

Thanks a lot. I will try it.
Go to Top of Page
   

- Advertisement -