需要有关以下sql查询的帮助

时间:2012-10-25 17:10:46

标签: sql sql-server-2008 tsql

这是我当前的SQL查询。我需要修改这个

SELECT 
    18 BrokerDealerID,
    5 PortfolioID,
    PeriodEndDate DATE,
    SecurityIdentifier_All.SecurityId,
    MAX(
    (CASE 
        WHEN Securities.Quantity < 0 THEN
        100 + (100 - LocalPriceAmount)
        ELSE
        LocalPriceAmount
     END
        ) /100) Mark
FROM
    Fireball_Reporting..StateStreet_DailyPosition_Second StateStreet 
INNER JOIN
    Fireball_Reporting..SecurityIdentifier_All ON StateStreet.CUSIP = SecurityIdentifier_All.Identifier
INNER JOIN
    Fireball..TradeBySecurityType Securities ON
        Securities.PricingSecurityID = SecurityIdentifier_All.SecurityId AND Securities.Position = 1 AND
(CASE WHEN StateStreet.SecurityName LIKE '% R V %' THEN StateStreet.SharesParValue * -1 ELSE StateStreet.SharesParValue END) = Securities.Quantity
WHERE
    CONVERT(DATETIME, StateStreet.PeriodEndDate) = '2012-10-23' --@PositionDate
GROUP BY
    PeriodEndDate,
    SecurityIdentifier_All.SecurityId

我需要改变第二个条件,即

(CASE WHEN StateStreet.SecurityName LIKE '% R V %' THEN StateStreet.SharesParValue * -1 ELSE StateStreet.SharesParValue END) = Securities.Quantity

我会举个例子。

我的选择查询给出了以下输出

securityname date       securityid portfolioid type mark               quantity
------------ ---------- ---------- ----------- ---- ------------------ -------------
R V DISH     10/23/2012 4879505    5           CDS  1.0487189900000000 -5000000.0000
R V DISH      10/23/2012 4879505    5           CDS  1.0487189900000000 -2000000.0000
R F DISH    10/23/2012 4879505    5           CDS  0.9512810100000000 3000000.0000

上面给出了3条相同安全ID的记录 当我使用上述结果进行MERGE时,如果我没有检查检查Quantity的第二个条件但是现在我只想检查 当In StateStreet.SecurityName LIKE'%R V%'从上面的数据为-的结果中获取记录时,如果没有,则取出数量为+的记录

我怎么能改变那个案例陈述?或者在我计算出的标记的select语句中直接检查% R V%? 需要技术帮助。 我是SQL的新手。

1 个答案:

答案 0 :(得分:1)

可能是这样的:

(
      (StateStreet.SecurityName LIKE '% R V %' AND Securities.Quantity < 0)
   OR (StateStreet.SecurityName NOT LIKE '% R V %' AND Securities.Quantity >= 0)
)

我只是猜测哪个有“=”(+可以包含0)。