交叉连接与reapitive值

时间:2016-05-20 07:52:36

标签: sql join

我有一张桌子“Asli”:

while getopts ":a:b:p:u" opts
do
   case $opts in #removed the dot at the end
    a) echo "got an A flag";;
    b) echo "got an B flag";;
    u) user="$OPTARGS"
       echo "$user"
       #double quote the variables to prevent globbing and word splitting
    ;;
    p) pass="$OPTARGS"
    #Passwords can contain whitespace in the beginning.
    #If you don't double quote , you loose them while storing.
    #eg. pass=$@ will strip the leading whitespaces in the normal case.
       echo "$pass"
    ;;
    ?) echo "I don't know what flag is this" 
    #Better double quote to make echo easy, consider something like \\\\\\
    #count the hashes? eh?
    ;;
   esac
done

我插入的值是:

CREATE TABLE Asli(
    clashhid INT NULL,
    TaDah INT NULL,
    DahTaPanz INT NULL,
    PanzTaBist INT NULL,
    BistTaBispan INT NULL,
    BispanTaC INT NULL,
    Marital VARCHAR(20) NULL)

当我通过此查询交叉加入我的表时:

INSERT INTO Asli
 VALUES (1,2,1,0,0,0,'single')

我的结果集:

Image1

但我需要更改“已婚”状态0中的值,如下所示:

Image2

1 个答案:

答案 0 :(得分:1)

这样的事情:

SELECT  clashhid,
        CASE WHEN mariStatus = 'married' THEN 0 ELSE TaDah END AS TaDah,
        CASE WHEN mariStatus = 'married' THEN 0 ELSE DahTaPanzEND AS DahTaPanz,
        CASE WHEN mariStatus = 'married' THEN 0 ELSE PanzTaBist END AS PanzTaBist,
        CASE WHEN mariStatus = 'married' THEN 0 ELSE BistTaBispan END AS BistTaBispan,
        CASE WHEN mariStatus = 'married' THEN 0 ELSE BispanTaC END AS BispanTaC ,     
        mariStatus
FROM asli 
CROSS JOIN (VALUES ('single'),('married')) AS custs (mariStatus)
相关问题