Order By Sub Query

Posts   
 
    
smurrell
User
Posts: 59
Joined: 22-Feb-2007
# Posted on: 09-Apr-2009 15:26:18   

Hello

I want to be able to sort via a count returned from a subquery.

SELECT tblProduct.sDescription FROM tblProduct WITH (NOLOCK) ORDER BY ( SELECT Count(*) FROM tblStock WITH (NOLOCK) WHERE tblStock.ifkProductID = tblProduct.ipkProductID )

What generated code would I use so I can put it in the Sort to sort by a sub query?

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 09-Apr-2009 15:53:27   

Hi Murrell,

I would do this in 1 of 2 other ways:

1.) Actually Join of tblProduct & tblStock and GROUP BY tblProduct.ipkProductID. Then add a SELECT field for Count(*) AS SortOrder. and SORT BY SortOrder.

or

2.) Put your Subselect in the SELECT clause. ( Subselect stuff ) AS SortOrder. and SORT BY SortOrder

Hope this helps!

Ryan

smurrell
User
Posts: 59
Joined: 22-Feb-2007
# Posted on: 09-Apr-2009 16:46:03   

Hey Ryan

I don't want to do an INNER JOIN as there are a lot of columns. I have showed a simplified version of it. That is why I thought of a sub query. I know it ain't the best option in terms of query optimisation but it will suffice.

Regards, Simon

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 09-Apr-2009 16:56:20   

Hi Simon,

Not sure if a subselect within the order clause is possible with LLBLGen. Of course, I could be wrong. However, there are other ways to do what you're trying to do as listed above.

Why would an INNER JOIN be impacted by having numerous columns, If you can use the SELECT clause to determine what columns are returned?

Ryan

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 10-Apr-2009 09:20:20   

rdhatch wrote:

2.) Put your Subselect in the SELECT clause. ( Subselect stuff ) AS SortOrder. and SORT BY SortOrder

A sub-select in the select lise is the best option, indeed. Use a Scalar query expression for this.

smurrell
User
Posts: 59
Joined: 22-Feb-2007
# Posted on: 10-Apr-2009 09:51:15   

Walaa, do you have a sample code snippet of how it should be done properly?

Walaa wrote:

rdhatch wrote:

2.) Put your Subselect in the SELECT clause. ( Subselect stuff ) AS SortOrder. and SORT BY SortOrder

A sub-select in the select lise is the best option, indeed. Use a Scalar query expression for this.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 10-Apr-2009 10:06:06   

To use the Scalar Query Expression, please check the example found in the docs.

Then it would be easy to use the field were the expression was applied in a sortExpression.

e.g. SortExpression sorter = new SortExpression(fields[1] | SortOperator.Descending);

And pass this to the fetch method.

smurrell
User
Posts: 59
Joined: 22-Feb-2007
# Posted on: 10-Apr-2009 10:55:52   

Perfect, thanks. I will give it a try!!

Walaa wrote:

To use the Scalar Query Expression, please check the example found in the docs.

Then it would be easy to use the field were the expression was applied in a sortExpression.

e.g. SortExpression sorter = new SortExpression(fields[1] | SortOperator.Descending);

And pass this to the fetch method.