.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

replacing Group By with row_number(partition by ...)

Posted By:      Posted Date: September 24, 2010    Points: 0   Category :Sql Server
I have an indexed view and I query this view to get counts on how many products each seller has in a specific category. This query works perfect and produces results in 45ms. I want to join additional tables and apply additional WHERE filters to the result set. I am not able to do it because I had to get products_pid out of With clause to be able to join other tables after CTE. I couldn't add products_pid to the select list because it would require me to have in the Group By. After reading articles in this forum, I found out that Group By can be replaced by row_number() over (partition by...). I re-wrote my query using row_number(). But this query takes 1100 ms to execute. Is this the write way to get rid of Group By? What am I doing wrong here and why it takes so long to execute?
Thank you,

Original query:
set statistics time on
with keys as (
select getproductssellers.productslocation_cid,
count(distinct getproductssellers.products_pid) as CNT
from GetProductsSellers WITH (NOEXPAND)
WHERE getproductssellers.products_subcategory=26
group by getproductssellers.productslocation_cid
select customer.businessname as Seller, productslocation_cid, CNT
from keys
inner join custom

View Complete Post

More Related Resource Links

ROW_NUMBER() OVER - PARTITION BY should be different from ORDER BY

I have noticed incorrect usage for OVER - PARTITION BY - ORDER BY with same (BY) column lists, making the query nondeterministic as far as the generated ROW_NUMBER() is concerned.  If random sort needed, use NEWID() instead. Demo follows. -- WRONG PARTITION BY ... ORDER BY ... usage - nondeterministic sort SELECT ProductName = Name, Color, PartRowID = ROW_NUMBER() OVER(PARTITION BY Color ORDER BY Color) FROM AdventureWorks2008.Production.Product WHERE Color IS NOT NULL ORDER BY Color, PartRowID /* ProductName Color PartRowID .... Men's Bib-Shorts, L Multi 3 AWC Logo Cap Multi 4 Long-Sleeve Logo Jersey, S Multi 5 .... */ -- CORRECT PARTITION BY ... ORDER BY ... usage - UNIQUE sort (Name is unique) SELECT ProductName = Name, Color, PartRowID = ROW_NUMBER() OVER(PARTITION BY Color ORDER BY Name) FROM AdventureWorks2008.Production.Product WHERE Color IS NOT NULL ORDER BY Color, PartRowID /* ProductName Color PartRowID .... Long-Sleeve Logo Jersey, M Multi 3 Long-Sleeve Logo Jersey, S Multi 4 Long-Sleeve Logo Jersey, XL Multi 5 Men's Bib-Shorts, L Multi 6 .... */ Kalman Toth, SQL Server & Business Intelligence Training; SQL 2008 GRAND SLAM

how to process just one partition along with other measure group and dimension in SSIS package Analy

HI All, i have to process just one partition1  of measure group A ,along with this i suppose to process all the Measure group and dimension with the help of SSIS Package Analysis Services Processing task. Partition1 having a query which fetch data only for previous day only. what i have done i select partition 1 in process data mode,all other measure group in full mode and dimension in process update mode.   i haven't taken measure group of partition1 and also not taken cube in the processing list ,when i run the package ,it runs suceesfully but data not get uplaoded into the Cube.   kindly suggest what other measures should i take to update the data . Amit



Hi all

I have the following table:

ID IsTop Value
1 1 TestGroup1
2 0 Child 1 (TestGroup1)
3 0 Child 2 (TestGroup1)
4 1 TestGroup2
5 0 Child 1 (TestGroup2)
6 0 Child 2 (TestGroup2)
7 0 Child 3 (TestGroup2)

Now I need to find out the groups,
The ID 1, 2, 3 are one Group
and the IDs 4, 5, 6, 7 are another Group.
The group names are not in the value,
it is just for this sample to show what I mean.

To identify I need to get IsTop = 1 and all following
rows until IsTop again = 1.

I have tried to use it with PARTITION BY, but this does not work.
Any other ideas ?

Best regards
Frank Uray

P.S. Here are the script for testing:

CREATE TABLE #tempPARTITION ([ID] int, [IsTop] int, [Value] varchar(255))
INSERT INTO #tempPARTITION ([ID], [IsTop], [Value])
SELECT 1, 1, 'TestGroup1'
SELECT 2, 0, 'Child 1 (TestGroup1)'
SELECT 3, 0, 'Child 2 (TestGroup1)'
SELECT 4, 1, 'TestGroup2'
SELECT 5, 0, 'Child 1 (TestGroup2)'
SELECT 6, 0, 'Child 2 (TestGroup2)'
SELECT 7, 0, 'Child 3 (TestGroup2)'


Problem to group radio button across gridview rows

In general no one can easily group or make a single selection from radio button list inside gridview rows. There are a lot of way to make a single or unique selection from list. Here i want to share how i can address this problem in the most easiest way. Googling the problem most of the cases i saw the grouping in horizontal way. Thats why i tried to group the radio buttons in vertical manner. Let i have a requirement like below:

Group Calendars in SharePoint 2010

One of the many requests I had in SharePoint 2007 was the ability to place SharePoint Calendars on top of each other like you can in Outlook 2007 saving you having to look at each and work out when the best time is for a meeting.

ListViewControl - how to group items by their value, for eg. date range



I have a table containing courses, columns are 

Name - "Yoga Classes"

DateStart - "2/23/2010"

DateEnd - "2/24/2010"

I am using ListView Control to bind it, is there a way to display them and group them by Month? 

for eg.


Piano Class (10th Jan 2010 - 11th Jan 2010)
Guitar Class (10th Jan 2010 - 11th Jan 2010)


Yoga Class (15th Feb 2010 - 16th Feb 2010)

Thanks for help.

replacing ok and cancel button of an alert message with YES an NO button in asp.net web application.


i want to replace ok and cancel button of an alert message with YES an NO button in asp.net web application.When a user will click yes the data will be saved in the database and if user clicks that will act as a reset button and do not save the data in database and the control will move with as usual to the redirect page.Any pointers will be highly appreciated.I am usin g C# language.

How do I get the emails of all the members of a given group in ActiveDirectory - Need help



  I have a need to send out emails to all the people who are in a given group say "HR Department". I need to be able to get all the emails through a for loop and then as it retrieves I should send emails to them. Can some one please help me with the code

this is what I came up with but not sure how to go about writing a for loop to get all the emails in the given group.

public static string SendEmails(string loginName)
        string Department = "HR Department"
        string userName = ExtractUserName(loginName);
        DirectorySearcher search = new DirectorySearcher();
        search.Filter = String.Format("(SAMAccountName={0})", userName);
        //search.Filter = String.Format("(SAMAccountName={FAY ImprovmentForm})", userName);
        //LdapSearcher.Filter = string.Format("(&(objectClass=user)(department={0}))", Department); 
        //search.Filter = string.Format("(&(objectClass=group)(SAMAccountName=" + Department + "))", userName);

Need Help in LINQ query for group By with chunks of record


I am assigning and unique id [strShipperIdSequence] on my List on bases of some properties which are grouped together uniquely.
Now what i needed is that my group should be further break down to some maximum amount of chunks.[Let say 10]
that mean's even i am having Same value in 12 records i should get 2 groups[I of 10 items and other of 2 items]

var uniqueGroups = objMdbContentInfoList.GroupBy(p => new
}).Select(g => g.First()).ToList();
foreach (var objUnique in uniqueGroups)
string strShipperIdSequence = APIGlobalMethods.GetShipperRequestID();
foreach (MdbContentInfo obj in objMdbContentInfoList.FindAll(h => (h.CON_ENTRY_POINT == objUnique.CON_ENTRY_POINT &&
h.APPTType == objUnique.APPTType &&

Test Run: Group Determination In Software Testing


There is a large body of research on group techniques to determine the best alternative from a set of options. Dr. James McCaffrey outlines five of them.

Dr. James McCaffrey

MSDN Magazine November 2008

Pulling data from MyProfile page to People & group's list

Hi, Is it possible to pull the data from Profile page, like the email, contact number etc to be displayed in the People & Groups page. Please refer the screenshot for better understanding..

Link to the image: http://img694.imageshack.us/i/pplgrps.png/

How do I prevent displaying membership of an AD group in Colleague Tracker web part?


I'm a big fan of the Colleague Tracker web part in MOSS 2007 My Sites, especially the option to show 'Membership Changes'. This is often useful in highlighting that a colleague has been added to a certain AD group.

We now want to hide the membership of certain AD groups from the colleague tracker. For example, if an AD group called 'Project X' is created, we don't want membership of the group broadcast throughout the My Sites where colleagues are tracking colleagues.

My first thought was that we could use AD to 'deny read' on the 'Project X' AD group to all SharePoint related service accounts. This does not appear to have worked, although perhaps the configuration is more complex? The best approach would seem to be to prevent the group membership data ever being imported from AD to the SSP, hence my 'deny read' approach.

Has anyone successfully implemented this, or does anyone have any better ideas?


Sharepoint list group by column data ,display in webcontrol like panel or label


I  have a sharepoint list[Managers] with groupby sector column. like below




Each Sector group by column data i want to display in label or panel control.

How to do that ? how to write the logic?



Regards, Mansoor

User group invalid for new document



Defined a user group and a site column of type people/group directing to this group.

Also defined a content type and added the site column to this content type.

When I create a new word document of this content type, word opens displaying my site column ok. But when I enter it, I see that all active directory (AD) entries are displayed. I want it to display only those users I added to the user group I created. Moreover, users can select any user from AD, which makes defining a user group and a site column meaningless.

This problem is only in creating new documents with Word. If I edit properties of an already existing document in my site, everything is ok: I can select only the users in the group.


Content Query with more thatn on group


I want to group the result of the content query web part in two levels but this web part only have one Grouping field. How can I do grouping with more than one field?

<site> and DocumentSubtype (custom property)


  • Site 1
  •   Document Sub type 1
  •        File 1
  •        File 2
  •   Document Sub type 2
  • Site 2
  • Document Sub type 1

Can't find AD security group while creating Audience rule


Hi guys,

I'm trying to create audience rule : all members of AD group. But when I try to choose a security group I need I can't find in search window. Though I see another groups. So in a whole the problem is I can't see all the AD groups but only some of them. It doesn't depends on on its type (global or local domain).

What could be a reason?

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend