Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here is query

SELECT   c.Customer_CNIC,g.Guarantor_CNIC FROM Customer c,Guaronters g where g.Customer_CNIC=c.Customer_CNIC and c.Customer_CNIC='12345-1234567-8' order by c.Customer_CNIC




i m getting result as:
Customer_CNIC         Guarantor_CNIC
12345-1234567-8       78663-2349875-6
12345-1234567-8       87676-5765757-6

but i want as:
Customer_CNIC         Guarantor_CNIC
12345-1234567-8       78663-2349875-6
                      87676-5765757-6
Posted
Comments
joshrduncan2012 10-Jan-13 9:44am    
group by clause?
zeshanazam 10-Jan-13 10:00am    
??
PIEBALDconsult 10-Jan-13 9:53am    
That should probably be handled in the presentation layer; not the data layer.
zeshanazam 10-Jan-13 10:00am    
how ?
PIEBALDconsult 10-Jan-13 10:08am    
That would depend on the presentation layer. What are you using? Is it a Web site? Crystal Reports or similar?

Look into level breaks.

There is no mechanism I know of in SQL to do that, and for good reason.
SQL is not about presentation of the data, each row is intended to be self contained, and not to rely on earlier results (because the order of processing of records is not guaranteed by SQL Server).

To do that, you would have to create a Stored procedure that selected the rows into a temporary table and looped through them creating a second temporary table for output. Nasty, and inefficient.

This is a task with is much, much more easily handled in the presentation layer, whatever that happens to be coded in.
 
Share this answer
 
Comments
Maciej Los 10-Jan-13 16:30pm    
+5!
Wendelius 12-Jan-13 2:53am    
Fully agree.
As i wrote before: SQL is not for 'visualize' data!
PIEBALDconsult wrote:

That is not a system for presentation; that is a language for querying and manipulating data.

OriginalGriff wrote:
SQL is not about presentation of the data, each row is intended to be self contained (...)

How many times we need to repeat it?

BTW:
You need to read about JOIN[^] statement. By using joins[^], you can retrieve data from two or more tables based on logical relationships between the tables. For example:
SQL
SELECT   c.Customer_CNIC, g.Guarantor_CNIC
FROM Customer c INNER JOIN Guaronters g ON g.Customer_CNIC=c.Customer_CNIC 
WHERE c.Customer_CNIC='12345-1234567-8'
ORDER BY c.Customer_CNIC


Tips (to achieve that what OriginalGriff wrote):
1) How to CREATE TABLE[^]? - section Temporary tables. There you'll find informations about: how to insert data into temporary table.
2) How to go through the collection of records? Use cursor[^] and WHILE[^] command - section: B. Using WHILE in a cursor.
 
Share this answer
 
Comments
Wendelius 12-Jan-13 2:53am    
Using a temp table is a good option, 5.
Maciej Los 12-Jan-13 16:24pm    
Thank you, Mika.
As said by others, formatting shouldn't be done using SQL. But if you still for some reason want to do it, depending on the database, you could use LAG.

For example, have a look at: How to fetch data from the previous or next rows in the resultset[^]
 
Share this answer
 
Comments
Maciej Los 11-Jan-13 17:26pm    
Good answer, perfect tip! +5!
Wendelius 12-Jan-13 2:54am    
Thanks :)

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