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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advise on SQL Pivot 1

Status
Not open for further replies.

johnhugh

Technical User
Mar 24, 2010
702
SG
Hi, I'm using SQl Server 2005 and have the below table and SQL statement.

My pivot would be working nicely if I did not need to add the receipt number as well.
Every price is linked to a receipt number, and therefore the receipt number needed to show in a column after the price, not in individual rows.

I believe I have to do a 2nd pivot but cannot work out the SQL code on how to do this.
Any help is much appreciated.

SELECT location,fmtitemno as ItemNo,docnum as ReceiptNo, [1] AS Price1, [2] AS Price2, [3] AS Price3, [4] AS Price4
FROM (SELECT location,fmtitemno,docnum, averagecost, rk
FROM averagecost) co PIVOT (sum(averagecost) FOR rk IN ([1], [2], [3], [4])) AS pvt
ORDER BY location,fmtitemno

<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:71px;" /><col style="width:137px;" /><col style="width:117px;" /><col style="width:74px;" /><col style="width:64px;" /><col style="width:64px;" /><col style="width:64px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >&#160;</td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Location</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">ItemNo</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">ReceiptNo</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Price1</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Price2</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Price3</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Price4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-00014 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1094</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">503.92667</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-00047 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0001238 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">926.91</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-01035 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0009002 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">2</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0016290 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">47.895</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0008113 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">54.24667</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0023502 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">38.375714</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0023502 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">19.8272</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0016290 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">6.845</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0009063 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">22.77</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</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>

This is how my table should look like.

<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:71px;" /><col style="width:137px;" /><col style="width:74px;" /><col style="width:101px;" /><col style="width:64px;" /><col style="width:93px;" /><col style="width:76px;" /><col style="width:101px;" /><col style="width:63px;" /><col style="width:82px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >&#160;</td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td><td >I</td><td >J</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Location</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">ItemNo</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Price1</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">ReceiptNo1</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Price2</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">ReceiptNo2</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Price3</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">ReceiptNo3</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">Price4</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; ">ReceiptNo4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-00014 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">503.92667</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1094</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-00047 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">926.91</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0001238 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-01035 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">2</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0009002 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">47.895</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0016290 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">54.2466667</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0008113 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</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-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">38.375714</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0023502 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">19.8272</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0023502 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">6.845</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0016290 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td style="font-family:Arial; font-size:10pt; ">10GEI </td><td style="font-family:Arial; font-size:10pt; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td style="font-family:Arial; font-size:10pt; ">&#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">22.77</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0009063 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:right; ">NULL</td><td >&#160;</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>
 
could you please post your original table and sample data?

Meganathan
 
Thank you for your reply.
Below is my original table.

<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:59px;" /><col style="width:117px;" /><col style="width:61px;" /><col style="width:84px;" /><col style="width:68px;" /><col style="width:137px;" /><col style="width:251px;" /><col style="width:37px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >&#160;</td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="font-weight:bold; text-align:left; ">Location</td><td style="font-weight:bold; text-align:left; ">Docnum</td><td style="font-weight:bold; text-align:left; ">Quantity</td><td style="font-weight:bold; text-align:left; ">AverageCost</td><td style="font-weight:bold; text-align:left; ">Transdate</td><td style="font-weight:bold; text-align:left; ">FMTItemNo</td><td style="font-weight:bold; text-align:left; ">Desc</td><td style="font-weight:bold; text-align:left; ">rk</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">001094                </td><td style="text-align:left; ">6</td><td style="text-align:left; ">503.9266667</td><td style="text-align:left; ">20080331</td><td style="text-align:left; ">1-01-01-01-00014        </td><td style="text-align:left; ">BEARING, SINGLE ROW TAPERED ROLLER ID-120 OD-165 W-29mm     </td><td style="text-align:left; ">1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">RCP0001238            </td><td style="text-align:left; ">1</td><td style="text-align:left; ">926.91</td><td style="text-align:left; ">20081001</td><td style="text-align:left; ">1-01-01-01-00047        </td><td style="text-align:left; ">Pump Water Cummins                                          </td><td style="text-align:left; ">1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">RCP0009002            </td><td style="text-align:left; ">12</td><td style="text-align:left; ">2</td><td style="text-align:left; ">20100210</td><td style="text-align:left; ">1-01-01-01-01035        </td><td style="text-align:left; ">GASKET, VALVE COVER                                         </td><td style="text-align:left; ">1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">RCP0023502            </td><td style="text-align:left; ">7</td><td style="text-align:left; ">38.37571429</td><td style="text-align:left; ">20110714</td><td style="text-align:left; ">1-01-01-01-02182        </td><td style="text-align:left; ">Square tube 40x40x6mtrs                                     </td><td style="text-align:left; ">1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">RCP0016290            </td><td style="text-align:left; ">4</td><td style="text-align:left; ">47.895</td><td style="text-align:left; ">20101120</td><td style="text-align:left; ">1-01-01-01-02182        </td><td style="text-align:left; ">Square tube 40x40x6mtrs                                     </td><td style="text-align:left; ">2</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">RCP0008113            </td><td style="text-align:left; ">6</td><td style="text-align:left; ">54.24666667</td><td style="text-align:left; ">20091218</td><td style="text-align:left; ">1-01-01-01-02182        </td><td style="text-align:left; ">Square tube 40x40x6mtrs                                     </td><td style="text-align:left; ">3</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">RCP0023502            </td><td style="text-align:left; ">50</td><td style="text-align:left; ">19.8272</td><td style="text-align:left; ">20110714</td><td style="text-align:left; ">1-01-01-01-02183        </td><td style="text-align:left; ">Square tube 25x25x6mtrs                                     </td><td style="text-align:left; ">1</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">RCP0016290            </td><td style="text-align:left; ">4</td><td style="text-align:left; ">6.845</td><td style="text-align:left; ">20101120</td><td style="text-align:left; ">1-01-01-01-02183        </td><td style="text-align:left; ">Square tube 25x25x6mtrs                                     </td><td style="text-align:left; ">2</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td style="text-align:left; ">10GEI</td><td style="text-align:left; ">RCP0009063            </td><td style="text-align:left; ">1</td><td style="text-align:left; ">22.77</td><td style="text-align:left; ">20100213</td><td style="text-align:left; ">1-01-01-01-02183        </td><td style="text-align:left; ">Square tube 25x25x6mtrs                                     </td><td style="text-align:left; ">3</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>
 
Thanks makros.
Will give it a try but this does not seem very efficient.
There must be an easier way in SQL 2005 with the Pivot function.
 
using Pivot function

SQL:
SELECT location, fmtitemno as ItemNo, [1] AS Price1, [1001] as ReceiptNo1, [2] AS Price2, [1002] as ReceiptNo2, [3] AS Price3, [1003] as ReceiptNo3, 
[4] AS Price4, [1004] as ReceiptNo4
FROM (SELECT location,fmtitemno,docnum, averagecost, rk , rk + 1000 rknew
FROM  averagecost) co 
PIVOT (sum(averagecost) FOR rk IN ([1], [2], [3], [4])) AS pvt 
PIVOT (max(Docnum) FOR rknew IN ([1001], [1002], [1003], [1004])) AS pvt1 
ORDER BY location,fmtitemno

Meganathan
 
I believe it's quite simple and for multi column pivot it's the best solution.

I know SQL Server MVP who prefers this case based syntax and never uses PIVOT syntax.

PluralSight Learning Library
 
Thank you very much! That pivot is what I couldn't work out
Your statement brings the receipt number right next to the receipt price. Perfect!
How would I get rid of the NULL values for the duplicate Item Numbers? Just do a grouping or is there a smarter way for doing it?

<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:70px;" /><col style="width:141px;" /><col style="width:92px;" /><col style="width:117px;" /><col style="width:66px;" /><col style="width:117px;" /><col style="width:93px;" /><col style="width:117px;" /><col style="width:66px;" /><col style="width:79px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >&#160;</td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td><td >F</td><td >G</td><td >H</td><td >I</td><td >J</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">Location</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">ItemNo</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">Price1</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">ReceiptNo1</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">Price2</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">ReceiptNo2</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">Price3</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">ReceiptNo3</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">Price4</td><td style="font-weight:bold; font-family:Arial; font-size:10pt; text-align:left; ">ReceiptNo4</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">10GEI </td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">47.895</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0016290 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">10GEI </td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">38.37571429</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0023502 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">10GEI </td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1-01-01-01-02182 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">54.24666667</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0008113 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">10GEI </td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">22.77</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0009063 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">10GEI </td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">6.845</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0016290 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">10GEI </td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1-01-01-01-02183 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">19.8272</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0023502 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">10GEI </td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1-01-01-01-02187 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">109.3175</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0027296 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">10GEI </td><td style="font-family:Arial; font-size:10pt; text-align:left; ">1-01-01-01-02320 &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">106.4</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">RCP0010163 &#160; &#160; &#160; &#160; &#160; &#160;</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</td><td style="font-family:Arial; font-size:10pt; text-align:left; ">NULL</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>
 
The grouping seems to have done the trick.

SQL:
SELECT location, itemno, sum([1]) AS Price1, max([1001]) as ReceiptNo1, sum([2]) AS Price2, max([1002]) as ReceiptNo2, sum([3]) AS Price3, max([1003]) as ReceiptNo3, sum([4]) AS Price4, max([1004]) as ReceiptNo4
FROM (SELECT location, itemno, fmtitemno ,docnum, averagecost, rk , rk + 1000 rknew
FROM  averagecost) co 
PIVOT (sum(averagecost) FOR rk IN ([1], [2], [3], [4])) AS pvt 
PIVOT (max(Docnum) FOR rknew IN ([1001], [1002], [1003], [1004])) AS pvt1 
group by location, itemno
order by location, itemno
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top