How to combine two selects of one table into an internal table? - abap

I have a self-created Z-table of this structure:
SPWOC NUMC 6
VKORG CHAR 4
MATNR CHAR 18
KUNNR CHAR 10
OLFMNG QUAN 13 (reference VOLEH)
WADAT DATS 8
VOLEH UNIT 3
How can I create an internal table with three additional fields:
SPWOC2
OLFMNG2
WADAT2
With two different calendar weeks I want to fill this internal table to be able to compare SPWOC and SPWOC2, OLFMNG an OLFMNG2 and WADAT and WADAT2.
With
SELECT * FROM ZTABLE INTO CORRESPONDING FIELDS of TABLE it_table where spwoc = l_kw1.
I get the calendar week 1 into internal table, but how can I add data of the second week into same rows?

In Your program:
TYPES: BEGIN OF gty_zextend,
INCLUDE TYPE your_ztype,
SPWOC2 TYPE referring_type,
OLFMNG2 TYPE referring_type,
WADAT2 type referring_type,
END OF gty_zextend.
DATA: lt_itab TYPE STANDARD TABLE OF gty_zextend.
There You have it.
And, remember, keyword "AS" is also possible for table-fields(columns), what makes it relatively easy, to use "into corresponding fields of table" once, You specify like this , column name "another_date" as wadat2 for example. But I think, Your source for the other three field is another table, right ?

Related

Generate random records from the table tblFruit based on the field Type

I will need your help to generate random records from the table tblFruit based on the field Type (without no duplication)
As per the above table.
There are 4 type of fruit number 1,2,3,4
I want to generate x records dynamically from the table tblFruit (e.g 7 records).
Let say I need to get 7 random record of fruit .
My result should contains fruit of the different types. However, we need to ensure that the result contains only 7 records.
i.e
2 records of type 1,
2 records of type 2,
2 records of type 3,
1 records of type 4
e.g
Note: If i want to generate 10 records (without no duplication),
then i will get 2 records of each type and the two remaining records randomly of any type.
Much grateful for your help.
I might suggest:
select top (7) f.*
from tblfruit f
order by row_number() over (partition by type order by newid());
This will actually produce a result with approximately the same number of rows of each type (well, off by 1), but that meets your needs.

Populate NULL Values based on Array Formula

New user, so apologies in advance for bad formatting.
Essentially what I'm trying to do is be able to populate the staff_hours column where it equals NULL with the one value that IS NOT NULL. As you can see from the screenshot, there will only be one person who staffs an open cl_hole_staffing_no and as a result will have a start_dt (with time) and end_dt (with time) along with staff_hours. 16 people were offered a shift, and the person in row 15 accepted it is what is going on here.
The ideal output would be the staff_hours column is populated with the amount of time of the one person who ended up taking the open job, so 24.00 in this example. How can I write a formula to do this? I was thinking something like an array function in Excel, but am not sure how to do that in SQL.
Your explanation is a bit confusing about what you are really trying to achieve. However I think that what you really want is just to populate the staff_hours column, which can be achieved with the following:
UPDATE
your_table_name
SET
staff_hours = 24
WHERE
staff_hours is NULL;
EDIT
I get it now. You want to operate with the two dates and extract the amount of hours between them. Since you are in sql-server you can actually define a Computed Column in which you can use the values from other columns to compute the value you want.
You will need to create your table again. (The example below contains only the necessary attributes for it to work)
CREATE TABLE your_table_name
( id INT IDENTITY (1,1) NOT NULL
, staff_start_dt DATETIME
, staff_end_dt DATETIME
, staff_hours AS DATEDIFF(hh, staff_start_dt , staff_end_dt)
);
Now every time you insert a record on the table with both staff_start_dt and staff_end_dt, the column staff_hours will automatically compute the number of hours between the two dates.
[pre]
Code (vb):
A B C
1 10 X X
2 11 A Y
3 12 Y Z
4 13 B
5 14 B
6 15 Z
[/pre]
Assuming that the rows in Col A is Named "datarange"
And your criteria is in C1:C3
The following formula will return an array {10,12,15}
=SMALL(COUNTIF(C1:C3,B1:B6)*datarange, ROW(INDEX(A:A,SUMPRODUCT(--(COUNTIF(C1:C3,B1:B6)=0))+1):INDEX(A:A,ROWS(datarange))))
COUNTIF(C1:C3,B1:B6)*datarange returns {10;0;12;0;0;15}
The segment ROW(INDEX(....):INDEX(...)) returns {4;5;6}, indicating the number of non-zero values.
The SMALL() function then returns the 4th smallest, 5th smallest and 6th smallest values.
One disadvantage with this approach is that you get a sorted sub-list. Perhaps that would work for you.

Update postgresql table column with increasing reference

I have a postgresql table that have a reference column (different than the id).
I want to iterate on the table and add a reference code ('ITEM001', 'ITEM002", etc) for each line.
If I want to change for all line with the same reference, I do:
UPDATE stock SET reference = 'ITEM001';
How could I update the column of all table based on the id ?
Current situation:
id name reference
1 item-name-1
2 item-name-2
3 item-name-3
10 item-name-10
11 item-name-11
Desired result:
id name reference
1 item-name-1 IT001
2 item-name-2 IT002
3 item-name-3 IT003
10 item-name-10 IT010
11 item-name-11 IT011
You can use to_char() to format the number:
UPDATE stock
SET reference = 'IT' || to_char(id, 'FM000');
The FM parameter for the format mask makes sure that the formatted value does not contain any blanks.
But in general it is a bad idea to store information that can easily be derived from existing data.
The better solution would be to create a view that returns that information:
create or replace view stock_with_reference
as
select *, 'IT' || to_char(id, 'FM000') as reference
from stock;

DAX - selecting rows with partial match

I have a powerpivot table that contains 2 columns:
Column 1 contains strings.
Column 2 contains comma delimited strings.
I would like to be able to display all the rows from column 1 when rows from column 2 contains the selection from a filter or slicer. For example:
String Values
ABCD A,A,B
EFGH A,C
if A is selected I would display both rows, if B is selected I would display only row 1...etc.
I know I can split the records - but this is not practical for me - the above is only the top of the iceberg. VBA is out of the question since this will published in SharePoint. Anybody has an idea on how I could do that ? Thanks.
I found the solution in a blog from Javier Guillem:
http://javierguillen.wordpress.com/2012/02/10/simulating-an-approximate-match-vlookup-in-powerpivot/
If in my example the name of the table is "facts", I create a second unlinked table called dimRef that I populate with all possible values that I am interested to match: A,B,C,D...etc.
Then I define the measure M as:
M:=If ( Hasonevalue(facts[Values] ),
Calculate (
LASTNONBLANK (dimRef[String], 1 ),
Filter ( dimRef, SEARCH(dimRef[String],Values(facts[String]),1,0) > 0 )
)
)
I can then use the string column of the facts table and the measure in a pivot table and use dimRef as a selector. If filters the row as per the selection.
One small detail: the measure is not available in PowerView...Anybody knows why ?

Splitting Data in a Column

I get some data that comes in to the table.
This table is currently only displaying after I merged all the tables. So you currently see:
Table 1
union
Table 2
union
Table 3
The issue I have is now i have one column in there where it contains data like this:
AA2B133
I want to split this column, so in the current column it tries to keep the first 3 charachters and the other 4 charachters it goes into another column.
What is the best way or simplest way of doing this.
Thank you
You can use the RIGHT() and LEFT() functions to split the data into multiple columns.
For example:
SELECT LEFT(data,3), RIGHT(data,4)
FROM (SELECT 'AA2B133' AS data) A
Will return two columns with the breakout you requested.
You can use Java to get the 3 first caracters using this
String substring (String s, int start, int len)
so if you want to get the three first charachters for example you can do like this
String substring ("AA2B133",0,2)