[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.2

Distinct select on non distinct rows

Asked by jonnyboy69 in MS SQL Server

Tags: select, distinct, rows

I have 3 tables

tbpProducts
tblCategories
tblProductCategories (allocation)

tblProductCategories acts as an allocation table allowing there to be multiple categories per product.

I have a problem with a complexish (for me anyway) query I have which returns a resultset back for a query. Its made more complex by the fact that it deals with pagin result sets too.
The problem is that I need to return DISTINCT products however the DISTINCT keyword does not work for products which are in multiple categories as the rows are obviously not unique.
How in the following query do I return rows where KEYPRODUCT is unique?

Thanks very much

CREATE PROCEDURE ts_GetAllToysByWhereClause
@CurrentPage int,
@PageSize int,
@WHEREClause nvarchar(1000)
AS
--Create a temp table to hold the current page of data
--Add and ID column to count the records
CREATE TABLE #TempTable
(
  ID int IDENTITY NOT NULL PRIMARY KEY,
        KeyProduct int,
        KeyFCategory int,
        ProductUrl nvarchar(80),
        Code nvarchar(80),
        ProductNameShort nvarchar(80),
        ProductDescShort nvarchar(300),
        MetaKeywords nvarchar(200),
        MetaDescription nvarchar(500),
        PageTitle nvarchar(80),
        ImageName nvarchar(80),
        ImageAltTag nvarchar(80),
        LinkTitleTag nvarchar(80),
        KeyFGroup int,
        SortPriority int,
        Price money,
        KeyFGender int,
        AgeMin int,
        AgeMax int,
        IsOutOfStock bit,
        IsLowStock bit,
        IsHide bit,
        IsNoGiftWrap bit,
        IsInternetOnly bit,
        IsTopTen bit

)

-- Create a variable @SQLStatement
DECLARE @SQLStatement  nvarchar(1000)

-- Enter the dynamic SQL statement into the
-- variable @SQLStatement
SET @SQLStatement = "INSERT INTO #TempTable SELECT DISTINCT KeyFCategory, KeyProduct, ProductUrl, Code, ProductNameShort, ProductDescShort, MetaKeywords, MetaDescription, PageTitle, ImageName, ImageAltTag, LinkTitleTag, KeyFGroup, SortPriority, Price, KeyFGender, AgeMin, AgeMax, IsOutOfStock, IsLowStock, IsHide, IsNoGiftWrap, IsInternetOnly, IsTopTen FROM tblProductCategories INNER JOIN tblProducts ON KeyFProduct = KeyProduct" + @WHERECLAUSE + " ORDER BY SortPriority"

-- Execute SQL statement, inserting into the #TempTable
EXEC(@SQLStatement)

--Create variable to identify the first and last record that should be selected
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@CurrentPage - 1) * @PageSize
SELECT @LastRec = (@CurrentPage * @PageSize + 1)

--Select one page of data based on the record numbers above
SELECT KeyFCategory, KeyProduct, ProductUrl, Code, ProductNameShort, ProductDescShort, MetaKeywords, MetaDescription, PageTitle, ImageName, ImageAltTag, LinkTitleTag, KeyFGroup, SortPriority, Price, KeyFGender, AgeMin, AgeMax, IsOutOfStock, IsLowStock, IsHide, IsNoGiftWrap, IsInternetOnly, IsTopTen
FROM
  #TempTable
WHERE
  ID > @FirstRec
AND
  ID < @LastRec
GO


[+][-]08/03/04 08:08 AM, ID: 11705470Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/03/04 08:18 AM, ID: 11705618Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/03/04 08:54 AM, ID: 11706108Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/03/04 09:45 AM, ID: 11706798Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/03/04 12:04 PM, ID: 11708555Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/04/04 05:37 AM, ID: 11714668Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/04/04 10:18 AM, ID: 11718150Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/05/04 05:58 AM, ID: 11725551Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/05/04 06:58 AM, ID: 11726166Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/05/04 07:34 AM, ID: 11726565Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/05/04 07:47 AM, ID: 11726710Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/05/04 08:11 AM, ID: 11726975Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/05/04 08:25 AM, ID: 11727159Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/05/04 08:26 AM, ID: 11727160Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/05/04 09:37 AM, ID: 11727977Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/06/04 08:46 AM, ID: 11736723Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: MS SQL Server
Tags: select, distinct, rows
Sign Up Now!
Solution Provided By: hkamal
Participating Experts: 2
Solution Grade: A
 
[+][-]08/06/04 09:09 AM, ID: 11736964Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/09/04 02:15 AM, ID: 11750866Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89