sql 查询去除重复行
需要去除重复的行并求每行的余额(缴费合计减去费用合计)语句如下:selectSFM_BHas住院号,SFM_XMas姓名,SFM_JZBZ标志,SFM_JF缴费合计,SF...
需要去除重复的行并求每行的余额(缴费合计 减去 费用合计)
语句如下:
select
SFM_BH as 住院号,SFM_XM as 姓名,SFM_JZBZ 标志, SFM_JF 缴费合计,SFM_DF 费用合计,SFM_YE 余额 from
(select SFD_BH,SFD_CZY,SFD_RQ,SFD_ZFLB,SFD_ZY from ZY_SFD
where
SFD_JDLB=0
and SFD_BZ=1
and DATEDIFF(Day, SFD_RQ, '2015-09-26 15:18:00') <= 0
and DATEDIFF(Day, SFD_RQ, '2015-10-25 15:18:00') >= 0 )
as acc left
join
ZY_SFM on (SFM_BH=SFD_BH)
Where 1=1
group by
SFM_BH,SFM_XM,SFM_RYRQ,SFM_CW,SFD_RQ,SFM_JZBZ,SFM_JF,SFM_DF,SFM_YE,SFD_ZFLB
Order by
SFM_BH,convert(char(16),SFM_RYRQ,120)DESC,SFD_RQ
请专家帮忙修改下语句,谢谢! 展开
语句如下:
select
SFM_BH as 住院号,SFM_XM as 姓名,SFM_JZBZ 标志, SFM_JF 缴费合计,SFM_DF 费用合计,SFM_YE 余额 from
(select SFD_BH,SFD_CZY,SFD_RQ,SFD_ZFLB,SFD_ZY from ZY_SFD
where
SFD_JDLB=0
and SFD_BZ=1
and DATEDIFF(Day, SFD_RQ, '2015-09-26 15:18:00') <= 0
and DATEDIFF(Day, SFD_RQ, '2015-10-25 15:18:00') >= 0 )
as acc left
join
ZY_SFM on (SFM_BH=SFD_BH)
Where 1=1
group by
SFM_BH,SFM_XM,SFM_RYRQ,SFM_CW,SFD_RQ,SFM_JZBZ,SFM_JF,SFM_DF,SFM_YE,SFD_ZFLB
Order by
SFM_BH,convert(char(16),SFM_RYRQ,120)DESC,SFD_RQ
请专家帮忙修改下语句,谢谢! 展开
展开全部
首先,从img表中取数据库,将new_id重复的过滤掉,代码为
select
min(id)
from
img
group
by
new_id
------以new_id字段分组,取最小的ID,这个ID总不会重复了吧
然后将这个查询结果以虚拟表形式,作为过滤条件,取你所要的结果,代码为
select
T.new_id
AS
is,title,d_time,imgurl
from
news,Img
where
news.id
=
img.new_id
and
img.id
in
(select
min(id)
AS
img_id,new_id
from
img
group
by
new_id)
select
min(id)
from
img
group
by
new_id
------以new_id字段分组,取最小的ID,这个ID总不会重复了吧
然后将这个查询结果以虚拟表形式,作为过滤条件,取你所要的结果,代码为
select
T.new_id
AS
is,title,d_time,imgurl
from
news,Img
where
news.id
=
img.new_id
and
img.id
in
(select
min(id)
AS
img_id,new_id
from
img
group
by
new_id)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在select 后面加一个distinct应该就可以了,不过不建议这么做,建议找出为什么重复的原因。能否把group by所有的字段的select 结果都给我们看看
select distinct
SFM_BH as 住院号,SFM_XM as 姓名,SFM_JZBZ 标志, SFM_JF 缴费合计,SFM_DF 费用合计,SFM_YE 余额 from
(select SFD_BH,SFD_CZY,SFD_RQ,SFD_ZFLB,SFD_ZY from ZY_SFD
where
SFD_JDLB=0
and SFD_BZ=1
and DATEDIFF(Day, SFD_RQ, '2015-09-26 15:18:00') <= 0
and DATEDIFF(Day, SFD_RQ, '2015-10-25 15:18:00') >= 0 )
as acc left
join
ZY_SFM on (SFM_BH=SFD_BH)
Where 1=1
group by
SFM_BH,SFM_XM,SFM_RYRQ,SFM_CW,SFD_RQ,SFM_JZBZ,SFM_JF,SFM_DF,SFM_YE,SFD_ZFLB
Order by
SFM_BH,convert(char(16),SFM_RYRQ,120)DESC,SFD_RQ
select distinct
SFM_BH as 住院号,SFM_XM as 姓名,SFM_JZBZ 标志, SFM_JF 缴费合计,SFM_DF 费用合计,SFM_YE 余额 from
(select SFD_BH,SFD_CZY,SFD_RQ,SFD_ZFLB,SFD_ZY from ZY_SFD
where
SFD_JDLB=0
and SFD_BZ=1
and DATEDIFF(Day, SFD_RQ, '2015-09-26 15:18:00') <= 0
and DATEDIFF(Day, SFD_RQ, '2015-10-25 15:18:00') >= 0 )
as acc left
join
ZY_SFM on (SFM_BH=SFD_BH)
Where 1=1
group by
SFM_BH,SFM_XM,SFM_RYRQ,SFM_CW,SFD_RQ,SFM_JZBZ,SFM_JF,SFM_DF,SFM_YE,SFD_ZFLB
Order by
SFM_BH,convert(char(16),SFM_RYRQ,120)DESC,SFD_RQ
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select distinct
SFM_BH as 住院号,SFM_XM as 姓名,SFM_JZBZ 标志, SFM_JF 缴费合计,SFM_DF 费用合计,SFM_YE 余额 from
(select SFD_BH,SFD_CZY,SFD_RQ,SFD_ZFLB,SFD_ZY from ZY_SFD
where
SFD_JDLB=0
and SFD_BZ=1
and DATEDIFF(Day, SFD_RQ, '2015-09-26 15:18:00') <= 0
and DATEDIFF(Day, SFD_RQ, '2015-10-25 15:18:00') >= 0 )
as acc left
join
ZY_SFM on (SFM_BH=SFD_BH)
Where 1=1
group by
SFM_BH,SFM_XM,SFM_RYRQ,SFM_CW,SFD_RQ,SFM_JZBZ,SFM_JF,SFM_DF,SFM_YE,SFD_ZFLB
Order by
SFM_BH,convert(char(16),SFM_RYRQ,120)DESC,SFD_RQ
SFM_BH as 住院号,SFM_XM as 姓名,SFM_JZBZ 标志, SFM_JF 缴费合计,SFM_DF 费用合计,SFM_YE 余额 from
(select SFD_BH,SFD_CZY,SFD_RQ,SFD_ZFLB,SFD_ZY from ZY_SFD
where
SFD_JDLB=0
and SFD_BZ=1
and DATEDIFF(Day, SFD_RQ, '2015-09-26 15:18:00') <= 0
and DATEDIFF(Day, SFD_RQ, '2015-10-25 15:18:00') >= 0 )
as acc left
join
ZY_SFM on (SFM_BH=SFD_BH)
Where 1=1
group by
SFM_BH,SFM_XM,SFM_RYRQ,SFM_CW,SFD_RQ,SFM_JZBZ,SFM_JF,SFM_DF,SFM_YE,SFD_ZFLB
Order by
SFM_BH,convert(char(16),SFM_RYRQ,120)DESC,SFD_RQ
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-07-26 · 知道合伙人软件行家
emeipanda
知道合伙人软件行家
向TA提问 私信TA
知道合伙人软件行家
采纳数:329
获赞数:1494
曾获联想、华为、Juniper认证,现任职于四川省内江市GPS卫星监控中心总工程师,主持技术管理工作逾10年。
向TA提问 私信TA
关注
展开全部
order by (select 1)与order by 1一样按第一列排序,按照查询的结果集第一列排序
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询