Focal Point
[CLOSED] Intermediate SQL Question involving Partition/Over

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/7507072196

April 29, 2019, 11:39 AM
intsoccersuperstar
[CLOSED] Intermediate SQL Question involving Partition/Over
I have two tables. The first looks like this:

Unit Status
1001 A
1002 A
1003 A
1004 A
1005 A
1006 A
1007 A
1008 A
1009 A
1010 A

The second looks like this:

Unit Status
2001 Z
2002 T
2003 X
2004 W
2005 M

I would like to join these two tables in such a way that every unit in the 2 table is "assigned" 2 units from the 1 table. For example, Unit 2001 is "assigned" units 1001 and 1002, unit 2002 is "assigned" units 1003 and 1004, and so on. The order is not important, just that two unique units from table 1 are assigned to one unit from table two.

I would like my final product to look something like this:

Unit Status Assignment_1 Assignment_2
2001 Z 1001 1002
2002 T 1003 1004

And so forth.

Any help would be greatly appreciated! Of course if you want to tell me to go take more SQL training, that is fine too!

Thanks!

This message has been edited. Last edited by: FP Mod Chuck,


intsoccersuperstar
WebFOCUS 8105m
Windows, Excel
April 30, 2019, 08:16 AM
MartinY
Not sure to understand the purpose of this, but with a few coding I can have the below
TABLE FILE CAR
SUM COMPUTE IDXT  /I2 = IDXT + 1; NOPRINT
    COMPUTE IDX   /I2 = IF DMOD(IDXT, 2, 'I2') EQ 0 THEN LAST IDXT ELSE IDXT;
    COMPUTE ROWID /I2 = IF LAST ROWID EQ 1 THEN 2 ELSE 1;
BY CAR
ON TABLE HOLD AS SRCA
END
-RUN

TABLE FILE CAR
SUM COMPUTE IDX/I2 = IF IDX EQ 0 THEN 1 ELSE IDX + 2;
BY COUNTRY
ON TABLE HOLD AS SRCB FORMAT FOCUS INDEX IDX
END
-RUN

JOIN 
     IDX IN SRCA
  TO IDX IN SRCB TAG J1 AS J1
END
SET ACRSVRBTITL  = ON
TABLE FILE SRCA
SUM CAR    AS 'Car'
BY COUNTRY AS 'Country'
ACROSS ROWID NOPRINT
ON TABLE SET PAGE-NUM NOLEAD
END
-RUN

Please, when posting sample data, format or code, use the tag code (last one on the ribbon) :
</>



WF versions : Prod 8.2.04M gen 33, Dev 8.2.04M gen 33, OS : Windows, DB : MSSQL, Outputs : HTML, Excel, PDF
In Focus since 2007