如何在视图中使用子查询?

发布于 2024-12-04 03:11:18 字数 2121 浏览 1 评论 0原文

我需要为此查询创建一个视图允许更改 m.arq_datam2.arq_data< /strong> 当选择使用此视图时的值,例如:

select * FROM the_view WHERE m.arq_data = X AND m2.arq_data = Y

这是我当前的查询:

SELECT distinct(p.num_processo),p.num_proc_jud,a.assunto,su.subassunto,ma.Materia,u.Unidade ,M.COD_UNIDADE as cod_serv ,u.Unidade as servidor  ,' '  as serv_ativo,
'  '  as data_vinc ,' '  as  V_ativo, M.motivo,M.data_movimentacao as data_mov_distr ,
(select max(m2.arq_data)  from movimentacao m2 where  m2.arq_data 
BETWEEN '2011-08-01 00:00:00'AND '2011-08-31 23:00:00' and m2.num_processo =
  p.num_processo) as Data_Arq_Desarq , status = 'A'

--pra view
, M.arq_data

FROM processo p 
INNER JOIN assunto a ON a.cod_assunto  = p.cod_assunto  
INNER JOIN subassunto su ON su.cod_subassunto  = p.cod_subassunto
LEFT  JOIN materia ma ON ma.cod_materia  = p.cod_materia 
inner JOIN movimentacao M on M.num_processo = p.num_processo
INNER JOIN Unidade u ON u.cod_unidade  = M.COD_UNIDADE  
where 
not exists(select * from anexos a where a.num_proc_anexo = p.num_processo and a.ativo = 1) 
and not exists(select * from movimentacao m1 where m1.num_movimentacao= M.num_movimentacao and m1.motivo = 10 and m1.arquivado = 0) 
and ( not exists (select  * from distrib_vincjud  d2 where d2.num_processo = p.num_processo) 
or  p.num_processo in  (select  d3.num_processo from distrib_vincjud  d3  
                         where d3.num_processo = p.num_processo 
                         and d3.cod_servidor not in(select cod_servidor from servidor)
                         and d3.id_vinc in (select  max(d4.id_vinc) from distrib_vincjud d4 
                  where d4.num_processo = d3.num_processo and d4.data_vinc <= M.arq_data )))
and M.COD_UNIDADE in (select M.COD_UNIDADE   from movimentacao m2 where 
(m2.cod_ORIGEM_MOV = '26000181' or m2.cod_ORIGEM_MOV = '2600000X')and m2.num_processo = p.num_processo)
and p.tipo = 'J'
and  M.arq_data  >= '2011-08-01 00:00:00'AND M.arq_data  <='2011-08-31 23:00:00'

I need create a View for this Query, allowing change m.arq_data and m2.arq_data values when a select uses this View, like:

select * FROM the_view WHERE m.arq_data = X AND m2.arq_data = Y

Here is my current query:

SELECT distinct(p.num_processo),p.num_proc_jud,a.assunto,su.subassunto,ma.Materia,u.Unidade ,M.COD_UNIDADE as cod_serv ,u.Unidade as servidor  ,' '  as serv_ativo,
'  '  as data_vinc ,' '  as  V_ativo, M.motivo,M.data_movimentacao as data_mov_distr ,
(select max(m2.arq_data)  from movimentacao m2 where  m2.arq_data 
BETWEEN '2011-08-01 00:00:00'AND '2011-08-31 23:00:00' and m2.num_processo =
  p.num_processo) as Data_Arq_Desarq , status = 'A'

--pra view
, M.arq_data

FROM processo p 
INNER JOIN assunto a ON a.cod_assunto  = p.cod_assunto  
INNER JOIN subassunto su ON su.cod_subassunto  = p.cod_subassunto
LEFT  JOIN materia ma ON ma.cod_materia  = p.cod_materia 
inner JOIN movimentacao M on M.num_processo = p.num_processo
INNER JOIN Unidade u ON u.cod_unidade  = M.COD_UNIDADE  
where 
not exists(select * from anexos a where a.num_proc_anexo = p.num_processo and a.ativo = 1) 
and not exists(select * from movimentacao m1 where m1.num_movimentacao= M.num_movimentacao and m1.motivo = 10 and m1.arquivado = 0) 
and ( not exists (select  * from distrib_vincjud  d2 where d2.num_processo = p.num_processo) 
or  p.num_processo in  (select  d3.num_processo from distrib_vincjud  d3  
                         where d3.num_processo = p.num_processo 
                         and d3.cod_servidor not in(select cod_servidor from servidor)
                         and d3.id_vinc in (select  max(d4.id_vinc) from distrib_vincjud d4 
                  where d4.num_processo = d3.num_processo and d4.data_vinc <= M.arq_data )))
and M.COD_UNIDADE in (select M.COD_UNIDADE   from movimentacao m2 where 
(m2.cod_ORIGEM_MOV = '26000181' or m2.cod_ORIGEM_MOV = '2600000X')and m2.num_processo = p.num_processo)
and p.tipo = 'J'
and  M.arq_data  >= '2011-08-01 00:00:00'AND M.arq_data  <='2011-08-31 23:00:00'

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

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

发布评论

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

评论(2

不忘初心 2024-12-11 03:11:18

视图不能带参数。将其实现为表值 udf(或存储过程)。

View cannot take parameters. Implement it as table-valued udf instead (or stored procedure).

静待花开 2024-12-11 03:11:18

如果您说要定义视图以处理诸如 m2.arq_data BETWEEN '2011-08-01 00:00:00'AND '2011-08-31 23:00:00' 之类的条件的更改日期 那么这取决于你的要求是什么。

例如,如果此日期范围始终是从今天到 31 天之后,那么您可以将此行更改为如下所示:

m2.arq_data BETWEEN GetDate() AND DATEADD (dd, 31, GetDate())

如果日期范围不是相当简单的内容,通常可以使用SQL 那么您可能需要考虑使用存储过程或 udf 并向其传递日期参数。

If you are saying you want to define your view to work with changing dates for conditions like m2.arq_data BETWEEN '2011-08-01 00:00:00'AND '2011-08-31 23:00:00' then it depends what your requirements are.

For instance if this date range will always be between today and 31 days from now then you could change this line to be something like this:

m2.arq_data BETWEEN GetDate() AND DATEADD (dd, 31, GetDate())

If the date range is not something fairly simple that can be defined generically using SQL then you might want to consider using a stored proc or a udf and passing it the date params.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文