Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Acc   Plan
121  A
121  A
121  B
131  A
131  B
141  C
141  C
145  D
190  R
200  S



i want result like:


SQL
Acc   Plan
121  A
121  NULL
121  B
131  A
131  B
141  C
141  NULL
145  D
190  R
200  S
Posted
Comments
OriginalGriff 28-Sep-15 9:23am    
Why would you want to?
Why not just use DISTINCT and only return the unique rows?
Naveen.Sanagasetti 28-Sep-15 9:31am    
Could you please confirm to me, as per your thread header you are asking to get the result. Where you want to show the same.? Is it in gridview or any other datasource controls.?
Tryingtobeageek 29-Sep-15 2:37am    
actually in the column Plan if the values are repeated then only for one Acc value should remain same and others should get update as NULL

As per my understand the post you want to get the result and show it in gridview or any other datasource control, in the above format right.?

If my understanding is correct then do the below steps

Step 1:

Design your gridview column like below

ASP.NET
<asp:TemplateField HeaderText="Material"  HeaderStyle-ForeColor="White">
    <ItemTemplate>
       <asp:Label ID="lblMaterial" runat="server" Text='<%# Bind_Material(Convert.ToString(Eval("Material_Name")))%>'/>
     </ItemTemplate>
</asp:TemplateField>


Step 2:

declare this method in server side of the page

C#
protected string Bind_Material(string sMaterial)
    {
        if (Material == sMaterial)
            {
                    sMaterial = "";
            }
            else
            {
                    Material = sMaterial;
            }
            return sMaterial;
    }


don't do any changes in database result set, keep as it is...
 
Share this answer
 
Comments
Maciej Los 29-Sep-15 16:17pm    
My vote of 2: the question is about pure SQL. I do not see ASP.NET or c# tag.
Krunal Rohit 30-Sep-15 0:32am    
And my of 1: For doing all this irrelevant work.

-KR
Have a look at example:
SQL
DECLARE @tmp TABLE(Acc VARCHAR(30), [Plan] VARCHAR(30))

INSERT INTO @tmp (Acc, [Plan])
VALUES('121', 'A'), ('121', 'A'),
('121', 'B'), ('131', 'A'),
('131', 'B'), ('141', 'C'),
('141', 'C'), ('145', 'D'),
('190', 'R'), ('200', 'S')

--1. solution
--===========
SELECT Acc, CASE WHEN ROW_NUMBER() OVER(PARTITION BY Acc, [Plan] ORDER BY [Plan])>1 THEN NULL ELSE [Plan] END AS [Plan]
FROM @tmp

--2.solution
--===========
--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
--Warning: original values will be overwritten!
--!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
UPDATE t1 SET t1.[Plan] = NULL
FROM (
    SELECT Acc, [Plan], ROW_NUMBER() OVER(PARTITION BY Acc, [Plan] ORDER BY [Plan]) As RowNo
    FROM @tmp
) AS t1
WHERE t1.RowNo > 1

SELECT *
FROM @tmp


Result:
Acc Plan
121 A
121 NULL
121 B
131 A
131 B
141 C
141 NULL
145 D
190 R
200 S


For further details, please see: Ranking Functions (Transact-SQL)[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900