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.
| Author |
Topic |
|
dnf999
Constraint Violating Yak Guru
253 Posts |
Posted - 2010-03-02 : 11:58:15
|
| Hi I have a table with data which I need to assign a unique ID based on date, currency and ledger.i.e.Date, Currency, Ledger, Amount Required_ID1/1/00 USD 5 123.14 11/1/00 USD 5 100.00 11/1/00 USD 5 100.03 11/1/02 USD 5 100.03 21/1/02 USD 5 100.03 21/1/03 USD 5 100.03 3Now I require the Required_ID field to populate as in the example above. I have tried to use:ROW_NUMBER() OVER(PARTITION BY DATE, CURRENCY, LEDGER ORDER BY date, currency, ledger) AS Required_IDbut that gives a unique row number per grouping row.Any help much appreciated... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 12:03:58
|
| DENSE_RANK() OVER(PARTITION BY CURRENCY, LEDGER ORDER BY date) AS Required_ID------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-02 : 12:09:23
|
| select *,DENSE_RANK()OVER(ORDER BY date) as Amount Required_ID from yourtablePBUH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 12:21:28
|
quote: Originally posted by Idera select *,DENSE_RANK()OVER(ORDER BY date) as Amount Required_ID from yourtablePBUH
see the differenceSELECT *,DENSE_RANK() OVER(PARTITION BY curr, Led ORDER BY dt) AS Required_ID_Visakh,DENSE_RANK()OVER(ORDER BY dt) as Required_ID_IderaFROM(select '1/1/00' as dt, 'USD' AS curr, 5 AS Led, 123.14 AS Amt UNION ALLselect '1/1/00', 'USD', 5, 100.00 UNION ALLselect '1/1/00', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'USD', 5, 100.03 UNION ALLselect '1/1/03' , 'GBP' , 6 , 145.28 UNION ALLselect '1/1/02', 'GBP', 6, 75.00 UNION ALLselect '1/1/02', 'GBP', 6, 243.03)tORDER BY curr desc,dtoutput----------------------------dt curr Led Amt Required_ID_Visakh Required_ID_Idera1/1/00 USD 5 123.14 1 11/1/00 USD 5 100.03 1 11/1/00 USD 5 100.00 1 11/1/02 USD 5 100.03 2 21/1/02 USD 5 100.03 2 21/1/03 USD 5 100.03 3 31/1/02 GBP 6 75.00 1 21/1/02 GBP 6 243.03 1 21/1/03 GBP 6 145.28 2 3 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-02 : 12:25:54
|
quote: Originally posted by visakh16
quote: Originally posted by Idera select *,DENSE_RANK()OVER(ORDER BY date) as Amount Required_ID from yourtablePBUH
see the differenceSELECT *,DENSE_RANK() OVER(PARTITION BY curr, Led ORDER BY dt) AS Required_ID_Visakh,DENSE_RANK()OVER(ORDER BY dt) as Required_ID_IderaFROM(select '1/1/00' as dt, 'USD' AS curr, 5 AS Led, 123.14 AS Amt UNION ALLselect '1/1/00', 'USD', 5, 100.00 UNION ALLselect '1/1/00', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'USD', 5, 100.03 UNION ALLselect '1/1/03' , 'GBP' , 6 , 145.28 UNION ALLselect '1/1/02', 'GBP', 6, 75.00 UNION ALLselect '1/1/02', 'GBP', 6, 243.03)tORDER BY curr desc,dtoutput----------------------------dt curr Led Amt Required_ID_Visakh Required_ID_Idera1/1/00 USD 5 123.14 1 11/1/00 USD 5 100.03 1 11/1/00 USD 5 100.00 1 11/1/02 USD 5 100.03 2 21/1/02 USD 5 100.03 2 21/1/03 USD 5 100.03 3 31/1/02 GBP 6 75.00 1 21/1/02 GBP 6 243.03 1 21/1/03 GBP 6 145.28 2 3 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Oh I missed the proper requirement of OP.SELECT *,DENSE_RANK() OVER(PARTITION BY curr, Led ORDER BY dt) AS Required_ID_Visakh,DENSE_RANK()OVER(ORDER BY dt,curr,led) as Required_ID_IderaFROM(select '1/1/00' as dt, 'USD' AS curr, 5 AS Led, 123.14 AS Amt UNION ALLselect '1/1/00', 'USD', 5, 100.00 UNION ALLselect '1/1/00', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'USD', 5, 100.03 UNION ALLselect '1/1/03' , 'GBP' , 6 , 145.28 UNION ALLselect '1/1/02', 'GBP', 6, 75.00 UNION ALLselect '1/1/02', 'GBP', 6, 243.03)tdt curr Led Amt Required_ID_Visakh Required_ID_Idera------ ---- ----------- --------------------------------------- -------------------- --------------------1/1/00 USD 5 123.14 1 11/1/00 USD 5 100.03 1 11/1/00 USD 5 100.00 1 11/1/02 GBP 6 75.00 1 21/1/02 GBP 6 243.03 1 21/1/02 USD 5 100.03 2 31/1/02 USD 5 100.03 2 31/1/03 GBP 6 145.28 2 41/1/03 USD 5 100.03 3 5 PBUH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 12:30:06
|
| still we're not sure what OP's looking at. it may be either your last suggestion or mine------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-02 : 12:34:18
|
| At first glance I to thought that your solution is right but then I thought maybe the Op wants it otherwise.PBUH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 12:37:22
|
lets wait until he comes back ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-02 : 12:38:17
|
quote: Originally posted by visakh16 lets wait until he comes back ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Yup PBUH |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-03-02 : 14:59:34
|
| [code]SELECT *,DENSE_RANK() OVER(ORDER BY dt, curr, led) AS Required_ID_Ms65gFROM(select '1/1/00' as dt, 'USD' AS curr, 5 AS Led, 123.14 AS Amt UNION ALLselect '1/1/00', 'USD', 5, 100.00 UNION ALLselect '1/1/00', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'GBP', 6, 145.28 UNION ALLselect '1/1/02', 'GBP', 6, 75.00 UNION ALLselect '1/1/02', 'GBP', 6, 243.03)tORDER BY dt, curr, led/*dt curr Led Amt Required_ID_Ms65g------ ---- ----------- --------------------------------------- --------------------1/1/00 USD 5 100.00 11/1/00 USD 5 100.03 11/1/00 USD 5 123.14 11/1/02 GBP 6 75.00 21/1/02 GBP 6 243.03 21/1/02 USD 5 100.03 31/1/02 USD 5 100.03 31/1/03 GBP 6 145.28 41/1/03 USD 5 100.03 5(9 row(s) affected)*/[/code] |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-02 : 23:32:33
|
| So how is your o/p different than mine?PBUH |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-03-03 : 01:23:52
|
quote: Originally posted by Idera So how is your o/p different than mine?PBUH
Oh I did not see your modified query! sorry.EDIT: write missed 'see' |
 |
|
|
dnf999
Constraint Violating Yak Guru
253 Posts |
Posted - 2010-03-03 : 01:51:16
|
| Hi all,Thanks for all your replies. This bit of code worked a treat:DENSE_RANK() OVER(ORDER BY dt, curr, led) AS Required_ID_Ms65gThanks! |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-03 : 01:57:46
|
quote: Originally posted by ms65g
SELECT *,DENSE_RANK() OVER(ORDER BY dt, curr, led) AS Required_ID_Ms65gFROM(select '1/1/00' as dt, 'USD' AS curr, 5 AS Led, 123.14 AS Amt UNION ALLselect '1/1/00', 'USD', 5, 100.00 UNION ALLselect '1/1/00', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'GBP', 6, 145.28 UNION ALLselect '1/1/02', 'GBP', 6, 75.00 UNION ALLselect '1/1/02', 'GBP', 6, 243.03)tORDER BY dt, curr, led/*dt curr Led Amt Required_ID_Ms65g------ ---- ----------- --------------------------------------- --------------------1/1/00 USD 5 100.00 11/1/00 USD 5 100.03 11/1/00 USD 5 123.14 11/1/02 GBP 6 75.00 21/1/02 GBP 6 243.03 21/1/02 USD 5 100.03 31/1/02 USD 5 100.03 31/1/03 GBP 6 145.28 41/1/03 USD 5 100.03 5(9 row(s) affected)*/
Highlighted in blue color(ORDER BY dt, curr, led) does this necessary to have this at the end,cos order by items menetined in the select colmn level itself..dont mistake me,iam beginner.. :) |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-03 : 02:02:39
|
| On basis of the initial requirement the OP wanted to assign unique id's based on date, currency and ledger.So the order by clause you marked in blue becomes irrelevant in this case.PBUH |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-03 : 02:05:13
|
quote: Originally posted by Idera On basis of the initial requirement the OP wanted to assign unique id's based on date, currency and ledger.So the order by clause you marked in blue becomes irrelevant in this case.PBUH
Yeah..Thank you very much for your immediate Response. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-03 : 02:06:27
|
quote: Originally posted by haroon2k9
quote: Originally posted by Idera On basis of the initial requirement the OP wanted to assign unique id's based on date, currency and ledger.So the order by clause you marked in blue becomes irrelevant in this case.PBUH
Yeah..Thank you very much for your immediate Response.
You are welcome. PBUH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 09:34:29
|
quote: Originally posted by ms65g
SELECT *,DENSE_RANK() OVER(ORDER BY dt, curr, led) AS Required_ID_Ms65gFROM(select '1/1/00' as dt, 'USD' AS curr, 5 AS Led, 123.14 AS Amt UNION ALLselect '1/1/00', 'USD', 5, 100.00 UNION ALLselect '1/1/00', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/02', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'USD', 5, 100.03 UNION ALLselect '1/1/03', 'GBP', 6, 145.28 UNION ALLselect '1/1/02', 'GBP', 6, 75.00 UNION ALLselect '1/1/02', 'GBP', 6, 243.03)tORDER BY dt, curr, led/*dt curr Led Amt Required_ID_Ms65g------ ---- ----------- --------------------------------------- --------------------1/1/00 USD 5 100.00 11/1/00 USD 5 100.03 11/1/00 USD 5 123.14 11/1/02 GBP 6 75.00 21/1/02 GBP 6 243.03 21/1/02 USD 5 100.03 31/1/02 USD 5 100.03 31/1/03 GBP 6 145.28 41/1/03 USD 5 100.03 5(9 row(s) affected)*/
Is DENSE_RANK a standard function or have you moved away from your earlier standards? http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=139575------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-03-03 : 11:01:18
|
quote: Originally posted by visakh16Is DENSE_RANK a standard function or have you moved away from your earlier standards? http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=139575------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
JOE CELKO:"With additional of OLAP functions in SQL-99, life becomes very easy" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-03 : 11:04:09
|
quote: Originally posted by ms65g
quote: Originally posted by visakh16Is DENSE_RANK a standard function or have you moved away from your earlier standards? http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=139575------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
JOE CELKO:"With additional of OLAP functions in SQL-99, life becomes very easy"
Exactly ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|