2013-05-07 41 views
1

這裏是故事:我必須實現過濾器。在這個過濾器中有一些我過濾的類別。 其中一個過濾器是「最喜歡的」過濾器(@includeFavourites)。從另一個表中加入唯一ID

我有這個巨大的SQL與分頁和排序和一切。現在,當過濾器中的「includeFavourites」選項被點擊時,我也必須從不同的表中選擇唯一的ID(這些條目存儲在不同的數據表中),其中存儲了收藏夾。 我試過左外連接,但它爲主表中的每條記錄返回「最愛數量」記錄。合併完全沒有幫助。

這裏是我的SQL:

  --this is needed because stored procedure must know how many days are selected 
      DECLARE @daysCount int 
      SET @daysCount = 0 
      IF (@mon IS NOT NULL) 
       BEGIN 
        SET @daysCount = @daysCount+1 
       END 
      IF (@tue IS NOT NULL) 
       BEGIN 
        SET @daysCount = @daysCount+1 
       END 
      IF (@wed IS NOT NULL) 
       BEGIN 
        SET @daysCount = @daysCount+1 
       END 
      IF (@thu IS NOT NULL) 
       BEGIN 
        SET @daysCount = @daysCount+1 
       END 
      IF (@fri IS NOT NULL) 
       BEGIN 
        SET @daysCount = @daysCount+1 
       END 
      IF (@sat IS NOT NULL) 
       BEGIN 
        SET @daysCount = @daysCount+1 
       END 
      IF (@sun IS NOT NULL) 
       BEGIN 
        SET @daysCount = @daysCount+1 
       END 

      -- Insert statements for procedure here 
      SELECT * FROM (
       SELECT ROW_NUMBER() OVER 
       (
        ORDER BY 
        CASE WHEN @OrderBy = 'ND' THEN title END DESC, 
        CASE WHEN @OrderBy = 'NA' THEN title END, 
        CASE WHEN @OrderBy = '' THEN title END, 
        CASE WHEN @OrderBy = 'RD' THEN authorRating END DESC, 
        CASE WHEN @OrderBy = 'RA' THEN authorRating 
       ) AS Row, 
       Articles.ArticleId, Articles.userId, Articles.timestamp as datePosted, users.screenName, 
      defaultSmallImagePath, authorRating, ArticleCosts, title, 

      FROM Articles 
      LEFT OUTER JOIN 
       Users on Articles.userId = Users.userId 
      LEFT OUTER JOIN 
       ArticleAdditionalInformation ON ArticleAdditionalInformation.ArticleId = Articles.ArticleId 
      --JOIN FOR CONTINENT 
      LEFT OUTER JOIN 
       Codings as Continent ON Continent.codingKeyId = ArticleAdditionalInformation.continentId AND Continent.languageId = @languageId 
      -- JOIN FOR COUNTRY 
      LEFT OUTER JOIN 
       CodingsAssociated as Country ON Country.codingKeyId = ArticleAdditionalInformation.countryId AND Country.languageId = @languageId 
      -- JOIN FOR Article TRANSLATION DATA 
      LEFT OUTER JOIN 
       ArticlesTranslations ON ArticlesTranslations.ArticleId = Articles.ArticleId AND [email protected] 
      LEFT OUTER JOIN 
       ArticleCategories ON ArticleCategories.ArticleId = Articles.ArticleId 

      WHERE 
        (
        ArticleCategories.categorieId =1 OR ArticleCategories.categorieId =2 OR 
        ArticleCategories.categorieId =3 OR ArticleCategories.categorieId =4 OR 
        ArticleCategories.categorieId = 5 
        ) AND 
        (ArticlesTranslations.title LIKE '%' + @searchString + '%' OR @searchString IS NULL) 
        -- COST filter 
        AND 
        (ArticleCosts < @cost OR @cost = 0) 
        AND 
        (ArticleCosts > 0 OR @cost = 0) 
        --END cost filter 

        -- EXCLUDE already stored for selected days 
        AND Articles.ArticleId -- exclude these ArticleIds 
        NOT IN 
        (
         SELECT DailyContent.ArticleId 
         FROM DailyContent 
         WHERE 
          [email protected] AND 
          (
           [email protected] OR 
           [email protected] OR 
           [email protected] OR 
           [email protected] OR 
           [email protected] OR 
           [email protected] OR 
           [email protected] 
          ) 
         GROUP BY 
          DailyContent.ArticleId 
         HAVING 
          (COUNT(sectionId) = @daysCount) 
        ) 
        -- END exclude 

        ) p 
        WHERE (Row > @startRowIndex AND Row <[email protected] + @pageSize) 
      ORDER BY Row 
     END 

所以,我只想包括最喜歡的表中是唯一articleIds,當@includeFavourites參數不爲null。

任何暗示將不勝感激;)

我使用SQL Server 2008

回答

0

嘗試這一個 -

DECLARE @daysCount INT 
SELECT @daysCount = 
    ISNULL(@mon, 0) + -- if @mon = 1, @tue = 2, .... 1 + (2-1)=1 + (3-2)=1 + ... 
    ISNULL(@tue - 1, 0) + 
    ISNULL(@wed - 2, 0) + 
    ISNULL(@thu - 3, 0) + 
    ISNULL(@fri - 4, 0) + 
    ISNULL(@sat - 5, 0) + 
    ISNULL(@sun - 6, 0) 

SELECT * 
FROM (
    SELECT ROW_NUMBER() OVER 
    (
     ORDER BY 
     CASE WHEN @OrderBy = 'ND' THEN title END DESC, 
     CASE WHEN @OrderBy IN ('NA', '') THEN title END, 
     CASE WHEN @OrderBy = 'RD' THEN authorRating END DESC, 
     CASE WHEN @OrderBy = 'RA' THEN authorRating 
    ) AS [Row] 
    , a.ArticleId 
    , a.userId 
    , a.[timestamp] as datePosted 
    , u.screenName 
    , defaultSmallImagePath 
    , authorRating 
    , ArticleCosts 
    , title 
FROM dbo.Articles a -- always use schema and alias 
LEFT JOIN dbo.Users u on a.userId = u.userId -- OUTER is unnecessary 
LEFT JOIN dbo.ArticleAdditionalInformation aai ON aai.ArticleId = a.ArticleId 
LEFT JOIN dbo.Codings cd ON cd.codingKeyId = aai.continentId AND cd.languageId = @languageId 
LEFT JOIN dbo.CodingsAssociated c ON c.codingKeyId = aai.countryId AND c.languageId = @languageId 
LEFT JOIN dbo.ArticlesTranslations at ON at.ArticleId = a.ArticleId AND at.languageId = @languageId 
LEFT JOIN dbo.ArticleCategories ac ON ac.ArticleId = a.ArticleId 
WHERE ac.categorieId IN (1, 2, 3, 4, 5) 
    AND (
       at.title LIKE '%' + @searchString + '%' 
      OR 
       @searchString IS NULL 
     ) 
    AND (ArticleCosts < @cost OR @cost = 0) 
    AND (ArticleCosts > 0 OR @cost = 0) 
    AND a.ArticleId NOT IN (
     SELECT dc2.ArticleId 
     FROM dbo.DailyContent dc2 
     WHERE sectionId = @sectionId 
     AND (
       weekDayId % @daysCount = 0 -- possible it's works 
       --weekDayId = @mon OR 
       --weekDayId = @tue OR 
       --weekDayId = @wed OR 
       --weekDayId = @thu OR 
       --weekDayId = @fri OR 
       --weekDayId = @sat OR 
       --weekDayId = @sun 
     ) 
     GROUP BY dc2.ArticleId 
     HAVING COUNT(sectionId) = @daysCount 
    ) 
) p 
WHERE [Row] BETWEEN @startRowIndex AND @startRowIndex + @pageSize 
--ORDER BY [Row] -- ROW_COUNT already sorted your rows 
相關問題