有没有人可以帮我创建'SORT'专栏。
这是两个例子,它应该如何运作。
请注意,第二个表中的最后两行是4和5,因为类型列更改为“k”,而数字是新的且日期相同。
示例1
DELIMITER $$
ALTER ALGORITHM=UNDEFINED DEFINER=`user_name`@`` SQL SECURITY DEFINER VIEW `VW_viewname` AS
SELECT
`DOM`.`CustomerID` AS `CustomerID`,
`DOM`.`User` AS `User`,
`DOM`.`DemoOrderDate` AS `DemoOrderDate`
FROM `Demo_Orders_M` `DOM`
WHERE (`DOM`.`DemoOrderStatus` = 253)
GROUP BY `DOM`.`CustomerID`
HAVING (COUNT(`DOM`.`CustomerID`) > 1)$$
DELIMITER ;
示例2
date number type sort
2012-03-23 3660 d 1
2015-06-01 1890 k 2
2015-06-02 8370 k 3
2015-06-11 4345 d 4
2015-06-11 4345 d 5
2015-06-11 4345 d 6
2015-06-11 4345 d 7
2015-06-11 4345 d 8
2015-06-11 4345 d 9
2015-06-11 3534 k 10
2015-06-11 3534 k 11
答案 0 :(得分:1)
有点奇怪的要求,但这是解决方案:
select date, number, type, dense_rank() over(order by date, type asc, rank asc) rank2 from (
select row_number() over (partition by date, number, type order by date asc,type desc) rank, * from testTable
) a order by date, number desc, rank2 asc
而sql小提琴: