編集:T-SQLを使用して列の値に基づいて行のランク付けを行う方法は?
私は、このコードを選択された溶液を改変し、それが意図した結果を達成しました。私は別の列の値に応じて行にランク値を割り当てるに行くかどう
DENSE_RANK()
OVER (ORDER BY
CASE WHEN f.empl_ssn = f.cov_ind_ssn
THEN 0 ELSE 1 END, f.cov_ind_ssn) AS [depend_no]
?私は、RANK()、ROW_NUMBER()、DENSE_RANK()関数を使用して読み込みました。特定の状況でDENSE_RANK()を動作させてテストを行いましたが、1家族から172,000行その解決策が失敗します。私はこのようなルックスを達成するために必要なもの
:
+-------------+-------------+--------+--------+------+---------------+-----------+
| empl_ssn | cov_ind_ssn | f_name | l_name | code | date_of_birth | depend_no |
+-------------+-------------+--------+--------+------+---------------+-----------+
| 000-11-0010 | 000-11-0011 | Joe | Bob | 1 | 12/25/72 | 2 |
| 000-11-0010 | 000-11-0010 | Morgan | Bob | 18 | 9/14/71 | 1 |
| 000-11-0010 | 000-12-0102 | Little | Bob | 19 | 2/16/06 | 3 |
| 000-11-0010 | 000-51-9212 | Wittle | Bob | 19 | 8/12/04 | 4 |
| 000-11-0010 | 000-53-0013 | Tiny | Bob | 19 | 8/25/02 | 5 |
+-------------+-------------+--------+--------+------+---------------+-----------+
このデータは、任意の順序になりますが、私はすでに1に人ダウンあたり12行からデータを取るために、いくつかのCTEのを使用しました行ごとに、私は今それらにdepend_noを割り当てる必要があります。
empl_ssnとcov_ind_ssnが等しい場合、depend_noは1である必要があります。これは、それらが会社の従業員であることを意味します。それ以降は2から始まり、すべての行に値が割り当てられるまでインクリメントする必要があります。
は今、私はこのCTEを使用することによって、これを達成した:
random AS
(
SELECT DISTINCT
empl_ssn,
cov_ind_ssn,
relationship_code,
DENSE_RANK()
OVER (
ORDER BY f.birthdate) AS [depend_no]
FROM full_data AS f
WHERE empl_ssn = @current_employee
)
しかし、私は従業員が常に最初に生まれてきたことを保証することはできませんので、それは明らかに実行可能ではありません。
ちょうど参考のために、これまでに書いたブロック全体を、慎重にストライプ化して投稿します。
DECLARE @current_employee VARCHAR(11);
SET @current_employee = 'XXX-XX-XXXX';
WITH premiums AS
(
SELECT *
FROM
(
SELECT
empl_ssn,
premium_amount,
month_covered
FROM full_data
WHERE empl_ssn = @current_employee
) AS src
PIVOT (
MAX(premium_amount)
FOR month_covered
IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) AS piv1
),
offer_codes AS
(
SELECT *
FROM
(
SELECT
empl_ssn,
offer_code,
month_covered
FROM full_data
WHERE empl_ssn = @current_employee
) AS src
PIVOT (
MAX(offer_code)
FOR month_covered
IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) AS piv1
),
safe_harbor_codes AS
(
SELECT *
FROM
(
SELECT
empl_ssn,
safe_harbor_code,
month_covered
FROM full_data
WHERE empl_ssn = @current_employee
) AS src
PIVOT (
MAX(safe_harbor_code)
FOR month_covered
IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) AS piv1
),
random AS
(
SELECT DISTINCT
empl_ssn,
cov_ind_ssn,
relationship_code,
DENSE_RANK()
OVER (
ORDER BY f.birthdate) AS [depend_no]
FROM full_data AS f
WHERE empl_ssn = @current_employee
)
SELECT DISTINCT
f.empl_ssn,
f.cov_ind_ssn,
f.f_name,
f.l_name,
f.relationship_code,
f.birthdate,
r.depend_no,
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[1] END AS [emp_share_jan],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[2] END AS [emp_share_feb],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[3] END AS [emp_share_mar],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[4] END AS [emp_share_apr],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[5] END AS [emp_share_may],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[6] END AS [emp_share_jun],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[7] END AS [emp_share_jul],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[8] END AS [emp_share_aug],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[9] END AS [emp_share_sep],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[10] END AS [emp_share_oct],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[11] END AS [emp_share_nov],
CASE WHEN p.[1] = p.[12]
THEN NULL
ELSE
p.[12] END AS [emp_share_dec],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[1] END AS [offer_code_jan],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[2] END AS [offer_code_feb],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[3] END AS [offer_code_mar],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[4] END AS [offer_code_apr],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[5] END AS [offer_code_may],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[6] END AS [offer_code_jun],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[7] END AS [offer_code_jul],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[8] END AS [offer_code_aug],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[9] END AS [offer_code_sep],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[10] END AS [offer_code_oct],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[11] END AS [offer_code_nov],
CASE WHEN o.[1] = o.[12]
THEN NULL
ELSE
o.[12] END AS [offer_code_dec],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[1] END AS [safe_harbor_jan],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[2] END AS [safe_harbor_feb],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[3] END AS [safe_harbor_mar],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[4] END AS [safe_harbor_apr],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[5] END AS [safe_harbor_may],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[6] END AS [safe_harbor_jun],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[7] END AS [safe_harbor_jul],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[8] END AS [safe_harbor_aug],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[9] END AS [safe_harbor_sep],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[10] END AS [safe_harbor_oct],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[11] END AS [safe_harbor_nov],
CASE WHEN S.[1] = S.[12]
THEN NULL
ELSE
S.[12] END AS [safe_harbor_dec],
CASE WHEN p.[1] = p.[12]
THEN p.[1]
ELSE NULL END AS [premium_all],
CASE WHEN o.[1] <> o.[12]
THEN NULL
ELSE o.[1] END AS [offer_all],
CASE WHEN S.[1] <> S.[12]
THEN NULL
ELSE S.[1] END AS [harbor_all]
FROM
premiums AS p
JOIN offer_codes AS o ON p.empl_ssn = o.empl_ssn
JOIN safe_harbor_codes AS S ON p.empl_ssn = S.empl_ssn
JOIN full_data AS f ON p.empl_ssn = f.empl_ssn
JOIN random AS r ON p.empl_ssn = r.empl_ssn
ORDER BY depend_no;
私は両方のソリューションをテストし、若干の変更を加えた後、この回答が私に解決策をもたらしました。ありがとうございました! –