如何在SQL Access中连接IIF

时间:2017-02-12 13:13:54

标签: sql ms-access iif-function

我要做的是创建一个表,其中我有两列,第二列是Variable1的聚类,根据其值对其进行分类;如果< 0.1,然后我要显示“0-10”,如果值在0.1和0.2之间,则显示“11 - 20”,如果是其他,则显示“21 - 100”。当我运行下面的代码时,出现“缺少语法操作符错误”。

SELECT Variable2_name,
       IIF(Variable1_name< 0.1,
           "0 - 10",
           IIF(Variable1_name >= 0.1 AND Variable1_name < 0.2,
               "11 - 20",
               "21 - 100")
       ) AS Bucket
FROM Table
GROUP BY Variable2_name,
         IIF(Variable1_name < 0.1,
             "0 - 10",
             IIF(Variable1_name < 0.1,
                 "0 - 10",
                 IIF(Variable1_name >= 0.1 AND Variable1_name < 0.2,
                     "11 - 20",
                     "21 - 100")
             ),
ORDER BY Variable2_name

问题必须在IIF条款中,因为当我有一个简单的IIF条款时它才能正常工作。语法有什么问题?在另一个IIF子句中编写连接IIF子句的方法是什么,以及如何在GROUP BY子句中包含它?

非常感谢!

非常感谢

2 个答案:

答案 0 :(得分:2)

您在代码中有明显的语法错误(iif()中有两个selectgroup by中有三个switch。通常,iif()比嵌套case更容易使用。它与其他数据库中的SELECT Variable2_name, SWITCH(Variable1_name < 0.1, "0 - 10", Variable1_name < 0.2, "11 - 20", "21 - 100" ) AS Bucket FROM Table GROUP BY Variable2_name, SWITCH(Variable1_name < 0.1, "0 - 10", Variable1_name < 0.2, "11 - 20", "21 - 100" ) ORDER BY Variable2_name; 非常相似:

SWITCH()

注意:因为它通过GROUP BY迭代的逻辑,你不需要两个部分用于第二个条件。这也减少了出错的可能性。

其次,您没有使用任何SELECT DISTINCT函数,因此您可以进一步使用SELECT DISTINCT Variable2_name, SWITCH(Variable1_name < 0.1, "0 - 10", Variable1_name < 0.2, "11 - 20", "21 - 100" ) AS Bucket FROM Table ORDER BY Variable2_name;

DISTINCT

而且,如果您知道值已经不同,则不需要<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="@dimen/small_margin" android:layout_marginRight="@dimen/small_margin" android:background="@android:color/transparent"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:background="@android:color/transparent" android:id="@+id/nested_scrollview" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content"> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginBottom="@dimen/medium_margin" android:background="@drawable/button_layout_progress_fragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="@dimen/medium_margin" android:layout_marginRight="@dimen/small_margin" android:layout_marginLeft="@dimen/small_margin" android:layout_marginBottom="@dimen/small_margin"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:textAppearanceMedium" android:text="Time spent:" android:textStyle="bold" android:textColor="@android:color/black" android:layout_marginRight="@dimen/small_margin"/> <TextView android:textColor="@android:color/black" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:textAppearanceMedium" android:text="2hrs 37 mins" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginRight="@dimen/small_margin" android:layout_marginLeft="@dimen/small_margin" android:layout_marginBottom="@dimen/small_margin" > <TextView android:textColor="@android:color/black" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:textAppearanceMedium" android:text="New words learnt:" android:textStyle="bold" android:layout_marginRight="@dimen/small_margin"/> <TextView android:textColor="@android:color/black" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:textAppearanceMedium" android:text="259 words"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginRight="@dimen/small_margin" android:layout_marginLeft="@dimen/small_margin" android:layout_marginBottom="@dimen/medium_margin" > <TextView android:textColor="@android:color/black" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:textAppearanceMedium" android:text="Word roots learnt:" android:textStyle="bold" android:layout_marginRight="@dimen/small_margin"/> <TextView android:textColor="@android:color/black" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:textAppearanceMedium" android:text="37 words"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/button_layout_progress_fragment" > <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="200dp" android:layout_width="match_parent" android:id="@+id/gvBadges" android:numColumns="3" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:gravity="center" android:stretchMode="columnWidth" > </GridView> </LinearLayout> </LinearLayout> </ScrollView>

答案 1 :(得分:1)

除了格式错误的剪切和粘贴之外,您还可以稍微减少它:

SELECT Variable2_name,
       IIF(Variable1_name < 0.1,
           "0 - 10",
           IIF(Variable1_name < 0.2,
               "11 - 20",
               "21 - 100")
       ) AS Bucket
FROM Table
GROUP BY Variable2_name,
       IIF(Variable1_name < 0.1,
           "0 - 10",
           IIF(Variable1_name < 0.2,
               "11 - 20",
               "21 - 100")
       )
ORDER BY Variable2_name