Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Get one row from two rows in SQL query
 

Get one row from two rows in SQL query

How can I get one row from two rows or more in SQL query?

For example I have table:

Table

ID Let

1 ab

1 cd

2 fd

2 ld

I wish to get

ID Let

1 ab, cd

2 fd, ld



Nensy V.
Nensy V_  Monday, August 24, 2009 1:02 PM

//Try this sample Code:

CREATE

TABLE TestTable (

acc_nbr

INT,

CName

VARCHAR(20),

DecNames

VARCHAR(20),

PRIMARY

KEY (acc_nbr,DecNames));

INSERT

INTO TestTable VALUES (1234, 'SUDHIR', '20080101');

INSERT

INTO TestTable VALUES (1234, 'PRASHAT', '20081002');

INSERT

INTO TestTable VALUES (1234, 'YOGESH', '20081003');

SELECT

acc_nbr,(SELECT DecNames+',' FROM TestTable t Where t.acc_nbr = o.acc_nbr for xml path('') ) as Types_Combined

from

TestTable o


Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
  • Proposed As Answer by_SuDhiR_ Monday, August 24, 2009 2:07 PM
  • Marked As Answer byNensy V_ Tuesday, August 25, 2009 12:06 PM
  •  
_SuDhiR_  Monday, August 24, 2009 1:30 PM

It's works!

You only have to put DISTINCT to get one row.

So select query would look like:

SELECT DISTINCT
acc_nbr,(SELECT DecNames+',' FROM TestTable t Where t.acc_nbr = o.acc_nbr for xml path('') ) as Types_Combined
from TestTable o

Thanks!


Nensy V.
  • Marked As Answer byNensy V_ Tuesday, August 25, 2009 12:06 PM
  •  
Nensy V_  Tuesday, August 25, 2009 11:52 AM

//Try this sample Code:

CREATE

TABLE TestTable (

acc_nbr

INT,

CName

VARCHAR(20),

DecNames

VARCHAR(20),

PRIMARY

KEY (acc_nbr,DecNames));

INSERT

INTO TestTable VALUES (1234, 'SUDHIR', '20080101');

INSERT

INTO TestTable VALUES (1234, 'PRASHAT', '20081002');

INSERT

INTO TestTable VALUES (1234, 'YOGESH', '20081003');

SELECT

acc_nbr,(SELECT DecNames+',' FROM TestTable t Where t.acc_nbr = o.acc_nbr for xml path('') ) as Types_Combined

from

TestTable o


Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
  • Proposed As Answer by_SuDhiR_ Monday, August 24, 2009 2:07 PM
  • Marked As Answer byNensy V_ Tuesday, August 25, 2009 12:06 PM
  •  
_SuDhiR_  Monday, August 24, 2009 1:30 PM

It's works!

You only have to put DISTINCT to get one row.

So select query would look like:

SELECT DISTINCT
acc_nbr,(SELECT DecNames+',' FROM TestTable t Where t.acc_nbr = o.acc_nbr for xml path('') ) as Types_Combined
from TestTable o

Thanks!


Nensy V.
  • Marked As Answer byNensy V_ Tuesday, August 25, 2009 12:06 PM
  •  
Nensy V_  Tuesday, August 25, 2009 11:52 AM

You can use google to search for other answers

Custom Search

More Threads

• Beginner data binding question
• Why does in one case "concurrency violation" is triggered, while it doesn't in another case?
• Datagrid as 2DTable with labeled rows and columns
• Cannot add record in VB 2005 Express Edition
• what is the reason for first chance exception of type system.ArgumentException when using sql_variant?
• How to update dataset xsd in VS?
• ListBox Data Binding and Selection Set Accessing
• How do i bind two datagrids independently to datasource from different queries?
• Assining Null value to data Field - Help !!!
• The functions of toolstrip