如何使用带有Having子句的Result列

时间:2014-09-15 11:01:45

标签: sql sql-server stored-procedures sql-server-2012

我有一个存储过程,

我想在列HAVINGIsUserFruitEater中使用IsUserOnSite子句,但它表示列不存在。

不确定如何使用列,必须有办法使用存储过程结果的列...

ALTER procedure [dbo].[Risks_GetFruitCratesForUser]
(
    @UserID VARCHAR(200),
    @URLFilter varchar(256)
)
AS
SELECT DISTINCT 
    Fruits.*
    ,FruitsCrate.* dbo.IsUserRegularFruitEater(@UserID, FruitsCrate.FutureID) AS 
    IsUserFruitEater
    ,dbo.IsUserOnSite(@UserID, FruitsCrate.FID) AS IsUserOnSite
    ,COALESCE(doThis.deeraw, - 1) AS deeraw
    ,COALESCE(doThat.HeroSheema, - 1) AS HeroSheema
    ,Jootey.deeraw * Jootey.HeroSheema AS BigBang
FROM someTable....
WHERE blahblah NOT NULL
    AND blahblah2 LIKE "abc"
HAVING Min(IsUserFruitEater) > 0
    OR Min(IsUserOnSite) > 0

1 个答案:

答案 0 :(得分:1)

使用子查询和where子句:

SELECT t.*
FROM (SELECT DISTINCT Fruits.*, FruitsCrate.*
            dbo.IsUserRegularFruitEater(@UserID, FruitsCrate.FutureID) AS IsUserFruitEater,
            dbo.IsUserOnSite(@UserID, FruitsCrate.FID) AS IsUserOnSite,
            COALESCE(doThis.deeraw, -1) As deeraw  , COALESCE(doThat.HeroSheema, -1) As HeroSheema,
            Jootey.deeraw * Jootey.HeroSheema As BigBang
      From someTable....
      WHERE blahblah NOT NULL AND blahblah2 LIKE "abc"
     ) t
WHERE IsUserFruitEater > 0 or IsUserOnSite > 0;

我不确定min()的含义。您没有group by,这是使用having子句的问题。