如何二进制化#39; SQL的一个表

时间:2017-08-28 09:18:03

标签: sql sql-server

例如,有一个表Employee:

id gender   age
--------------------
1  Male     32
2  Female   26
3  Female   45
4  NA       31

我想创建另一个表并插入如下数据:

id gender_Male gender_Female gender_NA age_26 age_31 age_32 age_45
--------------------------------------------------------------------
1  1           0             0         0       0     1      0
2  0           1             0         1       0     0      0          
3  0           1             0         0       0     0      1          
4  0           0             1         0       1     0      0          

2 个答案:

答案 0 :(得分:0)

使用CASE将每个字段指定为1或0.

SELECT
 e.[id],
 CASE WHEN e.gender = 'Male' THEN 1 ELSE 0 END AS gender_Male,
 CASE WHEN e.gender = 'Female' THEN 1 ELSE 0 END AS gender_Female,
 CASE WHEN e.gender = 'NA' THEN 1 ELSE 0 END AS gender_NA
INTO
 newTable
FROM
 Employee e

答案 1 :(得分:0)

enter image description here您可以使用Pivot和动态SQL获得结果 -

    <RelativeLayout
            android:id="@+id/input_password_outer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/layout_country_text_fields"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <EditText
                    android:id="@+id/input_oldpwd_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Confirm New Password"
                    android:inputType="textPassword"
                    android:paddingBottom="25dp"
                    android:textColor="@color/registration_login_edit_text"
                    android:textSize="15sp" />

            </android.support.design.widget.TextInputLayout>

            <LinearLayout
                android:layout_width="38dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="10dp"
                android:layout_weight="@dimen/password_eye_weight"
                android:gravity="right|center_horizontal"
                android:scaleType="fitXY">

                <ToggleButton
                    android:id="@+id/confirm_password_toggle_button"
                    android:layout_width="wrap_content"
                    android:layout_height="30dp"
                    android:layout_alignParentRight="true"
                    android:layout_gravity="right"
                    android:background="@color/mobile_screen_background"
                    android:clickable="true"
                    android:gravity="right|center_horizontal"
                    android:padding="3dp"
                    android:textOff=""
                    android:textOn=""
                    />
            </LinearLayout>
        </RelativeLayout>