表的结构大致如下:
卡号 交易日期 交易金额
123 1.1 111
123 1.1 59
456 1.1 110
123 1.2 36
123 1.2 78
456 1.2 89
456 1.2 55
每张卡号每天都会发生大量交易,现在想查询 所有单日有2笔或以上金额超过50元的交易情况
已商标为例,查询结果应该是
123 1.1 111
123 1.1 59
456 1.2 89
456 1.2 55另外还想查询3日有2笔或以上金额超过50元的交易情况
卡号 交易日期 交易金额
123 1.1 111
123 1.1 59
456 1.1 110
123 1.2 36
123 1.2 78
456 1.2 89
456 1.2 55
每张卡号每天都会发生大量交易,现在想查询 所有单日有2笔或以上金额超过50元的交易情况
已商标为例,查询结果应该是
123 1.1 111
123 1.1 59
456 1.2 89
456 1.2 55另外还想查询3日有2笔或以上金额超过50元的交易情况
WHERE
交易金额 >50
OR
COUNT(交易日期)>1
GROUP BY ID
卡号,交易日期,交易金额
from
tb
where
交易金额 > 50
group by
卡号 交易日期
having
count(1)>2
卡号,交易日期,交易金额
from
tb
where
交易金额 > 50
group by
卡号,交易日期
having
count(1)>=2
卡号, 交易日期 , 交易金额
FROM
TB
WHERE
交易金额 >=50
GROUP BY
卡号, 交易日期
HAVING COUNT( 交易金额 )>=2
from tb A
where 交易金额 >50
and exists (select 1 from tb where A.卡号=B.卡号 and b.交易金额 >50)
from (select * from tb where 交易金额>50) t
where 交易日期 in(
select 交易日期
from(select * from tb where 交易金额>50) t
group by 交易日期
having COUNT(1)>1)
-- -----------t_mac 小编-------------------
--------------------希望有天成为大虾----
-- =========================================IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb
GO
CREATE TABLE tb( 卡号 int,交易日期 varchar(10), 交易金额 int)
go
insert tb SELECT
123, '1.1' , 111 UNION ALL SELECT
123, '1.1' , 59 UNION ALL SELECT
456, '1.1' , 110 UNION ALL SELECT
123, '1.2' , 36 UNION ALL SELECT
123, '1.2' , 78 UNION ALL SELECT
456, '1.2' , 89 UNION ALL SELECT
456, '1.2' , 55
go
select *
from (select * from tb where 交易金额>50) z
where exists(
select *
from(select * from tb where 交易金额>50) t
where z.卡号=卡号 and z.交易日期=交易日期
group by 交易日期,卡号
having COUNT(1)>1)
go
/*
卡号 交易日期 交易金额
----------- ---------- -----------
123 1.1 111
123 1.1 59
456 1.2 89
456 1.2 55
*/
IF OBJECT_ID('table1') IS NOT NULL
DROP TABLE table1
GO
CREATE TABLE table1( 卡号 varchar(12), 交易日期 varchar(12), 交易金额 int)
go
/*
IF OBJECT_ID('table2') IS NOT NULL
DROP TABLE table2
GO
CREATE TABLE table2( item_A varchar(12), [name] varchar(12), docid varchar(12) )
goIF OBJECT_ID('table3') IS NOT NULL
DROP TABLE table3
GO
CREATE TABLE table3( docid varchar(12), des varchar(12), item_number varchar(12))
go
*/
insert table1 select
'123' , '01-01' , 111 union all select
'123' , '01-01' , 59 union all select
'456' , '01-01' , 110 union all select
'123' , '01-02' , 36 union all select
'123' , '01-02', 78 union all select
'456' , '01-02', 89 union all select
'456' , '01-02', 55 select t.* from table1 t
join (
select 卡号, 交易日期 from table1
where 交易金额>50
group by 卡号, 交易日期 having(count(*)>1)
)b
on t.卡号=b.卡号 and t.交易日期=b.交易日期卡号 交易日期 交易金额
------------ ------------ -----------
123 01-01 111
123 01-01 59
456 01-02 89
456 01-02 55(4 行受影响)
drop table test
create table test(id int,交易日期 varchar(10),交易金额 int)
insert into test
select 123,'1.1',111 union all
select 123,'1.1',59 union all
select 456,'1.1',110 union all
select 123,'1.2',36 union all
select 123,'1.2',78 union all
select 456,'1.2',89 union all
select 456,'1.2',55
go
select * from testSELECT id, 交易日期 ,sum( 交易金额 )AS NUM FROM test WHERE 交易金额 >=50 GROUP BY id, 交易日期 HAVING COUNT( 交易金额 )>=2id 交易日期 NUM
----------- ---------- -----------
123 1.1 170
456 1.2 144(所影响的行数为 2 行)
declare @t table(卡号 int,交易日期 varchar(50),交易金额 int)
insert into @t
select 123,'1.1',111 union all
select 123,'1.1',59 union all
select 456,'1.1',110 union all
select 123,'1.2',36 union all
select 123,'1.2',78 union all
select 456,'1.2',89 union all
select 456,'1.2',55
select m.卡号,m.交易日期,t.交易金额
from
(select 卡号 ,交易日期 from @t where 交易金额>50 group by 卡号 ,交易日期 having count(*)>1)
m
join @t t
on m.卡号=t.卡号 and m.交易日期=t.交易日期/*
卡号 交易日期 交易金额
----------- -------------------------------------------------- -----------
123 1.1 111
123 1.1 59
456 1.2 89
456 1.2 55(4 行受影响)
*/
select 卡号 , 交易日期, 交易金额
from tb
where 交易金额>50 or sum(交易日期)>1
select 卡号 , 交易日期, 交易金额
from tb
where 交易金额>50 or count(交易日期)>1
if object_id('[TB]') is not null drop table [TB]
create table [TB]([卡号] int,[交易日期] numeric(2,1),[交易金额] int)
insert [TB]
select 123,1.1,111 union all
select 123,1.1,59 union all
select 456,1.1,110 union all
select 123,1.2,36 union all
select 123,1.2,78 union all
select 456,1.2,89 union all
select 456,1.2,55select * from TB where 交易金额>50 and 交易金额 not in(
select max(交易金额) from TB where 交易金额>50
group by 卡号,交易日期 having count(1)<2)/*卡号 交易日期 交易金额
----------- --------------------------------------- -----------
123 1.1 111
123 1.1 59
456 1.2 89
456 1.2 55(4 行受影响)*/
drop table TB
declare @a table(卡号 int, 交易日期 varchar(5), 交易金额 int )
insert @a select
123, '1.1', 111 union all select
123, '1.1', 59 union all select
456, '1.1', 110 union all select
123, '1.2', 36 union all select
123, '1.2', 78 union all select
456, '1.2', 89 union all select
456, '1.2', 55 select b.* from @a as b inner join (select 卡号,交易日期 from @a where 交易金额>50 group by 卡号,交易日期 having count(1)>=2) as a
on b.卡号=a.卡号 and b.交易日期=a.交易日期
where b.交易金额>50
if object_id('[TB]') is not null drop table [TB]
create table [TB]([卡号] int,[交易日期] numeric(2,1),[交易金额] int)
insert [TB]
select 123,1.1,111 union all
select 123,1.1,59 union all
select 456,1.1,110 union all
select 123,1.2,36 union all
select 123,1.2,78 union all
select 456,1.2,89 union all
select 456,1.2,55select * from tb
--method1 (交易金额 not in 这点不是 很明白)
select * from TB where 交易金额>50 and 交易金额 not in(select max(交易金额) from TB where 交易金额>50 group by 卡号,交易日期 having count(1)<2)
--method2
select b.* from TB as b inner join (select 卡号,交易日期 from TB where 交易金额>50 group by 卡号,交易日期 having count(1)>=2) as a
on b.卡号=a.卡号 and b.交易日期=a.交易日期
where b.交易金额>50
--method3
select * from
(select * from tb where 交易金额>50) z
where exists(
select * from(select * from tb where 交易金额>50) t
where z.卡号=卡号 and z.交易日期=交易日期
group by 交易日期,卡号
having COUNT(1)>1)
--
select t.* from tb t
join (
select 卡号, 交易日期 from tb
where 交易金额>50
group by 卡号, 交易日期 having(count(*)>1)
)b
on t.卡号=b.卡号 and t.交易日期=b.交易日期
大杂烩
inner join
(select [卡号],[交易日期] from tb WHere [交易金额]>50 group by [卡号],[交易日期] having count(*)>1 )
Temp on tb.[卡号]=Temp.[卡号] and tb.[交易日期]=Temp.[交易日期]