Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:36:14+00:00 2026-05-27T00:36:14+00:00

I thought this was causing a Cartesian effect but the results always return exactly

  • 0

I thought this was causing a Cartesian effect but the results always return exactly two identical products in the data that are returned.

How I can force a distinct on the select? I tried modifying the final select statement to include a “Select Distinct Top…” but it gave me an error regarding syntax. Sorry if this is a simple question, my SQL skills are lacking.

(
@ProductSKUs varchar(500),
@CategoryIDs varchar(40),
@RecordCount int
)
AS
BEGIN
declare @Products table (ProductSKU varchar(8), ProductID int not null, TrackInventoryBySizeAndColor int not null)
declare @TempCID table(CategoryID int not null default 0)
declare @CountCID int
set @CountCID=0
declare @productIDcount int
select @productIDcount = count(*) from dbo.Split(@ProductSKUs, ',')
declare @categoryIDcount int
select @categoryIDcount = @RecordCount - @productIDcount

-- product SKUs first
insert into @Products(ProductSKU, ProductID, TrackInventoryBySizeAndColor)
select p.SKU, p.ProductID, p.TrackInventoryBySizeAndColor
from dbo.Product p with (nolock)
join dbo.Split(@ProductSKUs, ',') pi on p.SKU = pi.items
-- now variant SKUs
insert into @Products(ProductSKU, ProductID, TrackInventoryBySizeAndColor)
select pv.SKUSuffix, pv.ProductID, ISNULL(pv.TrackInventoryBySizeAndColor, 0)
from dbo.ProductVariant pv with (nolock)
join dbo.Split(@ProductSKUs, ',') pi on pv.SKUSuffix = pi.items
--debug
--SELECT * FROM @Products
if   @categoryIDcount > 0 begin
 insert into @TempCID(CategoryID)
  select c.CategoryID  from Category c with(nolock) --get the subcats
  join dbo.Split(@CategoryIDs, ',') ci on c.ParentCategoryID = cast(ci.items as int)
    union
  select c2.CategoryID  from Category c2 with(nolock)  --get the category itself
  join dbo.Split(@CategoryIDs, ',') ci on c2.CategoryID = cast(ci.items as int)
insert into @TempCID(CategoryID)
  select c.CategoryID from Category c with(nolock) 
  join @TempCID tc on c.ParentCategoryID = tc.CategoryID --get level 2 subcats    
  set @CountCID = @@ROWCOUNT
    insert into @Products(ProductSKU, ProductID, TrackInventoryBySizeAndColor)
        select TOP (@categoryIDcount) p.SKU, p.ProductID, p.TrackInventoryBySizeAndColor
        from dbo.Product p with (nolock)
        left join dbo.ProductCategory pc with (nolock) on p.ProductID = pc.ProductID 
        where pc.CategoryID in (select tc.CategoryID from @TempCID tc)
        AND pc.ProductID Not in (SELECT ProductID FROM @Products)
    ORDER BY CHECKSUM(NEWID())
end
--debug
--SELECT * FROM @Products
select top(@RecordCount)
    p.ProductID,
    p.Name,
    pv.VariantID,
    pv.Name as VariantName,
    p.ProductGUID,
    p.Summary,
    p.Description,
    p.SEKeywords,
    p.SEDescription,
    p.SpecTitle,
    p.MiscText,
    p.SwatchImageMap,
    p.IsFeaturedTeaser,
    p.FroogleDescription,
    p.SETitle,
    p.SENoScript,
    p.SEAltText,
    p.SizeOptionPrompt,
    p.ColorOptionPrompt,
    p.TextOptionPrompt,
    p.ProductTypeID,
    p.TaxClassID,
    p.SKU,
    p.ManufacturerPartNumber,
    p.SalesPromptID,
    p.SpecCall,
    p.SpecsInline,
    p.IsFeatured,
    p.XmlPackage,
    p.ColWidth,
    p.Published,
    p.RequiresRegistration,
    p.Looks,
    p.Notes,
    p.QuantityDiscountID,
    p.RelatedProducts,
    p.UpsellProducts,
    p.UpsellProductDiscountPercentage,
    p.RelatedDocuments,
    p.TrackInventoryBySizeAndColor,
    p.TrackInventoryBySize,
    p.TrackInventoryByColor,
    p.IsAKit,
    p.ShowInProductBrowser,
    p.IsAPack,
    p.PackSize,
    p.ShowBuyButton,
    p.RequiresProducts,
    p.HidePriceUntilCart,
    p.IsCalltoOrder,
    p.ExcludeFromPriceFeeds,
    p.RequiresTextOption,
    p.TextOptionMaxLength,
    p.SEName,
    p.Deleted,
    p.CreatedOn,
    p.ImageFileNameOverride,
    pv.VariantGUID,
    pv.Description as VariantDescription,
    pv.SEKeywords as VariantSEKeywords,
    pv.SEDescription as VariantSEDescription,
    pv.Colors,
    pv.ColorSKUModifiers,
    pv.Sizes,
    pv.SizeSKUModifiers,
    pv.FroogleDescription as VariantFroogleDescription,
    pv.SKUSuffix,
    pv.ManufacturerPartNumber as VariantManufacturerPartNumber,
    pv.Price,
    pv.CustomerEntersPrice, 
    pv.CustomerEntersPricePrompt,
    isnull(pv.SalePrice, 0) SalePrice,
    cast(isnull(pv.Weight,0) as decimal(10,1)) Weight,
    pv.MSRP,
    pv.Cost,
    isnull(pv.Points,0) Points,
    pv.Dimensions,
    pv.DisplayOrder as VariantDisplayOrder,
    pv.Notes as VariantNotes,
    pv.IsTaxable,
    pv.IsShipSeparately,
    pv.FreeShipping,
    pv.IsDownload,
    pv.DownloadLocation,
    pv.Published as VariantPublished,
    pv.IsSecureAttachment,
    pv.IsRecurring,
    pv.RecurringInterval,
    pv.RecurringIntervalType,
    pv.SubscriptionInterval,
    pv.SEName as VariantSEName,
    pv.RestrictedQuantities,
    pv.MinimumQuantity,
    pv.Deleted as VariantDeleted,
    pv.CreatedOn as VariantCreatedOn,
    d.Name as DistributorName,
    d.DistributorID,
    d.SEName as DistributorSEName,
    m.ManufacturerID,
    m.Name as ManufacturerName,
    m.SEName as ManufacturerSEName,
    s.Name as SalesPromptName
from dbo.Product p with (nolock)
    left join dbo.ProductVariant pv with (nolock) on p.ProductID = pv.ProductID
    join @Products pid on p.SKU = pid.ProductSKU OR pv.SKUSuffix = pid.ProductSKU
    left join dbo.SalesPrompt s with (nolock) on p.SalesPromptID = s.SalesPromptID 
    left join dbo.ProductManufacturer pm with (nolock) on p.ProductID = pm.ProductID 
    left join dbo.Manufacturer m with (nolock) on pm.ManufacturerID = m.ManufacturerID 
    left join dbo.ProductDistributor pd with (nolock) on p.ProductID = pd.ProductID
    left join dbo.Distributor d with (nolock) on pd.DistributorID = d.DistributorID
    where p.Deleted = 0 
    and p.Published = 1
    ORDER BY p.ShowBuyButton desc

EDIT: The @RecordCount is the number of records to return in the final select statement:
select top(@RecordCount) –this is passed into the SPROC.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:36:15+00:00Added an answer on May 27, 2026 at 12:36 am

    UPDATE 2 :
    My bad..I didnt know you could use top like that see updated code for correct syntax –

    If you need to do a top and distinct you need to it like –

    select distinct
    top (@RecordCount)
    p.ProductID,
    p.Name,
    pv.VariantID
    ..so on

    I do suggest you look at using a group by and rewriting your query to your needs. You should not use distinct for that many columns.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought this was pretty straight forward but I don't get the same results
I took out all the things that I thought could be causing this white
I thought this was asked before, but 15 minutes of searching on Google and
I thought this code would work, but the regular expression doesn't ever match the
I thought this would be simple, but its not working .. in both the
I thought this would be fairly easy, but I'm totally baffled. I want one
I thought this was covered elsewhere but I don't see it now. Anyway, having
I thought this would be pretty easy but I'm running into all sorts of
I thought this was a privilege issue but I logged on as admin and
EDIT: Clarifying. fout is is a FILE*. (I thought this was irrelevant since that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.