需要跟踪一个列中的更改。

huangapple go评论61阅读模式
英文:

Need to Track Changes in one column

问题

我有一个名为rx_claims的表,其中包含列epi、drugname和gpi。表rx_claim保存了历史数据,我需要报告gpi(16位数字)是否发生了任何变化。我需要的逻辑是,如果gpi的前10位数字与历史数据不同,则报告为新药物;如果有12位数字发生变化,则报告为剂量变化,并显示新旧列数据。

例如:epi为100,药品名称为ibrofine,该药品的历史gpi为0123456789012300。现在我们有一条新记录,epi仍为100,药品名称仍为ibrofine,但剂量发生了变化,导致gpi代码变为0123456789000000,即最后4位数字发生了变化。因此,我想要显示的结果是药品名称:ibrofine,旧gpi为0123456789012300,新gpi为0123456789000000。

英文:

I have a table called rx_claims with columns epi, drugname, gpi. The table rx_claim is have historical saved data and I need to report if any changes in the gpi which is 16 digit number. The logic which I need is if the first 10 number of gpi changes from the historical data then report as new med and if 12 digits change then report as dose change with new and old column data.

for example : epi is 100 and drug name is ibrofine and gpi for this drug is 0123456789012300 ( historical data ) . Now we have a incoming record of 100 and drugname is still ibrofine but dosage change so it changes the gpi code to 0123456789000000 i.e the last 4 digits has changed so i want to show the resulted out as drugname : ibrofine and oldgpi is 0123456789012300 and new gpi is0123456789000000

答案1

得分: 0

你可以使用查询来比较历史数据和当前数据。

SELECT
epi,
drugname,
gpi AS current_gpi,
CASE
WHEN SUBSTR(gpi, 1, 10) != SUBSTR(hist.gpi, 1, 10) THEN '新药'
WHEN SUBSTR(gpi, 1, 10) = SUBSTR(hist.gpi, 1, 10) AND SUBSTR(gpi, 12, 5) != SUBSTR(hist.gpi, 12, 5) THEN CONCAT('剂量变更(旧 GPI:', hist.gpi, ')')
END AS change_description
FROM
rx_claims
LEFT JOIN
(
SELECT
epi,
gpi
FROM
rx_claims
WHERE
-- 添加筛选历史数据的条件
date < '2023-02-15'
) hist
ON
rx_claims.epi = hist.epi

此查询比较每个 epi 编号的当前 GPI 值与历史 GPI 值。如果 GPI 值的前 10 位数字发生了变化,它将报告变更为“新药”。如果仅有第 12 到 16 位数字发生了变化,它将报告变更为“剂量变更”,并在变更描述中包括旧 GPI 值。

英文:

you could use a query to compare the historical data with the current data

SELECT
  epi,
  drugname,
  gpi AS current_gpi,
  CASE
    WHEN SUBSTR(gpi, 1, 10) != SUBSTR(hist.gpi, 1, 10) THEN &#39;New Med&#39;
    WHEN SUBSTR(gpi, 1, 10) = SUBSTR(hist.gpi, 1, 10) AND SUBSTR(gpi, 12, 5) != SUBSTR(hist.gpi, 12, 5) THEN CONCAT(&#39;Dose Change (Old GPI: &#39;, hist.gpi, &#39;)&#39;)
  END AS change_description
FROM
  rx_claims
LEFT JOIN
  (
    SELECT
      epi,
      gpi
    FROM
      rx_claims
    WHERE
      -- add condition to filter for historical data
      date &lt; &#39;2023-02-15&#39;
  ) hist
ON
  rx_claims.epi = hist.epi

This query compares the current GPI value with the historical GPI value for each epi number. If the first 10 digits of the GPI value have changed, it reports the change as a "New Med". If only the 12th to 16th digits have changed, it reports the change as a "Dose Change" with the old GPI value included in the change description.

huangapple
  • 本文由 发表于 2023年2月16日 07:20:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定