2012-11-18 69 views
1

這是存儲procdure,我需要選擇不同的記錄,並以隨機順序顯示它們,但我面臨一個錯誤,選擇Distinct不能用於newid(),所以我怎麼能走過這個?Sql服務器選擇不同的記錄和OrderBy NEWID()

USE [OtlobODR] 
GO 
/****** Object: StoredProcedure [OtlobFood].[ListOffersItems] Script Date: 11/18/2012 13:01:34 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER procedure [OtlobFood].[ListOffersItems] 
@Fk_CampaignID int 
as 
select distinct 
    CampaignID, CampaignOffers.OldPrice 
    , dbo.ItemSizes.IS_Id, 
    , dbo.Items.[Item_Description_L2] as Item_Description 
    , dbo.Items.[Item_Image] 
    , dbo.Items.[Item_Details] 
    , dbo.ItemSizes.[IS_Price] as Price 
    -- if null then it is not a featured meal 
    , dbo.ProviderItems.[PI_Id] as ProviderItems_PI_ID 
    , dbo.ItemCategories.[ItemCat_Id] 
    , dbo.Providers.Provider_Name_L2 as Provider_Name 
    , dbo.Providers.Provider_Menu_Logo 
    , dbo.Providers.Provider_Id 
FROM Items 
INNER JOIN ProviderItems ON Items.Item_Id = ProviderItems.Item_Id 
INNER JOIN dbo.ItemSizes ON dbo.Items.Item_Id = dbo.ItemSizes.Item_Id 
INNER JOIN CampaignOffers ON CampaignOffers.ItemID = ItemSizes.IS_Id 
INNER JOIN dbo.ItemCategories ON dbo.Items.ItemCat_Id = dbo.ItemCategories.ItemCat_Id 
INNER JOIN dbo.Providers ON dbo.ProviderItems.Provider_Id = dbo.Providers.Provider_Id 
INNER JOIN dbo.Branches ON dbo.Providers.Provider_Id = dbo.Branches.Provider_Id 
where [email protected]_CampaignID 
group by NEWID(),  
    CampaignID, CampaignOffers.OldPrice , 
    dbo.ItemSizes.IS_Id, 
    dbo.Items.[Item_Description_L2], 
    dbo.Items.[Item_Image], 
    dbo.Items.[Item_Details], 
    dbo.ItemSizes.IS_Id, 
    dbo.ItemSizes.[IS_Price] , 
    -- if null then it is not a featured meal 
    dbo.ProviderItems.[PI_Id] , 
    dbo.ItemCategories.[ItemCat_Id], 
     dbo.Providers.Provider_Name_L2, 
    dbo.Providers.Provider_Menu_Logo 
    ,dbo.Branches.Branch_Id, 
    dbo.Providers.Provider_Id,CampaignID,CampaignOffers.OldPrice 
order by NEWID() 

回答

0

這個查詢沒什麼意義,沒有聚合,那麼group by statement的目的是什麼?並且通過NEWID()進行分組無論如何不會讓你獲得任何分組。

將多餘的組合與獨特的子句結合起來,感覺更像是你有一個不正確的連接條件導致笛卡爾,並且你拼命地嘗試使用不同的和分組的by來消除該笛卡爾,它肯定會使得很少意識到他們都在聲明中,而團隊中的新人應該被刪除。

2

您需要將SELECT DISTINCT推入內部查詢(此時您也可以丟失GROUP BY),然後在外部查詢中執行NEWID()。一般形式爲

select 
    newid(), X.* 
from 
    (
    select distinct <cols> 
    from <tables> 
    where <whatever> 
    ) X 
order by 1 
你的情況

我想這是你想要什麼:

select 
    newid(), X.* 
from 
    (
    select distinct 
     CampaignID, CampaignOffers.OldPrice , 
     dbo.ItemSizes.IS_Id, 
     dbo.Items.[Item_Description_L2] as Item_Description , 
     dbo.Items.[Item_Image], 
     dbo.Items.[Item_Details], 
     dbo.ItemSizes.[IS_Price] as Price, 
     -- if null then it is not a featured meal 
     dbo.ProviderItems.[PI_Id] as ProviderItems_PI_ID, 
     dbo.ItemCategories.[ItemCat_Id], 
     dbo.Providers.Provider_Name_L2 as Provider_Name, 
     dbo.Providers.Provider_Menu_Logo, 
     dbo.Providers.Provider_Id 
    FROM 
     Items 
     INNER JOIN ProviderItems ON Items.Item_Id = ProviderItems.Item_Id 
     INNER JOIN dbo.ItemSizes ON dbo.Items.Item_Id = dbo.ItemSizes.Item_Id 
     inner join CampaignOffers ON CampaignOffers.ItemID = ItemSizes.IS_Id 
     INNER JOIN dbo.ItemCategories ON dbo.Items.ItemCat_Id = dbo.ItemCategories.ItemCat_Id 
     INNER JOIN dbo.Providers ON dbo.ProviderItems.Provider_Id = dbo.Providers.Provider_Id 
     INNER JOIN dbo.Branches ON dbo.Providers.Provider_Id = dbo.Branches.Provider_Id 
    where 
     Fk_CampaignID = @Fk_CampaignID 
    ) X 
order by 1 
1

取出NEWID()從查詢的,並與

SELECT * FROM (
    <your query> 
) as t 
ORDER BY NEWID() 
環繞它