0
我想在下面的查询中对每个位置使用with子句使用union。我尝试了多种方式,但一直没有能够得到这个工作。然后我想创建一个查询。如何使用with子句和union sqlserver
WITH MaxLoadId AS
(
select MAX(t.loadid) AS loadid, p.partitionkey, p.partdesc, p.partname
FROM tdataseg t
INNER JOIN part p ON t.partitionkey = p.partitionkey
WHERE p.partname = 'MEXICO'
GROUP BY p.partitionkey, p.partdesc, p.partname
)
select distinct
mli.loadid AS loadid,
a.process_id,
mli.partitionkey,
-- t.partitionkey,
mli.partdesc,
mli.partname,
c.catname,
r.rule_name,
r.file_path,
a.execution_start_time,
a.execution_end_time,
a.records_processed,
a.status,
a.last_updated_by as processed_by,
a.last_update_date
FROM MaxLoadId mli
INNER JOIN datat ON mli.loadid = t.loadid
INNER JOIN details a ON mli.loadid = a.process_id
INNER JOIN category c ON t.catkey = c.catkey
INNER JOIN balances r ON t.RULE_ID = r.RULE_ID;
工作,谢谢。我怎样才能把它变成一个视图? – ChilliSauce
我编辑了我的答案,只需在语句开头添加“CREATE VIEW schema.ViewName AS'。 –