Sql Server选择不同的记录和OrderBy NEWID()

时间:2012-11-18 12:36:34

标签: sql sql-server

这是存储的过程,我需要选择不同的记录并以随机顺序显示它们但我面临一个错误,选择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()

3 个答案:

答案 0 :(得分: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 :(得分:1)

从查询中删除NEWID()并用

将其包围
SELECT * FROM (
   <your query>
) as t
ORDER BY NEWID()

答案 2 :(得分:0)

这个查询没有多大意义,没有聚合,那么group by语句的目的是什么?并且通过NEWID()进行分组无论如何都不会让你获得任何分组。

将多余的组合与distinct子句相结合,感觉更像是你的连接条件不正确导致笛卡儿,并且你拼命地尝试使用distinct和group by来消除笛卡尔,这当然没什么意义将它们都放在语句中,并删除group by中的newid。