有两张表
一张是客户表
有两个字段 :客户名称 varchar、 结账日 int (不同客户的结账日期 是不同的,如有的每月15日结,有的20日结) A 15
B 20
C 25
D 25
E 15
另一张是出入库表
有三个字段 :客户名称 varchar、单据日期 datetime 、数量 int (这张表流水的记录了向各客户每天的送货情况)
A 2008-02-01 100
A 2008-03-15 100
A 2008-05-16 100
A 2008-05-30 100
A 2008-06-18 100
B 2008-02-01 100
B 2008-03-15 100
B 2008-05-16 100
B 2008-05-30 100
B 2008-06-18 100
现在想查出6月的数据
请问如何根据不同客户的结账期实现数据查询
(如A 客户按 2008-05-16到2008-06-15 而B客户按2008-05-21到2008-06-20查)上例中结果应该是 A 2008-05-16 100
A 2008-05-30 100
B 2008-05-30 100
B 2008-06-18 100
一张是客户表
有两个字段 :客户名称 varchar、 结账日 int (不同客户的结账日期 是不同的,如有的每月15日结,有的20日结) A 15
B 20
C 25
D 25
E 15
另一张是出入库表
有三个字段 :客户名称 varchar、单据日期 datetime 、数量 int (这张表流水的记录了向各客户每天的送货情况)
A 2008-02-01 100
A 2008-03-15 100
A 2008-05-16 100
A 2008-05-30 100
A 2008-06-18 100
B 2008-02-01 100
B 2008-03-15 100
B 2008-05-16 100
B 2008-05-30 100
B 2008-06-18 100
现在想查出6月的数据
请问如何根据不同客户的结账期实现数据查询
(如A 客户按 2008-05-16到2008-06-15 而B客户按2008-05-21到2008-06-20查)上例中结果应该是 A 2008-05-16 100
A 2008-05-30 100
B 2008-05-30 100
B 2008-06-18 100
union all
select * from tb where 单据日期 between '2008-05-21' and '2008-06-20' and 客户名称='B'
or (单据日期 between '2008-05-21' and '2008-06-20' and 客户名称='B') 还不如union all 呢.
set @d = '2008-06-01'
select * from crk a,kh b
where a.日期 between dateadd(month,-1,dateadd(day,b.结帐日,@d))
and dateadd(day,b.结帐日-1,@d))
set @d = '2008-06-01'
select * from crk a,kh b
where a.日期 between dateadd(month,-1,dateadd(day,b.结帐日,@d))
and dateadd(day,b.结帐日-1,@d))
and a.客户名称 = b.客户名称
from 出入库表 as a
inner join 客户表 as b on a.客户名称=b.客户名称
and a.单据日期 between dateadd(day,(b.结账日),'2008-05-01') and dateadd(day,b.结账日-1,'2008-06-01')
if object_id('tempdb.dbo.#客户表') is not null drop table #客户表
create table #客户表 (客户名称 varchar(1),结账日 int)
insert into #客户表
select 'A',15 union all
select 'B',20 union all
select 'C',25 union all
select 'D',25 union all
select 'E',15--> 测试数据: #出入库表
if object_id('tempdb.dbo.#出入库表') is not null drop table #出入库表
create table #出入库表 (客户名称 varchar(1),单据日期 datetime,数量 int)
insert into #出入库表
select 'A','2008-02-01',100 union all
select 'A','2008-03-15',100 union all
select 'A','2008-05-16',100 union all
select 'A','2008-05-30',100 union all
select 'A','2008-06-18',100 union all
select 'B','2008-02-01',100 union all
select 'B','2008-03-15',100 union all
select 'B','2008-05-16',100 union all
select 'B','2008-05-30',100 union all
select 'B','2008-06-18',100declare @s varchar(10)
set @s='2008-06-01' --6月
select a.* from #出入库表 as a
inner join #客户表 as b on a.客户名称=b.客户名称
and a.单据日期 between dateadd(day,(b.结账日),dateadd(month,-1,@s)) and dateadd(day,b.结账日-1,@s)/*
--结果
A 2008-05-16 00:00:00.000 100
A 2008-05-30 00:00:00.000 100
B 2008-05-30 00:00:00.000 100
B 2008-06-18 00:00:00.000 100
*/
DECLARE @TB TABLE(客户名称 varchar(20),结账日 int)
INSERT @TB SELECT 'A',15
UNION ALL SELECT 'B',20
UNION ALL SELECT 'C',25
UNION ALL SELECT 'D',25
UNION ALL SELECT 'E',15
DECLARE @TA TABLE(客户名称 varchar(20),单据日期 datetime,数量 int)
INSERT @TA SELECT 'A','2008-02-01',100
UNION ALL SELECT 'A','2008-03-15',100
UNION ALL SELECT 'A','2008-05-30',100
UNION ALL SELECT 'A','2008-06-18',100
UNION ALL SELECT 'A','2008-05-16',100
UNION ALL SELECT 'B','2008-02-01',100
UNION ALL SELECT 'B','2008-03-15',100
UNION ALL SELECT 'B','2008-05-30',100
UNION ALL SELECT 'B','2008-06-18',100
UNION ALL SELECT 'B','2008-05-16',100
DECLARE @DATE DATETIME
SET @DATE = '2008-06-01'
SELECT A.*,B. 结账日 FROM @TA A,@TB B
WHERE A.单据日期 BETWEEN DATEADD(MONTH,-1,DATEADD(DAY,b.结账日,@DATE))
AND DATEADD(DAY,B.结账日-1,@DATE)
AND A.客户名称 = B.客户名称--结果
/*
客户名称 单据日期 数量 结账日
-------------------- ------------------------------------------------------ ----------- -----------
A 2008-05-16 00:00:00.000 100 15
A 2008-05-30 00:00:00.000 100 15
B 2008-05-30 00:00:00.000 100 20
B 2008-06-18 00:00:00.000 100 20(所影响的行数为 4 行)
*/
join 客户表 b on(a.客户名称=b.客户名称 and convert(varchar(8),单据日期,112) between '200805'+convert(varchar(2),结账日+1) and '200806'+convert(varchar(2),结账日))
select distinct a.* from customer_out a
join customer b on(a.name=b.name and convert(varchar(8),out_date,112) between '200805'+convert(varchar(2),acc_date+1) and '200806'+convert(varchar(2),acc_date))
join 客户表 b on(a.客户名称=b.客户名称 and convert(varchar(8),单据日期,112) between '200805'+convert(varchar(2),结账日+1) and '200806'+convert(varchar(2),结账日))