Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hello, I am having some problems reaching a result, let me explain. I have a table with several columns the ones that matter here is pn and poPO PNabc123 erte56546abc123 ertbd4564abc123 sdbdfb245bdg214 sdgsdf548bdg214 sdfsd1651and i need thisPO PN newpoabc123 erte56546 abc1230001abc123 ertbd4564 abc1230002abc123 sdbdfb245 abc1230003bdg214 sdgsdf548 bdg2140001bdg214 sdfsd1651 bdg2140002I need to include a consecutive number to the po for each pnright now i only get the amount of PN per PO using the query below.select PO, max(PN) , count(PO) as qty from mytablegroup by POPlease help.
That's the way a program is developed. And it works with a table I currently import , but I need to importa new table that does not have this suffix so i have to includeit to the newpo
ehorn
Master Smack Fu Yak Hacker
1632 Posts
Posted - 2012-01-17 : 15:54:34
Hello jocast,Perhaps the ROW_NUMBER function would help here? something like the following;
SELECT PO, PN, PO + RIGHT('0000' + CAST(ROW_NUMBER() OVER (ORDER BY PO, PN) AS VARCHAR(4)),4) AS 'newpo' FROM @TableORDER BY PO, PN --or whatever you need to order by