Writing a function in PostgreSQL - postgresql

There's the following task:
To prices table (see the picture) add a field to store the date of the price validity. Fill this field with 01.02.2022. Write a function to insert information about new price and validity date for a given category.
Inputs: category, validity date. Increase prices for all ticket categories by 10% from today.enter image description here
I did the first part of the task:
alter table prices
add price_validity date not null
default ('01.02.2022')
But I can't cope with the second one, as I'm not familiar with functions in SQL. How to write such a function that will increase price for some category by 10% and change the validity date to the one you entered?

Related

Tableau measure count items if between dates

What I am trying to achieve is to get a count of people employed in a particular period.
I have 3 variables:
Employee ID (integer)
Hire date (date)
Termination date (date or null)
Example
the formula I am looking for is something like
if termination_date is null
then
count employee_ID in
dates between Hire_date and max of either hire_date or termination_date
else
count employee ID in
dates between hire_date and termination_date
This aims to show the dynamic of staff level over the time.
I am new to Tableau, not sure how to even start with it. Any suggestions welcome.
This problem will be simpler if you reshape your data to have the following three columns
Employee ID
Date
Action. (where action takes on the values of ‘Hire’ or ‘Terminate’).
Each data row represents one change in status for an employees. If an employee had a termination date, they will have two records in this new format, otherwise just one record showing the hiring date.
You can reshape your data by hand, or leave the original and use Tableau Prep or the Tableau data source page to reshape using a self Union and a few simple calculated fields.
Define a calculated field called Staffing_Change as
if Action=‘Hire’ then 1 else -1 end
Now you can plot the change in staff level over time by putting exact date on columns and sum(Staffing_Change) on Rows. You can use a quick Table calc, Running Sum, to see the net staffing level. For line mark types, I’d use a step style by pressing on the path button on the Marks card. Otherwise, the chart can give the impression of fractional number of employees.

level of detail expressions cannot contain table calculations or the attr function in Tableau

i have this tableau workbook
basically this calculated day different between each user_id and each transaction for each user_id with this calculation
DATEDIFF('day',LOOKUP(MIN([Created At]),-1), MIN([Created At]))
that pull filters its so filter the conditions of users (We can ignore this)
and date_rante filters its for calculated day different between date range on parameter
with this calculated
lookup(min(([Created At])),0) >= [START_DATE] and
lookup(min(([Created At])),0) <= [END_DATE]
so from the frequency i want to find out the Max of different day, with this calculated
MAX({FIXED [User Id]:DATEDIFF('day',LOOKUP(MIN([Created At]),-1), MIN([Created At]))})
but it says
level of detail expressions cannot contain table calculations or the attr function
so i used this solution https://kb.tableau.com/articles/howto/finding-the-dimension-member-with-the-highest-measure-value
and from that solution, i applied with my codes into like this
MAX({FIXED [User Id]:DATEDIFF('day',INT(LOOKUP(MIN([Created At]),-1)), INT(MIN([Created At])))})
but it turns to error datediff being called with string,integer,integer
based on #Anil solution, i tried to create it, and idk why the results was like this
new picture
Presently, as far as my knowledge of tableau is, tableau doesn't allow to calculate LOD calcs or further aggregations on table calcs. To find the transactions where the user took most/max time (in days) in subsequent order- You can do this workaround..
Let's assume your datediff calc field is named as CF1. create another calc field lets say CF2 with following calculation
rank_unique([CF1])
EDIT:
Change table calcs on this field similar to CF1. putting a filter on this field will give you the dates with max(time diff) as shown in screenshot.
table calculation options on first (datediff field)
table calculation options on second field (rank_unique)
I have added third field on colors
(Please note no field used in filters just to highlight)

how to put condition users on tableau

im mysql user, i have data source like this
User_id | Order_id | Status_id | createdAt | Transaction_Amount
user_id as the user, order_id as the id of the order, status_id as the status of each order, createdAt as the date of the transaction .
in MySQL, i divide the condition of the users in 4 conditions.
conditions 1 new_user
the user who doing FIRST transaction in date range, and not doing transaction before the date range
conditions 2 repeat_user
the user who doing transaction before the end of date range, and doing atleast 1 more transactions in date range
conditions 3 existing_user
the user who doing his first transaction before the date range, and doing atleast 1 more transactions in date range
conditions 4 unique_user
the user who doing transaction in date range
i've done it with the queries in mysql, but i want to visualize it in tableau, i already make the filter of the time range, but how to make the conditions to based on condition user
Assuming that you want to have create these conditions in tableau, I propose a solution like this. Since you've not provided any data to work upon, a data I used in excel is as
View-1 add a Calculated field(CF) First Transaction of User as
{FIXED [user_id]: MIN([CreatedAt])}
Drag this field on filter instead of created at and you'll get a view based on your condition_1. Screenshot
View-2 (I am assuming that both transactions may have been completed within the filtered date range. If you need otherwise please specify).
step-1. drag user_id to rows, createdAt at filters (select range of dates).
Step-2 Drag order_id to filters again select count distinct, select at least 2
You'll get a view like this
View-3 Add two CFs as. 1st condition 1 as
Min([First Transaction of User])< min([CreatedAt])
another CF condition 3 check2 as
{Fixed [user_id]: COUNTD([order_id])}>1
Add both these CFs on filter shelf along with createdAt. Add condition-1 check2 to context (important step) select TRUE from both filters and you'll get desired view. like this
View-4 It is the most basic chart in tableau. Try and I am sure you can do it. Good luck
From your workbook it is clear that you want cohort analysis. In this regard Let me explain a few things..
First create a user cohort like this
{FIXED [user_id]: MIN([CreatedAt])}
Output of this field will be date when user first ordered. So if you will add this field to filters the output will be users (added in view) who first ordered within the filtered date/range.
Now if you want to see the users' orders during the same period (condition 1) also add the createdAt field to filters with same date/range.
E.g. suppose your date range is 5jan to 15 jan. Then set both filters to these dates range. First filter will ensure to filter customer with their first transaction and second one will give you desired output.
For condition2, you'll have to add countd(order_ids) to filters with filter value set 'at least 2'.
E.g. setting this filter will ensure that customer did their first and at least one more transaction during the date range.
For condition3, changing dates in filter accordingly will do. Nothing extra to be done.
E.g. suppose you want the customers with second and onward transaction between 5 jan to 15 jan. Then you will have to set first cohort filter with date upto 4 jan but second date filter to 5 jan to 15 jan
Condition 4: remove cohort from filters. Set date range on createdAt field only.
If you want to add another filters like status etc., These will work after you add them to filters.
Try this

Tableau Target Vs. Actual - Can not get totals to show correctly

I am having trouble showing the correct totals in my tableau worksheet.
I have supervisors that are part of specific zones that need to complete a certain number of tests in different categories. For example, supervisor 15716 must complete 8 tests in category 1. I need to show the target, which is a number stored in the database and show the actual number of tests in that category that have been completed within a date range. I have it working, but Im not sure if I did it correctly because I can not show any totals.
System target - number stored in database
CountOfSheetID - calculated field
Percent Compliant - calculated field
Try this approach -
First define a calculated field called [Within Date Range?] as
[Date] >= [MyStartDate] AND [Date] <= [LastSelectedDayOfMonth]
and put that new field on the filter shelf, only including data where [Within Date Range?] is True. (You could also just filter the [Date] field if that is flexible enough for you)
The you don't need the CountofSheetId calculated field at all. If you want to know how many records have a non-null value for [SheetID] within your date range, you can simply drop [SheetID] on a shelf and choose to treat it as Measure with the aggregation function COUNT()
Then just build your visualization to show the counts you want (not percentages, the actual counts)
Finally, you can convert counts into Percentages by clicking on the pills for your Measures and choosing Percentage under Quick Table Calcs. You'll want to experiment with the "Compute Using" setting to tell Tableau how to compute your percentages -- i.e. define percentage of "what".
Percentages are implemented as table calcs in Tableau. Read the help to understand table calcs, especially the description of partitioning and addressing.

Can you send a full result set to an SQL function?

I am working in Postgres and I need to send in a full result set with many rows and column into a stored procedure or a function. Is this possible? If so, where can I see resources for syntax?
OK this is how I have it set up without being able to send in a result set, it forces me to break out comparison logic and put it in two different spots, however my goal is to keep the actual finding the promotion logic in one place, which I have done here. This may change one day, the comparison logic is less likely to change, it is pretty standard.
Promotion Line Item Logic
-There will be triggers set on INSERT for the promo_objects, promo_buy_objects, and promo_get_objects tables, there will be an UPDATE trigger on the promo table.
-The trigger for the xrefs will call a stored procedure called set_best_product_promos that will decide which promotion is best for that object and it will then save to a new table:
promo_best_product_promos
promo_id,
object_id,
expiration_date
-The trigger for promo will call update_best_product_promos and will send in the promo_id and if active = true it will update the expiration date for that promo else it will delete all entries for that promo
The new table has been added to the promo.sql script, however the triggers and function can not be added until the function is written.
A script will run at midnight every night to delete the entries that have expired.
PSEUDO FOR cart code (application code)
Run the union query just as we are now shown_object_promotions (this gets all available promotions for the item)
Loop through results
if buy_quantity > 0
IF the quantity of the buy item in the cart is greater than or = the buy_quantity (I think c.active_items is the items in the cart)
IF get_quantity > 0
If the get item is in the cart AND it is the item sent into this function (I think c.active_items is the items in the cart)
run the get_best_product_promos function
run comparison logic
else
run the get_best_product_promos function
run comparison logic
EDIT: So I guess I could dump this cart logic as a stored procedure as well, and then make one for the comparison logic, and boom its all in stored procedures and portable and generic?
PSEUDO FOR set_best_product_promos:
-You will send in the object_id and promo_id
-You will declare all of your variables
-Go ahead an query the end date of the promo
-You will then query the promo_best_product_promos table to see if an entry exists for this product
IF exists:
RUN YOUR UNION QUERY accept this time you will have to explicitly say all the fields you want and what variables to select them into
Then loop through your query
LOOP
run get_best_product_promos
run comparison logic
END LOOP
Now take those variables you set in the crazy logic and update promo_best_product_promos
ELSE:
insert the object_id, promo_id, and end date (expiration_date) into the promo_best_product_promos table
PSEUDO FOR get_best_product_promos:
If no buy and no get quantities
If discount type = percent
calculate value of the promotion for this item to compare later
calculate the new price for the product and update the estimated unit price
If discount type = dollar
calculate value of the promotion for this item to compare later
calculate the new price for the product and update the estimated unit price
If discount type = price
calculate value of the promotion for this item to compare later
calculate the new price for the product and update the estimated unit price
If discount amount = Free
do nothing
pass
If buy quantity but no get quantity
If discount type = percent
calculate value of the promotion for this item to compare later
If discount type = dollar
calculate value of the promotion for this item to compare later
If discount type = price
calculate value of the promotion for this item to compare later
If discount amount = Free
do nothing
pass
Else (assumes there is both buy and get)
IF the quantity of the buy item in the cart is >= the buy_quantity (I think c.active_items is the items in the cart)
If discount type = percent
calculate value of the promotion for this item to compare later
If discount type = dollar
calculate value of the promotion for this item to compare later
If discount type = price
calculate value of the promotion for this item to compare later
If discount amount = Free
#Use a different var here like in select_cart_promotion - they will always get this promotion
calculate the value of the promotion for these items
do something here to ensure the get product is in the cart
Take a look at cursors.
Postgres user defined functions can be written in many languages
On the formats of input and output parameters for PL/pgSQL you can check the documentation here
Are you sure that you need to pass this to a function? I believe you could structure your functions to avoid this, functions can return tables and get get tables inside of them. If this table of yours is a query/table/view then you can use SQL inside the function to get to it (passing only parameters of other data type); if this table is the result of another function you can call the function to get to the table. What's your scenario?