34,588
社区成员
发帖
与我相关
我的任务
分享
use Tempdb
go
--> --> 中国风(Roy)生成測試數據
if not object_id(N'Tempdb..#sellinfo') is null
drop table #sellinfo
Go
Create table #sellinfo([ID] int,[bh] nvarchar(22),[rq] Date,[A1] int,[A2] int,[A3] int)
Insert #sellinfo
select 1,N'c1','2018-07-01',5,0,0 union all
select 2,N'c1','2018-07-01',0,10,0 union all
select 3,N'c1','2018-07-12',0,20,0 union all
select 4,N'c2','2018-07-01',15,0,0 union all
select 5,N'c2','2018-07-01',5,0,0 union all
select 6,N'c3','2018-07-01',5,0,0 union all
select 7,N'c4','2018-07-12',5,0,0 union all
select 8,N'c5','2018-07-01',5,0,0 union all
select 9,N'c5','2018-07-01',0,10,0 union all
select 10,N'c5','2018-07-01',0,0,10
Go
SELECT [bh]
, [rq]
FROM #sellinfo
GROUP BY [bh]
, [rq]
HAVING MAX(SIGN(A1)) +MAX(SIGN(A2)) +MAX(SIGN(A3)) =3;
/*
bh rq
c5 2018-07-01
*/
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[bh] nvarchar(22),[rq] Date,[A1] int,[A2] int,[A3] int)
Insert #T
select 1,N'c1','2018-07-01',5,0,0 union all
select 2,N'c1','2018-07-01',0,10,0 union all
select 3,N'c1','2018-07-12',0,20,0 union all
select 4,N'c2','2018-07-01',15,0,0 union all
select 5,N'c2','2018-07-01',5,0,0 union all
select 6,N'c3','2018-07-01',5,0,0 union all
select 7,N'c4','2018-07-12',5,0,0 union all
select 8,N'c5','2018-07-01',5,0,0 union all
select 9,N'c5','2018-07-01',0,10,0 union all
select 10,N'c5','2018-07-01',0,0,10
Go
--测试数据结束
Select bh,rq from #T
GROUP BY bh,rq
HAVING SUM(
DISTINCT
CASE WHEN A1>0 THEN 1 ELSE 0 END )+SUM(
DISTINCT
CASE WHEN A2>0 THEN 1 ELSE 0 END )+SUM(
DISTINCT
CASE WHEN A3>0 THEN 1 ELSE 0 END )>1