在案例陈述中选择行

时间:2016-08-23 17:11:11

标签: mysql sql

我在if let user = user { 下方有<{p}}

PFAnonymousUtils.logIn{ (user, error) in
            if error == nil {
                print("Success")
            }
            else {
                print("Failed")
            }
        }

我有一个case语句,它将该表中的数据提取到另一个定义的表中。

a

有没有办法在case语句中指定行号,而不是对case语句中的表YEAR_WW 2016-23 2016-24 2016-25 2016-26 2016-27 2016-28 2016-29 2016-30 2016-31 2016-32 2016-33 2016-34 2016-35 进行硬编码?即:

SELECT Family,Value,YEAR_WW
            ,weekly_qty = CASE WHEN YEAR_WW = '2016-23' THEN week1
                            WHEN YEAR_WW = '2016-24' THEN week2
                            WHEN YEAR_WW = '2016-25' THEN week3
                            WHEN YEAR_WW = '2016-26' THEN week4
                            WHEN YEAR_WW = '2016-27' THEN week5
                            WHEN YEAR_WW = '2016-28' THEN week6
                            WHEN YEAR_WW = '2016-29' THEN week7
                            WHEN YEAR_WW = '2016-30' THEN week8
                            WHEN YEAR_WW = '2016-31' THEN week9
                            WHEN YEAR_WW = '2016-32' THEN week10
                            WHEN YEAR_WW = '2016-33' THEN week11
                            WHEN YEAR_WW = '2016-34' THEN week12
                            ELSE week13 END
FROM sample_table

1 个答案:

答案 0 :(得分:1)

如果我已正确理解您的样本表;

我认为你可以使用ROW_NUMBER()

SELECT Family, Value, YEAR_WW
    , CONCAT('week', ISNULL(CONVERT(VARCHAR, i),13))
FROM sample_table X
LEFT JOIN (
            SELECT ROW_NUMBER() OVER(ORDER BY YEAR_WW) AS i, YEAR_WW
            FROM a
            ) Y
ON X.weekly_qty = Y.YEAR_WW

为了安全起见,您可能希望使用ORDER BY CONVERT(INT, LEFT(YEAR_WW,4)*52)+CONVERT(INT, RIGHT(YEAR_WW,2)) 而不只是ORDER BY YEAR_WW