带有错误消息的SQL服务器的CTE表

时间:2018-01-28 07:01:05

标签: sql sql-server tsql

;WITH myTree AS 
(
    SELECT 
        y.User_id, y.user_usercode, y.user_username,
        y.user_uplineID, 
        trans_WinLose, y.User_ID AS sourceID, trans_id,
        trans_Rolling, y.User_Level, 
        lvl1.User_Level AS Level_lvl1, lvl2.User_Level AS Level_lvl2,
        y.User_GivenPT, lvl1.User_GivenPT AS GivenPT_lvl1,
        lvl2.User_GivenPT AS GivenPT_lvl2, y.User_GivenComm,
        lvl1.User_GivenComm AS downline_Comm  
    FROM
        tbl_user y
    INNER JOIN 
        tbl_trans x ON x.trans_Robot_ID = y.User_RobotID
    INNER JOIN 
        tbl_user lvl1 ON y.user_uplineID = lvl1.User_ID 
    INNER JOIN 
        tbl_user lvl2 ON lvl1.user_uplineID = lvl2.User_ID 

    UNION ALL

    SELECT 
        u.User_ID, u.user_usercode, u.user_username, u.user_uplineID,
        t.trans_WinLose, t.sourceID AS sourceID, t.trans_Id, 
        t.trans_Rolling, u.User_Level, t.User_Level, t.Level_lvl1,
        u.User_GivenPT, t.User_GivenPT, t.GivenPT_lvl1, 
        u.User_GivenComm, t.User_GivenComm
    FROM
        myTree t
    INNER JOIN 
        tbl_user u ON t.user_uplineID = u.User_ID
 )
 SELECT * 
 FROM 
     (SELECT 
          mytree.*,
          (SELECT 
               CASE 
                  WHEN Level_lvl1 = 7 THEN GivenPT_lvl1 
                  WHEN level_lvl2 = 7 THEN User_GivenPT-GivenPT_Lvl2 
                  ELSE (CASE 
                           WHEN (User_GivenPT-GivenPT_lvl1) > 0 
                              THEN User_GivenPT - GivenPT_lvl1 
                              ELSE 0 
                        END) 
               END) AS Net_PT 
      FROM 
          Mytree 
      ORDER BY 
          mytree.trans_ID) AS c

我想通过mytree.trans_ID订购,但收到错误:

  

Msg 1033,Level 15,State 1,Line 17
  除非还指定了TOP,OFFSET或FOR XML,否则ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。

如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

另一种可能性是在查询CTE时删除外部查询 - 它似乎没有任何用途。

    ;with myTree as (
    select y.User_id,y.user_usercode,y.user_username,y.user_uplineID,trans_WinLose, y.User_ID as sourceID, trans_id,trans_Rolling,y.User_Level,lvl1.User_Level as Level_lvl1,lvl2.User_Level as Level_lvl2,y.User_GivenPT,lvl1.User_GivenPT as GivenPT_lvl1 ,lvl2.User_GivenPT as GivenPT_lvl2,y.User_GivenComm,lvl1.User_GivenComm as downline_Comm  from tbl_user y
         Inner join tbl_trans x on x.trans_Robot_ID = y.User_RobotID
            Inner join tbl_user lvl1 on y.user_uplineID = lvl1.User_ID 
            Inner join tbl_user lvl2 on lvl1.user_uplineID = lvl2.User_ID 
    union all
    select u.User_ID,u.user_usercode,u.user_username,u.user_uplineID,t.trans_WinLose, t.sourceID as sourceID, t.trans_Id,t.trans_Rolling,u.User_Level,t.User_Level,t.Level_lvl1,u.User_GivenPT,t.User_GivenPT,t.GivenPT_lvl1,u.User_GivenComm,t.User_GivenComm
          from myTree t
          inner join tbl_user u on t.user_uplineID = u.User_ID

     )
     SELECT mytree.*,
     (SELECT CASE 
     WHEN Level_lvl1=7 THEN GivenPT_lvl1 
     WHEN level_lvl2=7 THEN User_GivenPT-GivenPT_Lvl2 
     ELSE (CASE WHEN (User_GivenPT-GivenPT_lvl1) > 0 THEN User_GivenPT-GivenPT_lvl1 else 0 END) 
     END) as Net_PT 
     From Mytree order by mytree.trans_ID

答案 1 :(得分:0)

SQL以数学集方式处理数据。在集合中,数据和元素的顺序毫无意义。因此,当您处理用于返回一组数据的CTE,子查询,视图,内联表值函数等时,您不能对它们进行排序和排序。

当您需要在输出中显示数据或使用TOP或O FFSET-Fetch或etc命令时,您需要对数据进行排序,其中Order By可帮助这些命令工作应该是。

您应该将查询重写为:

;with myTree as (
select y.User_id,y.user_usercode,y.user_username,y.user_uplineID,trans_WinLose, y.User_ID as sourceID, trans_id,trans_Rolling,y.User_Level,lvl1.User_Level as Level_lvl1,lvl2.User_Level as Level_lvl2,y.User_GivenPT,lvl1.User_GivenPT as GivenPT_lvl1 ,lvl2.User_GivenPT as GivenPT_lvl2,y.User_GivenComm,lvl1.User_GivenComm as downline_Comm  from tbl_user y
     Inner join tbl_trans x on x.trans_Robot_ID = y.User_RobotID
        Inner join tbl_user lvl1 on y.user_uplineID = lvl1.User_ID 
        Inner join tbl_user lvl2 on lvl1.user_uplineID = lvl2.User_ID 
union all
select u.User_ID,u.user_usercode,u.user_username,u.user_uplineID,t.trans_WinLose, t.sourceID as sourceID, t.trans_Id,t.trans_Rolling,u.User_Level,t.User_Level,t.Level_lvl1,u.User_GivenPT,t.User_GivenPT,t.GivenPT_lvl1,u.User_GivenComm,t.User_GivenComm
      from myTree t
      inner join tbl_user u on t.user_uplineID = u.User_ID

 )select * from (SELECT mytree.*,
 (SELECT CASE 
 WHEN Level_lvl1=7 THEN GivenPT_lvl1 
 WHEN level_lvl2=7 THEN User_GivenPT-GivenPT_Lvl2 
 ELSE (CASE WHEN (User_GivenPT-GivenPT_lvl1) > 0 THEN User_GivenPT-GivenPT_lvl1 else 0 END) 
 END) as Net_PT 
 From Mytree) as c  
 order by c.trans_ID

答案 2 :(得分:-1)

From Mytree order by mytree.trans_ID OFFSET 0 ROWS

将其改为此,解决了