我有以下情况,我有一个客户在我的数据库上他购买了特定的信息组合,我的意思是,目前该表显示了客户的名称,类别,地点,年份,国家,C1,C2 ,交付,频率和一个例子看起来像这样。
Customer Categ Place YM Country C1 C2 delivery Frequency
ALB Clock WM 201609 France 1 1 name Trimestral- 3,6,9,12
ALB car NewE 201610 Spain 1 0 Trimestral- 3,6,9,12
ALC TV AWS 201611 Guate 1 0 Trimestral- 3,6,9,12
ALB smart Eb 201612 Japan 1 1 Trimestral- 3,6,9,12
有时交货有名称和其他一些它不会取决于客户也C2取决于频率,它与YM有关。
让我们说如果频率是三分之一的话,那么当月份是3/6/9/12时我需要C2为1但是我必须选择从201701到201712生成值,而另一个只生成值时它是201703/201706等等。
所有这些价值将使用当年的最后四个月生成。
如果您有任何疑问,请告诉我,因为我知道这有点棘手
我正在使用案例句子,但它是复制组合,我的意思是创建一行c2 = 1和c2 = 0
Customer Categ Place YM Country C1 C2 delivery Frequency
ALB Clock WM 201609 France 1 1 name Trimestral- 3,6,9,12
ALB Clock WM 201609 France 1 0 name Trimestral- 3,6,9,12
case
when @Frecuencia_De_Proceso = 'Anual' and cast(right([Año_Mes],2) as int) % 12 = 0 and b.cadena <> 'Club Co' then 1
when @Frecuencia_De_Proceso in ('Bimensual','Bimensual 2,4,6,8...', 'Bimensual II') and cast(right([Año_Mes],2) as int) % 2 = 0 and b.cadena <> 'Club Co' then 1
when @Frecuencia_De_Proceso in ('Bimensual I')and cast(right([Año_Mes],2) as int) % 2 = 1 and b.cadena <> 'Club Co' then 1
when @Frecuencia_De_Proceso = 'Trimestral I' and cast(right([Año_Mes],2) as int) % 3 = 1 and b.cadena <> 'Club Co' then 1
when @Frecuencia_De_Proceso = 'Trimestral I' and cast(right([Año_Mes],2) as int) % 3 = 2 and b.cadena <> 'Club Co' then 1
when @Frecuencia_De_Proceso in('Trimestral- 3,6,9,12', 'Trimestral III') and cast(right([Año_Mes],2) as int) % 3 = 0 and b.cadena <> 'Club Co' then 1
when @Frecuencia_De_Proceso = 'Semestral' and cast(right([Año_Mes],2) as int) % 6 = 0 and b.cadena <> 'Club Co' and @Especial = '' then 1
when @Frecuencia_De_Proceso = 'Mensual' and b.cadena <> 'Club Co' then 1
when @Especial = 'Especial' and Año_Mes in (201610,201611,201612) and b.cadena <> 'Club Co' then 1
else
0 end ) as [Entregar]
也许是因为我想生成独特的价值观,并且不知道如何帮助或建议如何做到这一点。
以下是整个查询..
DECLARE @startnum INT=201701
DECLARE @endnum INT=201712
;
WITH gen AS (
SELECT @startnum AS num
UNION ALL
SELECT num+1 FROM gen WHERE num+1<=@endnum
)
SELECT * into #Temp FROM gen
option (maxrecursion 13)
select distinct Customer, Category,Place,b.num,country,
C1,
case
when Frecuency = 'Anual' and cast(right([Año_Mes],2) as int) % 12 = 0 and a.Place <> 'Club Co' then 1
when Frecuency in ('Bimensual','Bimensual 2,4,6,8...', 'Bimensual II') and cast(right([Año_Mes],2) as int) % 2 = 0 and a.Place <> 'Club Co' then 1
when Frecuency in ('Bimensual I')and cast(right([Año_Mes],2) as int) % 2 = 1 and a.Place <> 'Club Co' then 1
when Frecuency = 'Trimestral I' and cast(right([Año_Mes],2) as int) % 3 = 1 and a.Place <> 'Club Co' then 1
when Frecuency = 'Trimestral I' and cast(right([Año_Mes],2) as int) % 3 = 2 and a.Place <> 'Club Co' then 1
when Frecuency in('Trimestral- 3,6,9,12', 'Trimestral III') and cast(right([Año_Mes],2) as int) % 3 = 0 and a.Place <> 'Club Co' then 1
when Frecuency = 'Semestral' and cast(right([Año_Mes],2) as int) % 6 = 0 and a.Place <> 'Club Co' then 1
when Frecuency = 'Mensual' and a.Place <> 'Club Co' then 1
else
0 end as C2,
case when [name] <> '' then '' else [name] end as [name],
Frecuency,Cortes
--into [XtraTest].dbo.OP2017_B
from DB.dbo.Prod a,
#Temp b
where Año_Mes between 201608 and 201612 and Frecuency not in ('BackData','Cancelado')
and Frecuency in ('Bimensual','Bimensual 2,4,6,8...', 'Bimensual II')
order by Customer,num,Place
答案 0 :(得分:1)
使用测试数据进行了更新
我认为在与生成的值进行交叉连接之前,您需要从db.dbo.Prod
获取不同的行。
您可能还有Trimestral I
次,而不是Trimestral II
,请参阅下面的代码。
你的案例陈述也可以稍微优化一下,如下:
Rextester:Update-FormatData
create table Prod (Customer varchar(32),Categ varchar(32),Place varchar(32),YM int,Country varchar(32),C1 int,delivery varchar(32),Frequency varchar(32));
insert into Prod (Customer,Categ,Place,YM,Country,C1,delivery,Frequency) values
('ALB','Calendar','NewE',201610,'Spain' ,1,'','Anual')
,('ALB','Clothes','Eb' ,201609,'Japan' ,1,'','Semestral')
,('ALB','Manga','Eb' ,201612,'Japan' ,1,'','Mensual')
,('ALB','Razor','Eb' ,201612,'Japan' ,1,'','Bimensual I')
,('ALB','Toy','Eb' ,201612,'Japan' ,1,'','Bimensual II')
,('ALB','Clock','WM' ,201609,'France',1,'name','Trimestral I')
,('ALB','car' ,'NewE',201610,'Spain' ,1,'','Trimestral II')
,('ALC','TV' ,'AWS' ,201611,'Guate' ,1,'','Trimestral III')
,('ALB','Widget','Club Co',201608,'Denmark',1,'','Trimestral- 3,6,9,12');
declare @startnum int=201701;
declare @endnum int=201712;
with gen as (
select @startnum as num, mth=month(convert(date,convert(nvarchar(8),@startnum)+'01',120))
union all
select num+1, mth=month(convert(date,convert(nvarchar(8),num+1)+'01',120)) from gen where num+1<=@endnum
)
select * into #Temp from gen
option (maxrecursion 12);
with DistinctProd as (
select distinct
Customer
, Category = Categ
, Place
, country
, C1
, [Name] = case when [Delivery] <> '' then '' else [Delivery] end
, Frequency
--, Cortes
from Prod a
where YM between 201608 and 201612
and Frequency not in ('BackData','Cancelado')
--and Frequency in ('Bimensual','Bimensual 2,4,6,8...', 'Bimensual ii')
)
, GenC2 as (
select
Customer
, Category
, Place
, b.num
, country
, C1
, C2 = case
when a.Place = 'Club Co'
then 0
when Frequency = 'Anual'
and mth % 12 = 0
then 1
when Frequency in ('Bimensual','Bimensual 2,4,6,8...', 'Bimensual ii')
and mth % 2 = 0
then 1
when Frequency in ('Bimensual I')
and mth % 2 = 1
then 1
when Frequency = 'Trimestral I'
and mth % 3 = 1
then 1
when Frequency = 'Trimestral II' -- I think this is meant to be Trimestral II'
and mth % 3 = 2
then 1
when Frequency in('Trimestral- 3,6,9,12', 'Trimestral iii')
and mth % 3 = 0
then 1
when Frequency = 'Semestral'
and mth % 6 = 0
then 1
when Frequency = 'Mensual'
then 1
else 0
end
, [Name]
, Frequency
, Mth
--, Cortes
--into [XtraTest].dbo.op2017_B
from DistinctProd as a
cross join #Temp as b
)
select *
from GenC2
--where c2=1
order by Customer,Place,Category,num