使用 SQL Server 2012。我正在使用变量来确定在我的整个数据集中满足“各种标准”的次数。我想把这些实例的一半做一件事“first_half_thing”,另一半做一个“second_half_thing”。当我只包含满足条件的那些实例时,它可以正常工作。但我想用 3 或 4 个不同版本的“各种标准”对我的整个数据集执行此操作。
我的第一次尝试是在满足“各种条件”时尝试更改@CriteriaMetCount 的值,并使用该计数来执行“first_half_thing”或“second_half_thing”,但这不起作用。有没有办法Partition by
然后使用不同的“各种标准”
declare @TargetNumber BIGINT
declare @TargetHalf BIGINT
declare @CriteriaMetCount BIGINT
set @CriteriaMetCount=1
Set @TargetNumber=(
select count(distinct(AI2.AccountID)) from Table_AccountInfo AI2
where
AI2.OrgID in (
select distinct(Org2.OrgID) from Table_Organization Org2
where Org2.OrganizationName like ('%Name%'))
and (various criteria here)
)
Set @TargetHalf= (select iif(
(@TargetNumber%2)>0,((@TargetNumber+1)/2),(@TargetNumber/2)))
select AI.AccountID, @targethalf, @targetnumber,ROW_Number() over(order by AI.AccountID) AS ROW_COUNT,
case when ((ROW_Number() over(order by AI.AccountID))<=@targethalf) then 'first_half_thing' else 'second_half_thing' end,
iif(((ROW_Number() over(order by AI.AccountID))%2=0),(@CriteriaMetCount+1),@CriteriaMetCount)
from Table_AccountInfo LI
where AI.AccountID >= @TargetNumber
and AI.OrgID in (
select distinct(Org.OrgID) from Table_Organization Org
where Org.OrganizationName like ('%Name%'))
and (various criteria here)