0

我的老板给了我一个有趣的挑战。我们有一个数据集来提取页面上的某些广告商,但如果您是登录的广告商,他希望确保人员广告首先显示。这是现在的脚本:

SELECT TOP 15
a.MemberID,             -- 0
a.UserName,             -- 1
a.MemberDisplayName,    -- 2
a.NYKABizName           -- 3
FROM Member a
INNER JOIN AdDisplay b ON b.MemberID = a.MemberID
WHERE a.Claimed = 'Y'
AND a.NYKACatID IS NOT NULL
AND b.LocalPageID = @LocalPageID
AND a.SBIcon = 'N'
AND a.Viewable = 'Y'
AND a.ProfileTypeID <> 1
AND a.PackageType <> 'G'
AND a.PackageType <> 'P'
AND a.PackageType <> 'C'
ORDER BY PackageType ASC

我想添加一些像 Include MemberID = 102。我怎样才能改变这个脚本来完成老板的愿望。

提前谢谢了,

新杰克

4

3 回答 3

4

假设查询返回有问题的广告商(没有前 15 个),您可以通过修改 order by 子句来执行此操作:

order by (case when MemberId = 102 then 0 else 1 end),
         PackageType ASC

这对列表进行排序,将所需成员放在首位,然后是其他所有成员。您仍然可以在查询中保留前 15 个。

于 2012-07-12T15:51:20.577 回答
0

我在这里做了很多假设,但想法是将您的登录用户表添加到查询中,并确保显示该表中有记录的任何人。

SELECT TOP 15
a.MemberID,             -- 0
a.UserName,             -- 1
a.MemberDisplayName,    -- 2
a.NYKABizName           -- 3
FROM Member a
INNER JOIN AdDisplay b ON b.MemberID = a.MemberID
LEFT OUTER JOIN LoggedInUsers l ON a.MemberID = l.MemberID
WHERE (a.Claimed = 'Y'
AND a.NYKACatID IS NOT NULL
AND b.LocalPageID = @LocalPageID
AND a.SBIcon = 'N'
AND a.Viewable = 'Y'
AND a.ProfileTypeID <> 1
AND a.PackageType <> 'G'
AND a.PackageType <> 'P'
AND a.PackageType <> 'C')
OR l.MemberID IS NOT NULL
ORDER BY IF(l.MemberID IS NOT NULL,0,1), PackageType ASC

当然,如果您只想静态包含​​ MemberID = 102,那么@GordonLinoff 的答案是正确的。也许我对你的问题读得太多了。

于 2012-07-12T15:51:59.583 回答
0

您可以编写类似于第一个查询的第二个查询,但它只返回您已登录成员的结果,并让两个查询都包含一个附加值来标识它们来自哪个子查询,然后将它们合并并在外部从它们中选择查询哪个命令他们。

我知道这是一个连续的句子,所以让我看看我是否可以用代码详细说明......

SELECT TOP 15 * FROM (
    SELECT
      0 as loginStatus,
      a.MemberID,             -- 0 
      a.UserName,             -- 1 
      a.MemberDisplayName,    -- 2 
      a.NYKABizName           -- 3 
    FROM Member a 
    INNER JOIN AdDisplay b 
    ON b.MemberID = a.MemberID 
    WHERE a.Claimed = 'Y' 
    AND a.NYKACatID IS NOT NULL 
    AND b.LocalPageID = @LocalPageID 
    AND a.SBIcon = 'N' 
    AND a.Viewable = 'Y' 
    AND a.ProfileTypeID <> 1 
    AND a.PackageType <> 'G' 
    AND a.PackageType <> 'P' 
    AND a.PackageType <> 'C' 
    AND a.MemberID IN (@memberIdList)

    UNION

    SELECT
      1 as loginStatus,
      a.MemberID,             -- 0 
      a.UserName,             -- 1 
      a.MemberDisplayName,    -- 2 
      a.NYKABizName           -- 3 
    FROM Member a 
    INNER JOIN AdDisplay b 
    ON b.MemberID = a.MemberID 
    WHERE a.Claimed = 'Y' 
    AND a.NYKACatID IS NOT NULL 
    AND b.LocalPageID = @LocalPageID 
    AND a.SBIcon = 'N' 
    AND a.Viewable = 'Y' 
    AND a.ProfileTypeID <> 1 
    AND a.PackageType <> 'G' 
    AND a.PackageType <> 'P' 
    AND a.PackageType <> 'C'
)
ORDER BY loginStatus
于 2012-07-12T15:52:07.587 回答