1

这是存储过程,我需要选择 Distinct Records 并以随机顺序显示它们,但我面临一个错误,即选择 Distinct 不能与 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 Fk_CampaignID=@Fk_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()
4

3 回答 3

2

从查询中删除 NEWID() 并用

SELECT * FROM (
   <your query>
) as t
ORDER BY NEWID()
于 2012-11-18T12:59:14.637 回答
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
于 2012-11-18T12:49:40.017 回答
0

这个查询没有多大意义,没有聚合,那么group by语句的目的是什么?并且按 NEWID() 进行分组无论如何都不会给您带来任何分组。

将多余的 group by 与 distinct 子句结合起来,感觉更像是您有一个不正确的连接条件导致笛卡尔,并且您拼命尝试使用 distinct 和 group by 来消除该笛卡尔,拥有它们当然没有什么意义在声明中,以及 group by 中的 newid 都应该被删除。

于 2012-11-18T12:48:42.380 回答