June 13, 2013 at 6:06 am
wow amazing what a little coffee can do for me!
my issue was the wrong tool for the job.
i needed to use DENSE_RANK() and not ROW_NUMBER
this is what i should have used:
SELECT
1 As TheExpectedValue,
IDIS_ACTV_ID AS ID,
DENSE_RANK() OVER (PARTITION BY IDIS_ACTV_ID,ACTV_HOME_PROPERTY_ID ORDER BY IDIS_ACTV_ID) AS BUILDINGID,
Z.ACTNBR,
Z.ACTNAME,
Z.RELATEDYEAR,
Z.IDIS_ACTV_ID,
Z.ACTV_HOME_PROPERTY_FUND_ID,
Z.ACTV_HOME_PROPERTY_ID,
Z.FUND_CAT_ID
FROM #MYSAMPLEDATA Z
Lowell
June 13, 2013 at 6:18 am
I was working on this when you posted your solution and had also tried the ROW_NUMBER, RANK and DENSE_RANK, but it wasn't returning what I thought were the results you wanted when you added in another row with a different ACTV_HOME_PROPERTY_ID value. I was scratching my head when you posted your resolution. Are you sure this is what you're after?
And yes, coffee is a great help to all of us. 🙂
June 13, 2013 at 6:26 am
Ed you are correct, i'm still not getting what i'm after;
if i add these five more rows to my sample data above, i would expect five values with [1] and five values with[2], but i end up with all [1] values
INSERT INTO [#MYSAMPLEDATA] ([ACTNBR],[ACTNAME],[RELATEDYEAR],[IDIS_ACTV_ID],[ACTV_HOME_PROPERTY_FUND_ID],[ACTV_HOME_PROPERTY_ID],[FUND_CAT_ID])VALUES('2011-03699','RILEY & MERIAL UZEL','2011','3699','900000000315449','900000000105338','122')
INSERT INTO [#MYSAMPLEDATA] ([ACTNBR],[ACTNAME],[RELATEDYEAR],[IDIS_ACTV_ID],[ACTV_HOME_PROPERTY_FUND_ID],[ACTV_HOME_PROPERTY_ID],[FUND_CAT_ID])VALUES('2011-03699','RILEY & MERIAL UZEL','2011','3699','900000000329445','900000000105338','22')
INSERT INTO [#MYSAMPLEDATA] ([ACTNBR],[ACTNAME],[RELATEDYEAR],[IDIS_ACTV_ID],[ACTV_HOME_PROPERTY_FUND_ID],[ACTV_HOME_PROPERTY_ID],[FUND_CAT_ID])VALUES('2011-03699','RILEY & MERIAL UZEL','2011','3699','900000000315451','900000000105338','302')
INSERT INTO [#MYSAMPLEDATA] ([ACTNBR],[ACTNAME],[RELATEDYEAR],[IDIS_ACTV_ID],[ACTV_HOME_PROPERTY_FUND_ID],[ACTV_HOME_PROPERTY_ID],[FUND_CAT_ID])VALUES('2011-03699','RILEY & MERIAL UZEL','2011','3699','900000000315452','900000000105338','303')
INSERT INTO [#MYSAMPLEDATA] ([ACTNBR],[ACTNAME],[RELATEDYEAR],[IDIS_ACTV_ID],[ACTV_HOME_PROPERTY_FUND_ID],[ACTV_HOME_PROPERTY_ID],[FUND_CAT_ID])VALUES('2011-03699','RILEY & MERIAL UZEL','2011','3699','900000000315450','900000000105338','21')
Lowell
June 13, 2013 at 6:34 am
Maybe this?
DENSE_RANK() OVER (ORDER BY IDIS_ACTV_ID,ACTV_HOME_PROPERTY_ID) AS BUILDINGID,
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537
June 13, 2013 at 6:47 am
I came up with this.
SELECT
IDIS_ACTV_ID AS ID,
Z.ACTV_HOME_PROPERTY_ID,
ROW_NUMBER() OVER (PARTITION BY ACTV_HOME_PROPERTY_ID ORDER BY (SELECT NULL)) AS BUILDINGID,
Z.ACTNBR,
Z.ACTNAME,
Z.RELATEDYEAR,
Z.IDIS_ACTV_ID,
Z.ACTV_HOME_PROPERTY_FUND_ID,
Z.FUND_CAT_ID
FROM #MYSAMPLEDATA Z;
It builds a series for each ACTV_HOME_PROPERTY_ID and restarts for each unique value. I must admit, however, that I'm questioning whether or not I'm understanding the requirements properly.
June 13, 2013 at 7:49 am
Mark-101232 (6/13/2013)
Maybe this?
DENSE_RANK() OVER (ORDER BY IDIS_ACTV_ID,ACTV_HOME_PROPERTY_ID) AS BUILDINGID,
Adding anything else in the order by will screw the DENSE_RANK if they aren't all the same. This should do it based on how I've understood the requirement:
DENSE_RANK() OVER (ORDER BY ACTV_HOME_PROPERTY_ID) AS BUILDINGID_V4
Viewing 6 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply