Subquery in LIMIT function - sql-limit

A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to decimal places.
Hi, Can anyone please tell me why this query won't work?
select round(lat_n, 4) from station
order by 1 asc
limit select floor(count(lat_n)/2) from station, 1;
I'm using a subquery under limit function to return the number (249).

Related

How would I write a program in Python that creates a formatted table of wind chill values?

The National Weather Service computes the wind chill index using the following formula:
35.74 + 0.6215T – 35.75(V0.16) + 0.4275T(V0.16)
where T is the temperature in degrees Fahrenheit, and V is the wind speed in miles per hour. Rows should represent wind speed from 0 to 50 in 5 mph increments, and the columns represent temperatures from -20 to +60 in 10 degree increments.
Create a function windChill(temperature, velocity) which will calculate and return the wind chill for a combination of temperature and wind speed. If the velocity (wind speed) is less than 3 miles per hour, the function should return the provided temperature. If the velocity is greater than or equal to 3 MPH use the formula to calculate and return the wind chill.
Your main() function should use nested loops to create the table, with repeated calls to your windChill() function to determine the value for each wind/temperature combination.
Evaluation:
Use of function to calculate wind chill
Use of nested loops (for or while) to print the rows
Format of table is important
numbers are integers and column aligned
header aligns with values

How to split a Series into time intervals? (python)

I have this dataframe:
And I should split the rows of the ''Time.s'' column into intervals, calculate the average of each interval, and finally the deviation of each average.
I can't split the lines that have Volt.mv > 0.95 into a group for each second. I tried with GroupBy, but it creates problems with the second table:
I used this code, calculating the average directly, but I certainly did something wrong:
ecg.groupby("Time.s").apply(lambda x: x["Volt.mv"].mean())
Can anyone help me?
Before doing the groupby, you need to map Time.s to an interval. Otherwise each group will have only a single row (most of the time).
Here is how to group into intervals of 0.1 seconds and compute the mean and standard deviation for each interval:
interval_length = 0.1
df_aggregated = (
df
.assign(interval=df["Time.s"].div(interval_length).astype("int").mul(interval_length))
.groupby("interval")
.agg(volt_mean=("Volt.mv", "mean"), volt_std=("Volt.mv", "std"))
)

DAX. Problem with subtotals and grand totals

hope you are doing well and can help solve this puzzle in DAX for PowerBI and PowerPivot.
I'm having troubles with my measure in the subtotals and grand totals. My scene is the following:
I have 3 tables (I share a link below with a test file so you can see it and work there :robothappy:):
1) "Data" (where every register is a sold ticket from a bus company);
2) "Km" (where I have every possible track that the bus can do with their respective kilometer). Related to "Data";
3) and a "Calendar". Related to "Data".
In "Data" I have all the tickets sold from a period with their price, the track that the passenger bought and the departure time of that track.
Each track can have more than 1 departure time (we can call it a service) but only have a specific lenght in kilometers (their kilometers are specified in the "Km" table). 
Basically what I need is to calculate the revenue per kilometer for each service in a period (year, month, day).
The calculation should be, basically:
Sum of [Price] (each ticket sold in the period) / Sum of [Km] (of the period considerating the services with their respective kilometers)
I managed to calculate it for the day granularity with the following logic and measures:
Revenue = SUM(Data[Price])
Unique dates = DISTINCTCOUNT(Data[Date])
Revenue/Km = DIVIDE([Revenue]; SUM(Km[Km])*[Unique dates]; 0)
I created [Unique dates] to calculate it because I tried to managed the subtotals of track granularity taking into account that you can have more than 1 day with services within the period. For example:
For "Track 1" we have registered:
1 service on monday (lunes) at 5:00am.
Revenue = $1.140.
Km = 115.
Tickets = 6.
Revenue/Km = 1.140/115 = 9,91.
1 service on tuesday (martes) at 5:00am.
Revenue = $67.
Km = 115.
Tickets = 2.
Revenue/Km = 67/115 = 0,58.
"Subtotal Track 1" should be:
Revenue = 1.140 + 67 = 1.207.
Km = 115 + 115 = 230.
Tickets = 6 + 2 = 8.
Revenue/Km = 1.207/230 = 5,25.
So at that instance someone can think my formula worked, but the problem you can see it when I have more than 1 service per day, for example for Track 3. And also this impact in the grand total of march (marzo).
I understand that the problem is to calculate the correct kilometers for each track in each period. If you check the column "Sum[Km]" is also wrong.
Here is a table (excel file to download - tab "Goal") with the values that should appear: 
[goal] https://drive.google.com/file/d/1PMrc-IUnTz0354Ko6q3ZvkxEcnns1RFM/view?usp=sharing
[pbix sample file] https://drive.google.com/file/d/14NBM9a_Frib55fvL-2ybVMhxGXN5Vkf-/view?usp=sharing
Hope you can understand my problem. If you need more details please let me know.
Thank you very much in advance!!!
Andy.-
Delete "Sum of Km" - you should always write DAX measures instead.
Create a new measure for the km traveled:
Total Km =
SUMX (
SUMMARIZE (
Data,
Data[Track],
Data[Date],
Data[Time],
"Total_km", DISTINCT ( Data[Kilometers Column] )
),
[Total_km]
)
Then, change [Revenue/Km] measure:
Revenue/Km = DIVIDE([Revenue], [Total Km])
Result:
The measure correctly calculates km on both subtotal and total levels.
The way it works:
First, we use SUMMARIZE to group records by trips (where trip is a unique combination of track, date and time). Then, we add a column to the summary that contains km for each trip. Finally, we use SUMX to iterate the summary record by record, and sum up trip distances.
The solution should work, although I would recommend to give more thoughts to the data model design. You need to build a better star schema, or DAX will continue to be challenging. For example, I'd consider adding something like "Trip Id" to each record - it will be much easier to iterate over such ids instead of grouping records all the time. Also, more descriptive names can help make DAX clean (names like km[km] look a bit strange :)

DAX measure to iterate each row for correct division (for total as well)

I am not sure if there's a way in DAX to create a measure that would help me with the following:
Calculate the efficiency by day
Display the total efficiency in a pivot table / PowerBI matrix as the overall total and not as sum of the daily efficiency
Here's a simple example:
Where:
Total Categories = Category1 + Category2 + Category3
Efficiency = (Total Categories + Category4*0.33)/Category4
At first I've created measures for each category (e.g. TotalCateg1 = SUM[Category1] etc.) and hopping to get the right result in the end. My problem is I am not able to get both the daily efficiency and the total right. Is there a way around it?
For Total Categories use this formula:
=SUM([Category 1])+SUM([Category 2])+SUM([Category 3]) .
then for the Efficiency use this formula:
=([Total Categories]+SUM([Category 4])*0.33)/SUM([Category 4])

how Calculate Margin in GP

I want to Calculate profit margin from Dynamics GP Database.
Which fields or table been used. and How Can I do that.
If any one have an idea please share with me.
In general, there are many different ways to calculate gross profit margin. Be sure you are using the method which is accepted by your companies accounting policies.
Here is an example which looks at all invoices which have been posted year to date and calculates the gross profit margin percentage.
Assuming gross profit margin = total profit / total revenue.
SELECT ( SUM(SUBTOTAL) - SUM(EXTDCOST) ) / SUM(SUBTOTAL)
FROM SOP30200 t1
WHERE t1.SOPTYPE = 3
AND t1.DOCDATE BETWEEN '1/1/2013' AND GETDATE()
This will return a decimal number like .44323. In that case you would be making an average gross profit margin of 44% for every invoice year to date.
SOP30200 = posted sales transaction documents

Resources