SQL:排名错误或使用计数?

时间:2017-12-15 02:22:48

标签: sql sql-server

我正在努力实现这个结果:

enter image description here

和我的2个表是交易订单表:orderdatecountrymonthvalid order。第二个表格:customer idnew membersegment

我在下面尝试过以下代码:

SELECT 
    Distinct od.orderdate AS orderdate, od.country AS country,
    od.month ,c.cust_id as customerid
    Rank over () ( Partition by c.cust_id order by od.orderdate DESC) as rank
FROM 
    order_transaction_table as od
    INNER JOIN customertable as c
ON 
    od.cust_id=c.cust_id
    AND od.country=c.country
WHERE 
    od.country=2
    AND od.month between 201701 and 201711
    AND od.valid_order=1
    AND c. country = 2
    AND c.month between 201701 and 201711
    AND c.new member =1
    AND c.segment = 1
ORDER BY 
    od.month;

我继续遇到上述代码无效的错误。我不确定我哪里出错了。我试过命令'依靠c.custid'但它也没有成功。 感谢有人可以提供帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

只是为了解决RANK上的问题 查询应该是

Rank () over (Partition by  c.cust_id order by od.orderdate DESC) as rank