Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I'm working on SQL Server 20 1

Status
Not open for further replies.

johnhugh

Technical User
Mar 24, 2010
702
SG
Hi,

I'm working on SQL Server 2005.
I have the 2 tables below, 1 containing item numbers and description, the other one item numbers and supplier numbers.
Some item numbers can have more than 1 supplier number.
How would I be able to bring all suppliers on 1 row, as shown in my results table?

With a join I get a lot of duplicate item numbers and a subquery doesn't work because it cannot have more than 1 return value.

SELECT ICITEM.ITEMNO, ICITEM.[DESC],
(SELECT MANITEMNO FROM ICIOTH WHERE (ITEMNO = ICITEM.ITEMNO)) AS MANUFACTURERS_NUMBER
FROM ICITEM INNER JOIN ICITMV ON ICITEM.ITEMNO = ICITMV.ITEMNO


<html><head><title>Excel Jeanie HTML</title></head><body><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:64px;" /><col style="width:145px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >&#160;</td><td >A</td><td >B</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="font-weight:bold; ">ItemNo</td><td style="font-weight:bold; ">ItemDesc</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="text-align:right; ">1</td><td >Item 1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:right; ">2</td><td >Item 2</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:right; ">3</td><td >Item 3</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:right; ">4</td><td >Item 4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td >&#160;</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="font-weight:bold; ">ItemNo</td><td style="font-weight:bold; ">SupplierNo</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="text-align:right; ">1</td><td >Supplier 1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="text-align:right; ">2</td><td >Supplier 1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td style="text-align:right; ">2</td><td >Supplier 2</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td style="text-align:right; ">3</td><td >Supplier 4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td style="text-align:right; ">4</td><td >Supplier 3</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td style="text-align:right; ">4</td><td >Supplier 4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >14</td><td >&#160;</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >15</td><td >&#160;</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >16</td><td style="font-weight:bold; ">Result</td><td style="font-weight:bold; ">&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >17</td><td style="font-weight:bold; ">ItemNo</td><td style="font-weight:bold; ">SupplierNo</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >18</td><td style="text-align:right; ">1</td><td >Supplier 1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >19</td><td style="text-align:right; ">2</td><td >Supplier 1 / Supplier 2</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >20</td><td style="text-align:right; ">3</td><td >Supplier 4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >21</td><td style="text-align:right; ">4</td><td >Supplier 3 / Supplier 4</td></tr></table> <br /><br /><span style="font-family:Arial; font-size:9pt; font-weight:bold;background-color:#ffffff; color:#000000; ">Excel tables to the web &#62;&#62; </span><a style ="font-family:Arial; font-size:9pt; color:#fcf507; background-color:#800040; font-weight:bold;" href=" target="_blank"> Excel Jeanie HTML 4 </a>

</body></html>
 
Oooh Yuk!

The only way to do this would be with a function where the itemNumber is passed to the function and a concatenated string of values is returned.

SELECT DISTINCT ICITEM.ITEMNO, yourfuncionname(ICITEM.ITEMNO)
FROM ICITEM

This would call the function within the select statement

The function would recieve the itemno and pull all distinct suppliers against the itemno and concatenate using a loop.
 
Thanks all.

Imex - your statement works wonderful!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top