27,580
社区成员
发帖
与我相关
我的任务
分享
create view vw_test as
SELECT CONVERT(varchar(20), a.autonumb) Chg_Cards_His, CONVERT(varchar(20),
a.autonumb) Chg_Invoice, CONVERT(varchar(20), a.autonumb) Chg_Advic,
b.medi_code Chg_HIS_Code, '01' Chg_Pat_Style, b.itemamou chg_Price,
c.opercode Chg_Doctor_Code, c.opername Chg_Doctor_Name,
a.creadate Chg_Exec_Time, '0' Chg_Exec_Rate, space(50) Chg_Operater, getdate()
Chg_Operate_Time, '0' Chg_State, a.patiname Chg_Pat_Name, CONVERT(varchar(2),
a.sex) Chg_Pat_Sex, CONVERT(varchar(10), a.age) Chg_Pat_Age, ' ' Chg_Pat_bed,
space(50) Chg_Pat_Dgs, d .officecode Chg_Pat_Dept_Code,
d .officename Chg_Pat_Dept_Name, space(50) Chg_Pat_Corp, space(50)
Chg_Ar_Code, '0' Chg_sys_state
FROM nclininvoinfo a INNER JOIN
nclinreciinfo b ON a.autonumb = b.autonumb AND b.charge_id IN (8, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 145, 147) LEFT OUTER JOIN
dictoper c ON a.doctid = c.operid LEFT OUTER JOIN
dictoffice d ON a.offiid = d .officeid
GO
select b.* from
(select distinct Chg_Cards_His, Chg_HIS_Code from vw_test) a
cross apply
(select top(1) * from vw_test where Chg_Cards_His = a.Chg_Cards_His and Chg_HIS_Code = a.Chg_HIS_Code order by NEWID()) b
GO
declare @table table (col1 int,col2 int,col3 int,col4 int,col5 int)
insert into @table
select 1,2,3,2,1 union all
select 1,2,4,2,1 union all
select 1,2,3,2,2 union all
select 1,2,4,2,2 union all
select 2,2,3,3,1 union all
select 2,2,4,3,1 union all
select 2,2,3,3,2 union all
select 2,2,4,3,2
;with maco as(
select row_number() over (partition by col1,col4 order by col2,col3,col5)
as id,* from @table
)
select t.col1,t.col2,t.col3,t.col4,t.col5 from maco t where
id=(select max(id) from maco
where col1=t.col1 and col4=t.col4)
/*
col1 col2 col3 col4 col5
----------- ----------- ----------- ----------- -----------
1 2 4 2 2
2 2 4 3 2
*/
;with maco as
(select row_number() over (partition by Chg_Cards_His,Chg_HIS_Code order by Chg_Invoice,Chg_Advic,chg_Price)
as id,* from (
SELECT CONVERT(varchar(20), a.autonumb) Chg_Cards_His, CONVERT(varchar(20),
a.autonumb) Chg_Invoice, CONVERT(varchar(20), a.autonumb) Chg_Advic,
b.medi_code Chg_HIS_Code, '01' Chg_Pat_Style, b.itemamou chg_Price,
c.opercode Chg_Doctor_Code, c.opername Chg_Doctor_Name,
a.creadate Chg_Exec_Time, '0' Chg_Exec_Rate, space(50) Chg_Operater, getdate()
Chg_Operate_Time, '0' Chg_State, a.patiname Chg_Pat_Name, CONVERT(varchar(2),
a.sex) Chg_Pat_Sex, CONVERT(varchar(10), a.age) Chg_Pat_Age, ' ' Chg_Pat_bed,
space(50) Chg_Pat_Dgs, d .officecode Chg_Pat_Dept_Code,
d .officename Chg_Pat_Dept_Name, space(50) Chg_Pat_Corp, space(50)
Chg_Ar_Code, '0' Chg_sys_state
FROM nclininvoinfo a INNER JOIN
nclinreciinfo b ON a.autonumb = b.autonumb AND b.charge_id IN (8, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 145, 147) LEFT OUTER JOIN
dictoper c ON a.doctid = c.operid LEFT OUTER JOIN
dictoffice d ON a.offiid = d .officeid) aa
)
select t.Chg_Cards_His,t.Chg_Invoice,t.Chg_Advic,t.Chg_HIS_Code,t.chg_Price
from maco t where
id=(select max(id) from maco
where Chg_Cards_His=t.Chg_Cards_His and Chg_HIS_Code=t.Chg_HIS_Code)
create view tp
as
SELECT CONVERT(varchar(20), a.autonumb) Chg_Cards_His, CONVERT(varchar(20),
a.autonumb) Chg_Invoice, CONVERT(varchar(20), a.autonumb) Chg_Advic,
b.medi_code Chg_HIS_Code, '01' Chg_Pat_Style, b.itemamou chg_Price,
c.opercode Chg_Doctor_Code, c.opername Chg_Doctor_Name,
a.creadate Chg_Exec_Time, '0' Chg_Exec_Rate, space(50) Chg_Operater, getdate()
Chg_Operate_Time, '0' Chg_State, a.patiname Chg_Pat_Name, CONVERT(varchar(2),
a.sex) Chg_Pat_Sex, CONVERT(varchar(10), a.age) Chg_Pat_Age, ' ' Chg_Pat_bed,
space(50) Chg_Pat_Dgs, d .officecode Chg_Pat_Dept_Code,
d .officename Chg_Pat_Dept_Name, space(50) Chg_Pat_Corp, space(50)
Chg_Ar_Code, '0' Chg_sys_state
FROM nclininvoinfo a INNER JOIN
nclinreciinfo b ON a.autonumb = b.autonumb AND b.charge_id IN (8, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 145, 147) LEFT OUTER JOIN
dictoper c ON a.doctid = c.operid LEFT OUTER JOIN
dictoffice d ON a.offiid = d .officeid
select * from tp t where not exists(
select 1 from tp where Chg_Cards_His=t.Chg_Cards_His and Chg_HIS_Code=t.Chg_HIS_Code and chg_Price>t.chg_Price)
SELECT CONVERT(varchar(20), a.autonumb) Chg_Cards_His, CONVERT(varchar(20),
a.autonumb) Chg_Invoice, CONVERT(varchar(20), a.autonumb) Chg_Advic,
b.medi_code Chg_HIS_Code, '01' Chg_Pat_Style, b.itemamou chg_Price,
c.opercode Chg_Doctor_Code, c.opername Chg_Doctor_Name,
a.creadate Chg_Exec_Time, '0' Chg_Exec_Rate, space(50) Chg_Operater, getdate()
Chg_Operate_Time, '0' Chg_State, a.patiname Chg_Pat_Name, CONVERT(varchar(2),
a.sex) Chg_Pat_Sex, CONVERT(varchar(10), a.age) Chg_Pat_Age, ' ' Chg_Pat_bed,
space(50) Chg_Pat_Dgs, d .officecode Chg_Pat_Dept_Code,
d .officename Chg_Pat_Dept_Name, space(50) Chg_Pat_Corp, space(50)
Chg_Ar_Code, '0' Chg_sys_state
INTO #temp
FROM nclininvoinfo a INNER JOIN
nclinreciinfo b ON a.autonumb = b.autonumb AND b.charge_id IN (8, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 145, 147) LEFT OUTER JOIN
dictoper c ON a.doctid = c.operid LEFT OUTER JOIN
dictoffice d ON a.offiid = d .officeid
select * from #temp t where not exists(
select 1 from #temp where Chg_Cards_His=t.Chg_Cards_His and Chg_HIS_Code=t.Chg_HIS_Code and chg_Price>t.chg_Price)
;with cte as(
SELECT CONVERT(varchar(20), a.autonumb) Chg_Cards_His, CONVERT(varchar(20),
a.autonumb) Chg_Invoice, CONVERT(varchar(20), a.autonumb) Chg_Advic,
b.medi_code Chg_HIS_Code, '01' Chg_Pat_Style, b.itemamou chg_Price,
c.opercode Chg_Doctor_Code, c.opername Chg_Doctor_Name,
a.creadate Chg_Exec_Time, '0' Chg_Exec_Rate, space(50) Chg_Operater, getdate()
Chg_Operate_Time, '0' Chg_State, a.patiname Chg_Pat_Name, CONVERT(varchar(2),
a.sex) Chg_Pat_Sex, CONVERT(varchar(10), a.age) Chg_Pat_Age, ' ' Chg_Pat_bed,
space(50) Chg_Pat_Dgs, d .officecode Chg_Pat_Dept_Code,
d .officename Chg_Pat_Dept_Name, space(50) Chg_Pat_Corp, space(50)
Chg_Ar_Code, '0' Chg_sys_state
FROM nclininvoinfo a INNER JOIN
nclinreciinfo b ON a.autonumb = b.autonumb AND b.charge_id IN (8, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 145, 147) LEFT OUTER JOIN
dictoper c ON a.doctid = c.operid LEFT OUTER JOIN
dictoffice d ON a.offiid = d .officeid
)
select * from cte a where not exists(
select 1 from cte where Chg_Cards_His=a.Chg_Cards_His and Chg_HIS_Code=a.Chg_HIS_Code and chg_Price>a.chg_Price)
;with maco as
(
SELECT CONVERT(varchar(20), a.autonumb) Chg_Cards_His, CONVERT(varchar(20),
a.autonumb) Chg_Invoice, CONVERT(varchar(20), a.autonumb) Chg_Advic,
b.medi_code Chg_HIS_Code, '01' Chg_Pat_Style, b.itemamou chg_Price,
c.opercode Chg_Doctor_Code, c.opername Chg_Doctor_Name,
a.creadate Chg_Exec_Time, '0' Chg_Exec_Rate, space(50) Chg_Operater, getdate()
Chg_Operate_Time, '0' Chg_State, a.patiname Chg_Pat_Name, CONVERT(varchar(2),
a.sex) Chg_Pat_Sex, CONVERT(varchar(10), a.age) Chg_Pat_Age, ' ' Chg_Pat_bed,
space(50) Chg_Pat_Dgs, d .officecode Chg_Pat_Dept_Code,
d .officename Chg_Pat_Dept_Name, space(50) Chg_Pat_Corp, space(50)
Chg_Ar_Code, '0' Chg_sys_state
FROM nclininvoinfo a INNER JOIN
nclinreciinfo b ON a.autonumb = b.autonumb AND b.charge_id IN (8, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 145, 147) LEFT OUTER JOIN
dictoper c ON a.doctid = c.operid LEFT OUTER JOIN
dictoffice d ON a.offiid = d .officeid
)
select * from maco t where
chg_Price=(select max(chg_Price) from maco
where Chg_Cards_His=t.Chg_Cards_His and Chg_HIS_Code =t.Chg_HIS_Code )