How to output duplicate row count into a custom field in SAP query - sql

Our company runs on SAP ERP 6.0, SAP_BASIS 702, SAP_ABA 702 (2005), just something to keep in mind in regards to the code syntax.
I have a table LTAP, I want to create a query based on this table, which would produce "N" as number of picks that occured in the past month from the day the report is ran.
I've defined the dynamic date with the following:
YEAR = SY-DATUM(4).
MTH = SY-DATUM+4(2).
DAY = SY-DATUM+6(2).
IF MTH EQ 1.
MTH1 = MTH + 11.
ELSE.
MTH1 = MTH - 1.
ENDIF.
IF MTH eq 1.
YEAR1 = YEAR - 1.
ELSE.
YEAR1 = YEAR.
ENDIF.
FROM_DATE(4) = YEAR1.
FROM_DATE+4(2) = MTH1.
FROM_DATE+6(2) = DAY.
Which seems to do the job.
What I can't seem to get working is to get the count of duplicate lines of field MATNR (each line entry represents a unique pick) and output it to a custom field, while removing the duplicate rows and keeping just the unique values.
Example of what I mean:
I'd like to do this while respecting the date range above and be doable within the sq01/sq02.
How to output duplicate row count into a custom field in SAP query?

Related

Multiple Joins with Multiple Date and ID numbers Omitting ALL Columns When One Column Does Not Exist For a Specific Date/ID

I am pulling data from multiple tables using sub queries. I want all the ID numbers (in this case they are restaurant numbers) with all the dates in the date range (in this case fiscal weeks). I am pulling one column from each sub query, but if ONE column (sub query) does not contain data for a specific week and ID number the entire row is omitted, even if there are data for all the other columns for that specific date/ID.
The closest I have gotten is by using CASE WHEN NOT EXIST. This returns the date and ID and the placeholder data in the statement but it repeats the rows.
select R.restaurantNumber, P.JobCodeKey,
case when not exists (select 1 from FactEmployeePayrollDay P where P.JobCodeKey = 18 and P.RestaurantKey = 190
and BusinessDateKey = 20190205 )
then 77 else p.hours end as hourz,
P.BusinessDateKey
from DimRestaurant R
join FactEmployeePayrollDay P on P.restaurantkey = r.RestaurantKey
join DimJobCode J on J.JobCodeKey = p.JobCodeKey
where R. RestaurantNumber = 195
and P.BusinessDateKey = 20190205
group by r.RestaurantNumber, p.Hours, p.BusinessDateKey, P.JobCodeKey
This is small version of my code that I have been testing different things with. This is the only code I have tried that returns anything. But I am getting repeated data (the placeholder and the date/ID). If I pull that date and ID (restaurant number) just from the FACTEMPLOYEEPAYROLLDAY table there are 27 rows, but my query only returns 26. (I only want one line returned).

Wrapping a range of data

How would I select a rolling/wrapping* set of rows from a table?
I am trying to select a number of records (per type, 2 or 3) for each day, wrapping when I 'run out'.
Eg.
2018-03-15: YyBiz, ZzCo, AaPlace
2018-03-16: BbLocation, CcStreet, DdInc
These are rendered within a SSRS report for Dynamics CRM, so I can do light post-query operations.
Currently I get to:
2018-03-15: YyBiz, ZzCo
2018-03-16: AaPlace, BbLocation, CcStreet
First, getting a number for each record with:
SELECT name, ROW_NUMBER() OVER (PARTITION BY type ORDER BY name) as RN
FROM table
Within SSRS, I then adjust RN to reflect the number of each type I need:
OnPageNum = FLOOR((RN+num_of_type-1)/num_of_type)-1
--Shift RN to be 0-indexed.
Resulting in AaPlace, BbLocation and CcStreet having a PageNum of 0, DdInc of 1, ... YyBiz and ZzCo of 8.
Then using an SSRS Table/Matrix linked to the dataset, I set the row filter to something like:
RowFilter = MOD(DateNum, NumPages(type)) == OnPageNum
Where DateNum is essentially days since epoch, and each page has a separate table and day passed in.
At this point, it is showing only N records of type per page, but if the total number of records of a type isn't a multiple of the number of records per page of that type, there will pages with less records than required.
Is there an easier way to approach this/what's the next step?
*Wrapping such as Wraparound found in videogames, seamless resetting to 0.
To achieve this effect, I found that offsetting the RowNumber by -DateNum*num_of_type (negative for positive ordering), then modulo COUNT(type) would provide the correct "wrap around" effect.
In order to achieve the desired pagination, it then just had to be divided by num_of_type and floor'd, as below:
RowFilter: FLOOR(((RN-DateNum*num_of_type) % count(type))/num_of_type) == 0

SAP BO - how to get 1/0 distinct values per week in each row

the problem I am trying to solve is having a SAP Business Objects query calculate a variable for me because calculating it in a large excel file crashes the process.
I am having a bunch of columns with daily/weekly data. I would like to get a "1" for the first instance of Name/Person/Certain Identificator within a single week and "0" for all the rest.
So for example if item "Glass" was sold 5 times in week 4 in this variable/column first sale will get "1" and next 4 sales will get "0". This will allow me to have the number of distinct items being sold in a particular week.
I am aware there are Count and Count distinct functions in Business Objects, however I would prefer to have this 1/0 system for the entire raw table of data because I am using it as a source for a whole dashboard and there are lots of metrics where distinct will be part/slicer for.
The way I doing it previously is with excel formula: =IF(SUMPRODUCT(($A$2:$A5000=$A2)*($G$2:$G5000=$G2))>1,0,1)
This does the trick and gives a "1" for the first instance of value in column G appearing in certain value range in column A ( column A is the week ) and gives "0" when the same value reappears for the same week value in column A. It will give "1" again when the week value change.
Since it is comparing 2 cells in each row for the entire columns of data as the data gets bigger this tends to crash.
I was so far unable to emulate this in Business Objects and I think I exhausted my abilities and googling.
Could anyone share their two cents on this please?
Assuming you have an object in the query that uniquely identifies a row, you can do this in a couple of simple steps.
Let's assume your query contains the following objects:
Sale ID
Name
Person
Sale Date
Week #
Price
etc.
You want to show a 1 for the first occurrence of each Name/Week #.
Step 1: Create a variable with the following definition. Let's call it [FirstOne]
=Min([Sale ID]) In ([Name];[Week #])
Step 2: In the report block, add a column with the following formula:
=If [FirstOne] = [Sale ID] Then 1 Else 0
This should produce a 1 in the row that represents the first occurrence of Name within a Week #. If you then wanted to show a 1 one the first occurrence of Name/Person/Week #, you could just modify the [FirstOne] variable accordingly:
=Min([Sale ID]) In ([Name];[Person];[Week #])
I think you want logic around row_number():
select t.*,
(case when 1 = row_number() over (partition by name, person, week, identifier
order by ??
)
then 1 else 0
end) as new_indicator
from t;
Note the ??. SQL tables represent unordered sets. There is no "first" row in a table or group of rows, unless a column specifies that ordering. The ?? is for such a column (perhaps a date/time column, perhaps an id).
If you only want one row to be marked, you can put anything there, such as order by (select null) or order by week.

Conditional summing in FileMaker Pro 14

I want to know how do you get conditional sums on FM 14, because I can´t find anything.
I have X number of records in a layout. Each record has Price, Name and Month. I want to create another layout in which I will have a table with the months arranged horizontally and Name arranged Vertically. Like this:
January February March April .... .... .... December
Name 1 (calc)
Name 2 (calc)
Name 3 (calc)
....
....
....
Name X
(calc) = Calculation
I want to sum every price on each record that has the Name and Month specified in each calculation.
I can´t find the way to do it and it´s driving me crazy.
Thanks
Create 12 calculation fields with formula:
If( myTable::Month = 'January'; myTable::Price ; "" )
(repeat for every month)
Create a list layout based on the same table, with header and footer
Add sub-summary when sorted by Name
Add to sub-summary Name field
Create a summary fields for every earlier created calculation fields based on Sum()
Add these summary fields to sub-summary layout part
Delete Body layout part
Sort records by Name
Filemaker is not very good at cross-tab reports, Still, here's one way to look at it:
Define a calculation field cSplitByMonth (result is Number, Number of repetitions: 12) =
Let ( [
monthNames = "January¶February¶March¶April¶May¶June¶July¶August¶September¶October¶November¶December" ;
monthName = GetValue ( monthNames ; Get ( CalculationRepetitionNumber ) )
] ;
If ( Extend ( Month ) = monthName ; Extend ( Price ) )
)
Define a summary field as Total of cSplitByMonth, summarize repetitions individually.
Create a layout with a sub-summary part (when sorted by name) and no body part. Place the Name field and the summary field (with all its 12 repetitions, oriented horizontally) in this part.
Sort the records by Name.
Another approach can be seen here: http://fmforums.com/topic/71836-getting-more-out-of-filtered-portals-in-version-11/#comment-339728

Qlikview calculation of range for frequencies

I am given a task to calculate the frequency of calls across a territory. If the rep called a physician regarding the sale of the product 5 times, then frequency is 5 and HCP count is 1....I generated frequencies from 1 to 124 in my pivot table using a calculated dimension which is working fine. But my concern is :
My manager wants frequencies till 19 in order from 1..2..3..4...5..6.....19...
And from the frequency 21-124 as 20+.
I would be grateful if someone helps me with this.....Eager for the reply....
Use the Class function in the dimension, to split into buckets:
=class(CallId,5)
And the expression:
=count(Distinct CallId)
You can then customize the output by adding parameters:
class( var,10 ) with var = 23 returns '20<=x<30'
class( var,5,'value' ) with var = 23 returns '20<= value <25'
class( var,10,'x',5 ) with var = 23 returns '15<=x<25'
I think you can do this with a calculated dimension.
If your data has one row per physician coming from the load statement below will likely work.
Dimension
- =IF(CallCount<=19,CallCount,'+20')
Expression
- =COUNT(DISTINCT Physician_ID)
Sort
- Numeric Value Ascending
If your data has to be aggregated, more than one call row per provider incoming from the load try above substituting below for the Dimension.
Dimension
- =IF(AGGR(SUM(CallCount), Physician_ID) <=19,AGGR(SUM(CallCount), Physician_ID),'+20')