排名计算电话号码

时间:2014-03-03 00:35:33

标签: sql sql-server tsql

感谢您提出的所有建议。在小型测试数据上工作正常,但我想我估计了总行数和计划,所以我想我必须发布一些大数据样本数据

这是:

CREATE TABLE MasterTable
(`Date` datetime, `PhNO` int, `Plan_name` varchar(13), `Plan_price` varchar(3))
;

INSERT INTO MasterTable
(`Date`, `PhNO`, `Plan_name`, `Plan_price`)
VALUES
('2014-01-01 13:00:00', 3232222, 'Basepack1', '$32'),
('2014-01-01 13:00:00', 3232222, 'Basepack2', '$31'),
('2014-01-01 13:00:00', 3221111, 'Basepack6', '$21'),
('2014-01-01 13:00:00', 543222, 'BaseValuePack', '$76'),
('2014-01-01 13:00:00', 543222, 'Basepack1', '$30'),
('2014-01-01 13:00:00', 32322221, 'Basepack1', '$37'),
('2014-01-01 13:00:00', 32322221, 'Basepack2', '$21'),
('2014-01-01 13:00:00', 32322354, 'Basepack7', '$23'),
('2014-01-01 13:00:00', 32322254, 'Basepack8', '$11'),
('2014-01-01 13:00:00', 3267767,  'Non-base1', '$21'),
('2014-01-01 13:00:00', 3267762,  'Non-base1', '$21'),
('2014-01-01 13:00:00', 32677676,  'Non-base3', '$76'),
('2014-01-01 13:00:00', 5267767,  'Non-base9', '$21')

现在我基本上想要将“休闲”类别

下的所有“非基本%”计划分组

这是所需的输出:

日期计划名称Phone_no_count

'2014-01-01 13:00:00''Basepack1'2 '2014-01-01 13:00:00''Basepack2'0 '2014-01-01 13:00:00''Basepack6'1 '2014-01-01 13:00:00''BaseValuepack'1 '2014-01-01 13:00:00''休闲'4

由于

........................................... 以前的要求: 我需要计算按某些计划分类的所有电话号码。但是为了将这些计划分组,我需要根据它们的价格对它们进行排名。

这是ddl

CREATE TABLE MasterTable
    (`Date` datetime, `PhNO` int, `Plan_name` varchar(13), `Plan_price` varchar(3))
;

INSERT INTO MasterTable
(`Date`, `PhNO`, `Plan_name`, `Plan_price`)
VALUES
('2014-01-01 13:00:00', 3232222, 'Basepack1', '$32'),
('2014-01-01 13:00:00', 3232222, 'Basepack2', '$31'),
('2014-01-01 13:00:00', 3221111, 'Basepack6', '$21'),
('2014-01-01 13:00:00', 543222, 'BaseValuePack', '$76'),
('2014-01-01 13:00:00', 543222, 'Basepack1', '$30'),
('2014-01-01 13:00:00', 32322221, 'Basepack1', '$37'),
('2014-01-01 13:00:00', 32322221, 'Basepack2', '$21')
;

现在的规则是,我只需要根据计划价格(较高的一个)计算一次超过1计划的电话。 但也有一种情况,手机没有两个不同的'包' - Basepack和valuepack,这意味着我需要计算该手机不仅一次分类在价值包(在这种情况下,我们需要忽略价格)。

这是所需的输出。

Date                  Plan_name     Phone_no_count
-------------------------------------------------
'2014-01-01 13:00:00' 'Basepack1'     2
'2014-01-01 13:00:00' 'Basepack2'     0
'2014-01-01 13:00:00' 'Basepack6'     1
'2014-01-01 13:00:00' 'BaseValuepack' 1

如何使用rank函数来实现此结果?

2 个答案:

答案 0 :(得分:0)

试试这段代码,假设它是mysql,因为发布的代码看起来像mysql

SELECT date, 
   plan_name, 
   Sum((SELECT Count(1) 
        FROM   mastertable T2 
        WHERE  t2.phno = tt.phno 
               AND t2.date = Tt.date 
               AND ( tt.plan_price > t2.plan_price 
                      OR TT.count = 1 ))) CNT 
FROM   (SELECT *, 
           (SELECT Count(1) 
            FROM   mastertable Ti 
            WHERE  ti.date = T.date 
                   AND ti.phno = t.phno) AS Count 
    FROM   mastertable T) Tt 
GROUP  BY date, 
      plan_name 

以下是SQL Fiddle Demo

答案 1 :(得分:0)

;with cte as
(
select *,ROW_NUMBER()over(partition by phno order by plan_price desc)rn 
from @MasterTable

)
,
cte1 as
(
select a.Plan_name,count(*)[Phone_no_count] from  cte a
 where rn=1
 group by a.Plan_name
)
select distinct a.dates,a.Plan_name,isnull(b.Phone_no_count,0)[[Phone_no_count]  
from @MasterTable a left join cte1 b on a.Plan_name=b.Plan_name