top n values (row-wise) in a table in presto - max

I want to find the max n values of each row (excluding the table key and time stamp) of a table in presto. For example, I have the daily sales of a shop in different category, I want to find the top 3 sale amounts on each day and their category (which would be the column name women's apparel, kids, toy, etc). The table has 100+ columns (different sale categories). Is there a better way than to use greatest() function and writing out all column names?

Related

D365/Dataverse - Create Calculated/Look Up Column that is set to the highest date in another table

I have Table 1. It is filled with dates a inspection is going on. Plumbing or Garden inspections for example.
Table 2 links to these appointments and has additional columns with details such as a Person assigned to the inspection, and what property the inspection is at. I need these two tables to be separate as described, and they are linked by a simple ID column.
Is it possible at all to add a column to Table 2 called 'Last Date of Plumbing Inspection'. The idea is for any given Property in Table 2, there can be multiple inspection entries in Table 1 for it. The point of this column is that it should look in Table 1, find the matching ID, find the latest inspection date out of all the Plumbing-related inspections, and then set the column value to that.
The problem I am having with this is it seems like calculated columns can ONLY implement logic using the columns of the table the calculated column was created in. In Table 2, I can't create a calculated column that interacts with Table 1 at all. I could create a look up column, but I can't combine calculated columns with look up columns. Is there a way to build this latest inspection date column without too much complexity?
Actually you can create a Rollup field and put a MAX aggregate function for achieving your requirement from related table. Read more

Custom function by date for related tables

"In a crosstab, latest month where Actuals has any data, show Actuals data for that and all previous months. Future months, show Forecast data."
.
I have two tables- Forecasts and Actuals- and the common columns between them are Team, Month, Value.
I'd like to show the data in a crosstab with Month as columns and Team as rows. I'm trying to write an expression to do this in the crosstab: The most recent month where Actuals has any data, I'd like to show Actuals data for that and all previous months, for all teams. For following months, I'd like to show Forecast data.
Any suggestions about how to go about this would be appreciated. I'm still piecing together my knowledge :)
Create a third table from a transformations:
Create Third table from - Pivot date on Team & Month (to ensure every possible combination) from first table
Add Rows from transforming the second table (pivot on Team & Month)
Join the two original tables to your newly created table (has every possible combination of Team & Month) so that both your data sets are now in one table.
Now use the third table in your cross table.
If you try using column matching instead of the above method only dates from the main table will show as the dates are matched and ones missing from the other will not display.

Unreliable results of a query

I need to get sales figures from open orders, sorted by code. The items are separated in the stock table by lot number (for traceability reasons) but the lot numbers do not appear in the orders table. The only link between the 2 tables is the part number.
When my query
SELECT code, SUM(qty*price) AS Sales
FROM orders INNER JOIN stock ON orders.partno = stock.partno
GROUP BY code
started returning strange results (very high sales figures for a given code), I changed it to
SELECT DISITNCT orders.partno, stock.lot, stock.code
FROM orders INNER JOIN stock ON orders.partno = stock.partno
and noticed that if several lots of a given part are in stock they are all returned
Part1 LotA code
Part1 LotB code
Part1 LotC code
which means that if a customer orders 300 units of Part1, my query returns 900 and my sales figure is multiplied by 3.
How can I work around that?
It must be noted that I do not work from a database but from a group of tables, the structures of which can sometimes be whimsical.
You should really use table.column or alias.column reference when writing queries. As your question stands, we do not know which table the PRICE comes from... the parts table or the lots table. If you are dealing with inventory tracking such as FIFO or LIFO method accounting, you must have an association to the lot table for inventory being tracked/sold.
Now, why are you getting large numbers? That is because of a Cartesian result. If you are not familiar with that, for each record in one table joined to another, it is returning however many matches.
So, if you have an order of one line item, there is only one line item in a products available table. So this is simple 1:1 ratio. Now, you have your STOCK table that can have multiple records for the exact same part number. You are now returning the same original order line item for EACH LOT ENTRY in the Stock table. So now, for your 1 item, you are getting 3 lots (1:3 result).
I know this is important from a cost-of-goods sold basis, hence your need to know which "lot" it is joined to so you only get that one specific record for proper pricing.
If however, you do have a generic product table of everything you sell, and that table has a generic common price no matter which "lot" was used for the sale, I would join to that table instead for your report. But you will still have the accounting issue of inventory, cost-of-goods, etc.

Calculated Item in OBIEE - Moving Average

I have consulted this question - OBIEE Moving Average (Mavg) for 4 weeks on Pivot Table - in order to understand how to do a moving average in OBIEE. However, I am having trouble calculating that item in a pivot table.
In the pivot table view I thought I would just select New Calculated Item and create my moving average function for the pivoted values. Yet, in the "Values From" drop down menu the items I want to average are not present. I believe this may be because they are not extracted data values but previously calculated values from the data (in the original table's columns). I tried selecting "treat as attribute column" but this failed as well. How can I created a moving average within the pivot table for a column that was calculated from the original data?
Looks similar to this:
Pivot Table
ID Value
01 45
02 54
03 65
... ...
Where Value is Amount Sold / Days. And both Amount Sold and Days are stored in the original data table.
Go to the criteria tab and pull in a measure (any measure will do). Now edit that measure's column formula to your formula of Amount Sold / Days. I would rename it as well.
When you click over to the results tab, you will see that column added to all views. Just edit each view and remove the column from the views you do not want to see it in. You can also replace your existing value column in your pivot table (if it was present) with this new calculated column.
Pivot table calculated items are typically used in conjunction with columns that are already in the pivot table.

Sort Report table by totals

I have built a report with a table. It looks very much like the table in the following link (last figure, before "Next Steps").
Microsoft Tutorial: Adding grouping and totals
I have turnover values for products which are grouped by subcategories and categories and have a total for subcategories and categories. I want to sort the table by turnover (in the example it is "line total"). I can do that for the single product values in a subcategory, but not for the totals. Is that possible?
For all people interested: the row groups can be sorted by themselves (in Row Group - Group Properties). As the sort expression, one has just to choose SUM(Fields.LineTotal.Value).

Resources