为表写一个查询并给出一个分数

时间:2016-12-21 00:26:01

标签: sql sql-server

我正在尝试为以下内容编写查询:

计算过去90天内的交易次数。注意:这需要对新成员进行平均= =(交易数/天为成员)x 90

新成员是DateCreated从今天起0到90天之间的成员。

表格结构:

 Column Name         Datatype
---------------------------------
Member_No           nvarchar(255)
Order_No            int
Transaction_Date    datetime 
Net                 money
Date_Created        datetime

此外,最后一步是在计算交易后,我需要给出一个分数。因此,如果一名成员的数量超过8,则得分为5。

以下是范围:`

Transaction count      Score
  >8                  5
  6-8                 4
  4-6                 3
  2-4                 2
  0-2                 1

示例数据:

Member No   Sales Order Number  Date Created    Transaction Date    Net
M1                2332            01-10-15       10-07-16           354
M2                2311            12-12-16       14-12-16           53
M3                5422            04-10-14       07-10-14           35
M5                4535            10-10-16       12-11-16           54
M9                5522            03-05-15       07-10-15           55
M3                5422            04-10-14       02-12-16           83
M5                4534            10-10-16       13-12-16           73
M3                5432            04-10-14       09-10-14           35
M3                5484            04-10-14       11-10-16           34
M3                5453            04-10-14       07-11-16           67
M3                5474            04-10-14       09-11-16           56
M3                5493            04-10-14       07-12-16           52
M3                5452            04-10-14       10-12-16           75
M3                5496            04-10-14       11-12-16           34
M3                5442            04-10-14       13-12-16           90
M3                5494            04-10-14       14-12-16           757
M3                5464            04-10-14       16-12-16           72
M5                4542            10-10-16       15-12-16           76
M5                4502            10-10-16       17-12-16           72
M5                4535            10-10-16       18-12-16           43

输出:

Member No   Order Count (In last 90 days)   Score
  M1                    0                      1
  M2                   10                      5
  M3                    9                      5
  M5                    5                      3
  M9                    0                      1

[注意:

  

M1为0,因为DateCreated从今天起不到90天(不是新成员),并且该成员在过去90天内没有成交   天

     

M2订单数量为10,因为DaetCreated是从今天起90天内(新会员),交易日期也在90以内   天所以应用公式(No of         交易/天是会员)x 90)=(1/10)* 90 = 10这是订单数> 8+因此得分为5.

     

M3订单数量为9,因为该会员在过去90天内已成交9次。得分为5。

     

M5订单数量为5,因为DaetCreated是从今天起90天内(新会员),他的交易日期也在   90天所以应用公式(交易数量/天数为a   成员)x 90)=(4/72)* 90 = 5因此是4-6范围内的ordercount   得分为3。

如果有任何疑问,请告诉我。

2 个答案:

答案 0 :(得分:0)

以下内容完全未经测试,但至少应该让您了解可以使其运行的过程。

我建议你让每一步都执行,并尝试了解他们做了什么以及如何做。

第一步:计算过去90天内的交易。

select Member_No, count(Order_No) as TransactionCount
from Sales
where datediff(day, Transaction_Date, getdate()) <= 90
group by Member_No

第二步:获取每位成员的开始日期和年龄

select 
    Member_No, 
    min(Date_Created) as Date_Created, 
    datediff(day, min(Date_Created), getdate()) as Days_Old
from Sales
group by Member_No

第三步:结合以上

select *
from (
    select 
        Member_No, 
        min(Date_Created) as Date_Created, 
        datediff(day, min(Date_Created), getdate()) as Days_Old
    from Sales
    group by Member_No
) dc
join (
    select Member_No, count(Order_No) as TransactionCount
    from Sales
    where datediff(day, Transaction_Date, getdate()) <= 90
) tc on tc.Member_No=dc.Member_No

第四步:新成员的按比例交易计数

select 
    Member_No,
    case when dc.Days_Old > 90 
        then tc.Transaction_Count
        else tc.Transaction_Count * 90 / dc.Days_Old
    end as Transaction_Count        
from (
    select 
        Member_No, 
        min(Date_Created) as Date_Created, 
        datediff(day, min(Date_Created), getdate()) as Days_Old
    from Sales
    group by Member_No
) dc
join (
    select Member_No, count(Order_No) as TransactionCount
    from Sales
    where datediff(day, Transaction_Date, getdate()) <= 90
) tc on tc.Member_No=dc.Member_No

第五步,根据交易次数添加分数(请注意,我更改了分数表,使其不重叠):

select
    *,
    case 
        when x.Transaction_Count > 8 then 5
        when x.Transaction_Count between 6 and 7 then 4
        when x.Transaction_Count between 4 and 5 then 3
        when x.Transaction_Count between 2 and 3 then 2
        else 1
    end as Score
from (
    select 
        Member_No,
        case when dc.Days_Old > 90 
            then tc.Transaction_Count
            else tc.Transaction_Count * 90 / dc.Days_Old
        end as Transaction_Count        
    from (
        select 
            Member_No, 
            min(Date_Created) as Date_Created, 
            datediff(day, min(Date_Created), getdate()) as Days_Old
        from Sales
        group by Member_No
    ) dc
    join (
        select Member_No, count(Order_No) as TransactionCount
        from Sales
        where datediff(day, Transaction_Date, getdate()) <= 90
    ) tc on tc.Member_No=dc.Member_No
) x

答案 1 :(得分:0)

我相信这是一个基本的聚合查询,还有一些额外的逻辑:

select memberno,
       order_count_90_days,
       (case when days_ago_start >= 90
             then (case when order_count_90_days > 8 then 5
                        when order_count_90_days > 6 then 4
                        when order_count_90_days > 4 then 3
                        when order_count_90_days > 2 then 2
                        else 1
                   end)
             else (case when (order_count_90_days * 90.0 / days_ago_start) > 8 then 5
                        when (order_count_90_days * 90.0 / days_ago_start) > 6 then 4
                        when (order_count_90_days * 90.0 / days_ago_start) > 4 then 3
                        when (order_count_90_days * 90.0 / days_ago_start) > 2 then 2
                        else 1
                   end)
        end) as score                
from (select memberno,
             datediff(day, min(transactiondate), getdate()) as days_ago_start,
             sum(case when transactiondate >= getdate() - 90 then 1 else 0 end) as order_count_90_days
      from sample
      group by memberno
     ) m;