Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
captain89
Creator
Creator

join with Cartesian product (join con prodotto cartesiano)

Hi!, I need the result of this sql join in a qlik table:

with tab1 as (                                          

select cdarmm, cdmgmm, min(dtmomm) as dtmin, max(dtmomm) as dtmax

from mgmov00f group by cdarmm, cdmgmm)                                

                                                        

select cdarmm, cdmgmm, dtmomx from tab1 join tab2 on dtmomx > dtmin and dtmomx<=dtmax

I've loaded two tables in my .qvw:

tab1:

Item CodeInventoryDate first movDate last mov
A0018882012010320120301
A0010012011010220110430
B0025552014060820140701
B0020012014050120140920

tab2:

Monthly Calendar
20120101
20120201
20120301
20120401

20120501

.........
20140801
20140901

This is the result I need:

Tab3:

Item CodeInventoryMonthly
A00188820120101
A00188820120201
A00188820120301
A00100120110101
.................

I must select the record where the (date first mov < monthly calendar) and (date last mov. >= monthly calendar)

So I need to duplicate records like a cartesian product but conditioned by an interval of date.

Sorry for my English.

Thanks for the support.

1 Solution

Accepted Solutions
maxgro
MVP
MVP

1.png

a:

LOAD [Item Code],

     Inventory,

     [Date first mov],

     [Date last mov]

FROM

[https://community.qlik.com/thread/167817]

(html, codepage is 1252, embedded labels, table is @1);

for y = 2011 to 2014

  for m=1 to 12

  b:

  load $(y) & num($(m), '00') & '01' as [Monthly Calendar]

  autogenerate 1;

next;

next;

join(a) load * Resident b;

DROP Table b;

final:

NoConcatenate

load * Resident a

where left([Monthly Calendar],6) >= left([Date first mov],6) and left([Monthly Calendar],6) <= left([Date last mov],6);

DROP Table a;

View solution in original post

3 Replies
arthur_dom
Creator III
Creator III

tried intervalmatch function?

Re: IntervalMatch Multiplying Records

maxgro
MVP
MVP

1.png

a:

LOAD [Item Code],

     Inventory,

     [Date first mov],

     [Date last mov]

FROM

[https://community.qlik.com/thread/167817]

(html, codepage is 1252, embedded labels, table is @1);

for y = 2011 to 2014

  for m=1 to 12

  b:

  load $(y) & num($(m), '00') & '01' as [Monthly Calendar]

  autogenerate 1;

next;

next;

join(a) load * Resident b;

DROP Table b;

final:

NoConcatenate

load * Resident a

where left([Monthly Calendar],6) >= left([Date first mov],6) and left([Monthly Calendar],6) <= left([Date last mov],6);

DROP Table a;

captain89
Creator
Creator
Author

Thanks Massimo. Maybe I did it in the same way you indicated, I think.

First I make three variables:

LET vFIRST = LEFT(FieldValue('V_DTMINN', 1), 6); (this is the date of the first movement in the inventory file)

LET vSTART = 20140101;

LET vOGGI = year(today())*10000+month(today())*100+day(today());

Then I load a table with the date of the first and the last stock movement for item number, inventory number

[Data Primo-Ultimo Mov.]:

LOAD

TEXT(CDARMM) AS [Cod. Articolo W],

Text(CDMGMM) as [Cod. Magazzino W],

Date(Date#(DTMMIN, 'YYYYMMDD')) AS [Data Primo Mov.Mag. W],

Date(Date#(DTMMAX, 'YYYYMMDD')) as [Data Ultimo Mov. Mag W],

Date(Date#(left($(vOGGI), 6)*100+1, 'YYYYMMDD')) as [Data Ultimo Saldo W],

date(date#(if((LEFT(DTMMIN, 6)*100+1)>=$(vSTART),  LEFT(DTMMIN, 6)*100+1, $(vSTART)), 'YYYYMMDD'))  AS [Data Primo Saldo W]

;

SQL

SELECT CDARMM, CDMGMM, MAX(DTMOMM) AS DTMMAX, MIN(DTMOMM) as DTMMIN FROM MGMOV00F

WHERE CDARMM IN $(vIN)

GROUP BY CDARMM, CDMGMM

;

I joined with the monthly calendar:

join

load

date(Date#([Data Saldo Work]*100+1, 'YYYYMMDD')) as [Data Saldo W]

;

load

if((left($(vFIRST),4)=left(recno()+$(vFIRST)-1,4) and right(recno()+$(vFIRST)-1,2)*1<13), recno()+$(vFIRST)-1, recno()+$(vFIRST)-1+

88*(floor((right(recno()+$(vFIRST)-1,3)*1-(mid($(vFIRST),4,1)*100+12)-1)/12)+1)) as [Data Saldo Work]

Autogenerate(left($(vOGGI), 6)-left($(vFIRST), 6) - 88*(left($(vOGGI), 4)-left($(vFIRST), 4)) + 1);

I take only the records between the interval of dates.

[Saldi]:

load

text([Cod. Articolo W]) as [Cod. Articolo W1],

text([Cod. Magazzino W]) as [Cod. Magazzino W1],

[Data Saldo W] as [Data Saldo W1],

[Data Primo Saldo W] as [Data Primo Saldo W1],

[Flag Ripresa Saldi W] as [Flag Ripresa Saldi W1]

resident [Data Primo-Ultimo Mov.]

where [Data Saldo W]>=[Data Primo Saldo W]

;

drop table [Data Primo-Ultimo Mov.];

left join ([Saldi])

LOAD

text(CDARMM) AS [Cod. Articolo W1],

text(CDMGMM) AS [Cod. Magazzino W1],

date(Date#(DTMOMM*100+1, 'YYYYMMDD')) AS [Data Mese Variazione W1],

QTMOMS AS [Qtà Variaz. Mensile W1]

;

SQL: sum of the stock movements by item number, inventory number, monthname

.......

I do the inventory balances:

[Saldi Finale]:

load

[Cod. Articolo W1] as [Cod. Articolo W2],

[Cod. Magazzino W1] as [Cod. Magazzino W2],

[Data Saldo W1] as [Data Saldo W2],

sum([Qtà Variaz. Mensile W1]) as [Qtà Saldo Mensile W2]

resident Saldi

where [Data Saldo W1]>[Data Mese Variazione W1]

GROUP BY [Cod. Articolo W1], [Cod. Magazzino W1], [Data Saldo W1]

;

drop table Saldi;

then i re-load all the stock movements... and with a concatenate I consider the montly inventory balance like a simple stock movement.