Oracle查询以检查一个字段是否已更改为另一个特定

发布于 2025-02-11 13:18:46 字数 2234 浏览 0 评论 0原文

我们最近注意到,当一些工作几乎没有购买的物品时,有些工作的最高速率(13),因此我们需要查询,以检查是否有任何工人的费率更改为最大值,因此我们可以检查其合法性是否合法。

所以我有这两个表:

table1-> 工人(DNI,费率,票,价格,...)

table2-> 出售(DNI,名称,日期)

编辑:日期和费率都是字符串,日期遵循以下结构:yyyymmdd

出售表有特殊的折扣,取决于客户的费用。

我想检查这些摇摆器中的任何一个是否会从而不是13 的东西中进行更改。> 到13

如果工人买了很多东西,或者有人手动更改它,则可以获得更好的折扣(不允许)。

因此,我想检查昨天是否有6次率,今天13率。

SELECT RATE,DNI FROM SELLS WHERE RATE='13' AND DNI IN 
(
SELECT DNI FROM WORKERS
)
AND DATE = to_char(sysdate-1, 'yyyymmdd')

是否有任何办法将这些工具结合起来,以便匹配的保留?

SELECT RATE,DNI FROM SELLS WHERE RATE <> '13' AND DNI IN 
(
SELECT DNI FROM WORKERS
)
AND DATE to_char(sysdate, 'yyyymmdd')

还是有更好的方法?

尝试的编辑:

真实查询:

select
  coalesce(t.dni, y.dni) as dni,
  t.Tarifa as today_rate,
  y.Tarifa as yesterday_rate
from (
  SELECT Tarifa, DNI FROM pws_ventas_materiales,pws_ventas_cabecera
  WHERE Tarifa='13' 
    AND DNI IN (SELECT DNI FROM trabajadores_sirgo)
    AND pws_ventas_materiales.fecha = to_char(sysdate-1, 'yyyymmdd')
    and pws_ventas_materiales.ticket = pws_ventas_cabecera.ticket
    and pws_ventas_cabecera.fecha = pws_ventas_materiales.fecha
) y
full join (
  SELECT Tarifa, DNI FROM pws_ventas_materiales ,pws_ventas_cabecera
  WHERE Tarifa <> '13' and Tarifa is not null
    AND DNI IN (SELECT DNI FROM trabajadores_sirgo)
    AND pws_ventas_materiales.fecha = to_char(sysdate, 'yyyymmdd')
    and pws_ventas_materiales.ticket = pws_ventas_cabecera.ticket
    and pws_ventas_cabecera.fecha = pws_ventas_materiales.fecha
) t on t.dni = y.dni
;

表更新:

PWS_VENTAS_MATERIALES = 出售

trabajadores_sirgo = 工人

pws_ventas_cabecera - &gt;卖出表上有一个错误,该表不包含DNI列,该表确实如此。

对此感到抱歉

,RATE = TARIFA和DATE = FECHA +我添加了pws_ventas_materiales.ticket = pws_ventas_cabecera.ticket,以确保我们在这两个表之间谈论相同的卖出。

We recently noticed that some workes have the maximum rate (13) when they barely have any bought items, so we need a query that checks if any worker rate changes to the maximum so we can check if its legit.

So I have these two tables:

Table1-> Workers (DNI,RATE,TICKET,PRICE,...)

Table2-> Sells (DNI,NAME,DATE)

edit: DATE and RATE are both Strings, Date follows this structure: YYYYMMDD

Sells table have a special discount depending on the rate of the client.

I want to check if any of those wokers rate changes from something that is not 13 --> to 13

The only way this can happen if is the worker buys a lot, or if someone changes it manually so they get a better discount (NOT ALLOWED).

So, I want to check if yesterday any worker had a 6 rate and today its 13.

SELECT RATE,DNI FROM SELLS WHERE RATE='13' AND DNI IN 
(
SELECT DNI FROM WORKERS
)
AND DATE = to_char(sysdate-1, 'yyyymmdd')

Is there any way to UNION those so the matched remain?

SELECT RATE,DNI FROM SELLS WHERE RATE <> '13' AND DNI IN 
(
SELECT DNI FROM WORKERS
)
AND DATE to_char(sysdate, 'yyyymmdd')

Or is there any better way?

Edit of what tried:

Real query:

select
  coalesce(t.dni, y.dni) as dni,
  t.Tarifa as today_rate,
  y.Tarifa as yesterday_rate
from (
  SELECT Tarifa, DNI FROM pws_ventas_materiales,pws_ventas_cabecera
  WHERE Tarifa='13' 
    AND DNI IN (SELECT DNI FROM trabajadores_sirgo)
    AND pws_ventas_materiales.fecha = to_char(sysdate-1, 'yyyymmdd')
    and pws_ventas_materiales.ticket = pws_ventas_cabecera.ticket
    and pws_ventas_cabecera.fecha = pws_ventas_materiales.fecha
) y
full join (
  SELECT Tarifa, DNI FROM pws_ventas_materiales ,pws_ventas_cabecera
  WHERE Tarifa <> '13' and Tarifa is not null
    AND DNI IN (SELECT DNI FROM trabajadores_sirgo)
    AND pws_ventas_materiales.fecha = to_char(sysdate, 'yyyymmdd')
    and pws_ventas_materiales.ticket = pws_ventas_cabecera.ticket
    and pws_ventas_cabecera.fecha = pws_ventas_materiales.fecha
) t on t.dni = y.dni
;

Table Updates:

pws_ventas_materiales=Sells

trabajadores_sirgo=Workers

pws_ventas_cabecera --> There was a mistake on the Sells table, that table does not contain a DNI column, this table does.

Sorry for that mistake

Also, rate=Tarifa and date=fecha + I added a pws_ventas_materiales.ticket = pws_ventas_cabecera.ticket to make sure that we are talking about the same sell between those two tables.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2025-02-18 13:18:46

您可以加入两个查询进行比较。在这种情况下,我认为完整的外部连接更合适。例如:

select
  coalesce(t.dni, y.dni) as dni,
  t.rate as today_rate,
  y.rate as yesterday_rate
from (
  SELECT RATE, DNI FROM SELLS 
  WHERE RATE='13' 
    AND DNI IN (SELECT DNI FROM WORKERS)
    AND DATE = to_char(sysdate-1, 'yyyymmdd')
) y
full join (
  SELECT RATE, DNI FROM SELLS 
  WHERE RATE <> '13' 
    AND DNI IN (SELECT DNI FROM WORKERS)
    AND DATE to_char(sysdate, 'yyyymmdd')
) t on t.dni = y.dni
-- WHERE today_rate is not null -- extra filtering here

此外,以下查询可以比较所有日期,不仅是今天和昨天:

select *
from (
  select
    dni, date, rate,
    lag(date) over(partition by dni order by date) as prev_date,
    lag(rate) over(partition by dni order by date) as prev_rate
  from workers
) x
where prev_rate <> '13' and rate = '13'

You can join both queries to compare. In this case I would think that an full outer join is more appropriate. For example:

select
  coalesce(t.dni, y.dni) as dni,
  t.rate as today_rate,
  y.rate as yesterday_rate
from (
  SELECT RATE, DNI FROM SELLS 
  WHERE RATE='13' 
    AND DNI IN (SELECT DNI FROM WORKERS)
    AND DATE = to_char(sysdate-1, 'yyyymmdd')
) y
full join (
  SELECT RATE, DNI FROM SELLS 
  WHERE RATE <> '13' 
    AND DNI IN (SELECT DNI FROM WORKERS)
    AND DATE to_char(sysdate, 'yyyymmdd')
) t on t.dni = y.dni
-- WHERE today_rate is not null -- extra filtering here

Also, the following query can compare all dates, not just today and yesterday:

select *
from (
  select
    dni, date, rate,
    lag(date) over(partition by dni order by date) as prev_date,
    lag(rate) over(partition by dni order by date) as prev_rate
  from workers
) x
where prev_rate <> '13' and rate = '13'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文