Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello friends,

column 1|  column 2
Name    | Parentsname
________|_____________
john    | xyz
john    | pqr


I insert name and parent name in first row but in second row i want only parent name to be inserted if same name is same.(name should be empty)..I want output is-

column 1|  column 2
Name    | Parentsname
________|_____________
john    | xyz
        | pqr



how to write query for it in sql server...please help me..

Thanks
Posted
Updated 30-Oct-15 12:55pm
v9
Comments
OriginalGriff 30-Oct-15 15:22pm    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So show us what your tables look like, and what output you expect - because your description is not very clear.
Also try to explain what you have tried, where you are stuck, and what help you need.

Use the "Improve question" widget to edit your question and provide better information.
Member 10852141 30-Oct-15 16:04pm    
how to make table in text filled..so that i can edit my question.
reply
[no name] 30-Oct-15 15:58pm    
I think you have to think about the logic to store data first.
Member 10852141 30-Oct-15 16:12pm    
i need a query in sql server
[no name] 30-Oct-15 16:25pm    
" I want, In first column and second row should be empty":
Far away clear what you need. Again, think about your logic!

Your question makes no sense as it breaks the fundamentals of relational databases.
You cannot insert data in a table without some kind of reference or key to so you can get it back.

Let's say that in your example, that you insert a row with Name and Parent.
Name  Parent
John  Mary

Then you insert a second row with the father and leave name empty
Name   Parent
John   Mary
<null> Robert

Then you insert row 3 and 4 with Johns friend, Peter.
Name   Parent
John   Mary
<null> Robert
Peter  Elisabeth
<null> Patrick

Now try to create an SQL query to get both the parents name for either of the boys.
That might be a bit tricky. Right?
The SQL query
SQL
SELECT Parent FROM Parents WHERE Name = 'John';

will give the following result:
Parent
Mary

The father cannot be found because Name and Parent are not connected in this case.
So when you store the data, you cannot leave a row hanging without any reference to call it back.

The table should look like this:
Name   Parent
John   Mary
John   Robert
Peter  Elisabeth
Peter  Patrick


The SQL query
SQL
SELECT Parent FROM Parents WHERE Name = 'John';

will give the following result:
Parent
Mary
Robert

How you decide to format and present this data on a screen or report is i different thing from how you store the data.

I think you have confused the way you store data in an Excel sheet with how you store it in a database, but that is just a guess.
 
Share this answer
 
v2
Comments
Krunal Rohit 31-Oct-15 2:31am    
I like the very first line :laugh:
5!

-KR
George Jonsson 31-Oct-15 2:35am    
Thanks. :)
I kind of thought it was an essential thing to mention.
Use the Row_number() and group it with John so the output looks like this

column 0 |column 1|  column 2
Rownumber|Name    | Parentsname
_________|_________|_____________
   1     | john    | xyz
   2     | john    | pqr


And use case statement to remove the repeating John like when rownumber>1 then '' as Name.
You might use CTE(Common table Expression) and do the case statement in the derived query.

But the best practice is to do it in the code part of front end
 
Share this answer
 

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