我有一张这样的表:
RP Type
列指定费率计划的类型,可以是(低,高,正常)
我想创建一个视图,在其中我可以看到每个订阅者的数量'这可能是高,正常,低。
我猜应该是这样的:
Create view t as
select
SubID,
Count(something) as NumeOfHIGH,
Count(Something) as NumOfLOW,
Count(Something) as NumOfNormal
Group by SubID
我可能错了。谢谢你
答案 0 :(得分:3)
您可以通过以下方式形成查询:
SELECT SubID,
SUM (
CASE
WHEN RP_Type='High' THEN 1
ELSE 0 END
) AS NumOfHigh,
SUM (
CASE
WHEN RP_Type='Low' THEN 1
ELSE 0 END
) AS NumOfLow,
SUM (
CASE
WHEN RP_Type='Normal' THEN 1
ELSE 0 END
) AS NumOfNormal
FROM
<table_name>
Group by SubID
如果高,低和普通类别中分别有多个RP_Type
,则可以在每个类别中添加WHEN
。
有关更多信息和参考:Conditional Processing using CASE
答案 1 :(得分:0)
我试过这个:
select SubscriberID
,COUNT(*) AS NumOfPlans
,SUM([SP active days]) as ActiveDays
,SUM(Case when [RP Type]='low' then 1 else 0 end ) as #LowPlan
,SUM(Case when [RP Type]='high' then 1 else 0 end ) as #NormalPlan
,SUM(Case when [RP Type]='high' then 1 else 0 end ) as #HighPlan
,SUM(Case when [RP Type]='promotion' then 1 else 0 end ) as #PromotionsPlan
,SUM(Case when [RP Type]='portal' then 1 else 0 end ) as #PortablePlan
,SUM(Case when [RP Type]='newyear' then 1 else 0 end ) as #NewYearPlan
,SUM(Case when [RP Type]='hamaval1' then 1 else 0 end ) as #HamrahAval1Plan
,SUM(Case when [RP Type]='hamaval2' then 1 else 0 end ) as #HamrahAval2Plan
,SUM(Case when [RP Type]='samsung' then 1 else 0 end ) as #SamsungPlan
,SUM(Case when [RP Type]='DEMO' then 1 else 0 end ) as #DemoPlan
,SUM(Case when [RP Type]IS null then 1 else 0 end ) as #NuLL
,SUM([Extra Quota]) as SumGIG
from [Cus Consumption].[dbo].CustData
where [Expiry Date]<= '2014 - 01 - 15'
group by [SubscriberID]
讽刺的是,我没有回答正确答案。我不知道为什么@Rohit Aggrawal
答案 2 :(得分:-1)
这有用吗?
Create view t as
select From [tableName]
[Rp Type],count(*) as NumberOfSubscribers
Group by [Rp Type]
这将为您提供每种类型的订阅者数量,并键入
希望这有用