0

以下のコードが専門家として誰かを怒らせる場合は、前もって謝罪させてください。以下のビットが完了した後、探しているものを正確に吐き出します。列に文字列がある場合、そうでない場合は 1 を取得します。ゼロ。私の意図は、これらを合計する 7 番目の列を追加して、レポートの数式に注ぎ込み、別の列と比較できるようにすることです。クエリで実行できると確信していますが、方法がわかりません。サーバーはSQL 2008です

    select CASE
WHEN [1  First Name] = ''
THEN 1
ELSE 0
    END as "First Name 1",
    CASE
WHEN [1  Last Name] = ''
THEN 1
ELSE 0
    END as "Last Name 1",
    CASE
WHEN [2  First Name] = ''
THEN 1
ELSE 0
    END as "First name 2",
    CASE
WHEN [2  Last Name] = ''
THEN 1
ELSE 0
    END as "Last Name 2",
    CASE
WHEN [3  First Name] = ''
THEN 1
ELSE 0
    END as "First Name 3",
    CASE
WHEN [3  Last Name] = ''
THEN 1
ELSE 0
    END as "Last Name 3"
    FROM
    member
4

2 に答える 2

3

これにより、すべての行と列の 1 が合計されます

SELECT
    X.*,
    "First Name 1"+
        "Last Name 1"+
        "First name 2"+
        "Last Name 2"+
        "First Name 3"+
        "Last Name 3" AS TheSum
FROM
    (
    SELECT
        CASE WHEN [1  First Name] = '' THEN 1 ELSE 0 END as "First Name 1",
        CASE WHEN [1  Last Name] = '' THEN 1 ELSE 0 END as  "Last Name 1",
        CASE WHEN [2  First Name] = '' THEN 1 ELSE 0 END as "First name 2",
        CASE WHEN [2  Last Name] = '' THEN 1 ELSE 0 END as  "Last Name 2",
        CASE WHEN [3  First Name] = '' THEN 1 ELSE 0 END as "First Name 3",
        CASE WHEN [3  Last Name] = '' THEN 1 ELSE 0 END as  "Last Name 3"
    FROM
        member
    ) X
于 2013-03-07T14:07:21.660 に答える
2

何か不足していない限り、現在のクエリをサブクエリに配置してから、合計の列を追加できます。

select "First Name 1",
    "Last Name 1",
    "First name 2",
    "Last Name 2",
    "First Name 3",
    "Last Name 3",
    ("First Name 1" +"Last Name 1"+"First name 2"+"Last Name 2"+"First Name 3"+"Last Name 3") As total
from
(
    select CASE
            WHEN [1  First Name] = '' THEN 1 ELSE 0
        END as "First Name 1",
        CASE
            WHEN [1  Last Name] = '' THEN 1 ELSE 0
        END as "Last Name 1",
        CASE
            WHEN [2  First Name] = '' THEN 1 ELSE 0
        END as "First name 2",
        CASE
            WHEN [2  Last Name] = '' THEN 1 ELSE 0
        END as "Last Name 2",
        CASE
            WHEN [3  First Name] = '' THEN 1 ELSE 0
        END as "First Name 3",
        CASE
            WHEN [3  Last Name] = '' THEN 1 ELSE 0
        END as "Last Name 3"
    FROM member
) m
于 2013-03-07T15:00:09.800 に答える