How to search value range inside another range in sql - sql

I have a form where i enter min price and max price when creating product details. In the product search i have also 2 fields called min and max. So how can i get the result of given range considering the range given when inserting products
Products
min max
10 15
15 30
20 30
In the search form i insert min as 5 and max as 16. Which products i will get in the result and whats the best theory for searching considering practical situations.

Compare opposite ends of each range to find products in the overlap:
select * from products
where min < $max and max > $min
This approach works well for date ranges too.

select * from products where min>=5 and max<=16
so based on this you will get
Row -> 10, 15

The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.
SQL BETWEEN Operator tutorial
For example:
SELECT * FROM Product
WHERE price
BETWEEN $min AND $max

Related

MAX funtion in SQL

I need to query a table to extract maximum values of purchases in any month. However, if there are 2 months with both having the maximum number of purchases, the query should return both. MAX function only returns one of them. How do I pull both or more if available
You can GROUP BY by the month(Do not know the exact names of your columns so I wild guessed):
select max(purchases), month
from db.table
group by month
So you will have something like this(EXAMPLE DATA).
max(purchases) month
100 1
200 2
150 3
150 4
etc.

MDX Calculation Tab

I am new to SSAS and I am trying to write a query in ssas caluculation tab that will show all customers (customername) and items ordered (ItemName) in last 40 days. I am not sure is calculation tab the best option to do this.
I have got only one measure VOLUME and dimension attributes (customername,itemname,officename,shipdate ...) I have got also Date Hierarchy with Year->Q->Month->Date.
I have got this statement that gives me netvolume for specific itemname in last 40 days, but what i am looking for is all customers and items ordered in last 40 days. I do not need measure. ( this 40 days might change to 20 or 60 days)
SELECT
{ [Measures].[NET VOLUME]} ON COLUMNS,
FILTER
(
{[CUBENAME].[SHIPDATE].&[2018-03-23T00:00:00]:[CUBENAME].[SHIPDATE].&[2018-05-01T00:00:00]},
[CUBENAME].[ITEMNAME].&[11# RPC 6411]
) ON ROWS
FROM[CUBENAME]
Try using named query in the DSV and filter based on the date field.

Price Calculating formula in SQL

I have been using the following formula in excel but now I a trying with sql:
the formula:
Price field=(ROUND(Cost field/Constant number,1))-0.01
If the cost is 1.43 the price outcome would be according to this formula would be 2.59.
How can I write this formula is sql?
You can use a simple select and round
assuming your column are name Cost field and Constant number
select ROUND(`Cost field`/`Constant number`,2)-0.01 as price
from my_table
or using as Constant number = 30
select ROUND(`Cost field`/30,2)-0.01 as price
from my_table
and as suggestd by Philipp be sure for the proper rounding take a look at https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_round

DAX formula for calculate Sum between 2 dates

I have a couple of tables in PowerPivot:
A Stock table - WKRelStrength whose fields are:
Ticker, Date, StockvsMarket% (values are percentages), RS+- (values can be 0 or 1)
A Calendar Table - Cal with a Date field.
There is a many to one relationship between the tables.
I am trying to aggregate RS+-against each row for dates between 3 months ago to the date for that row - i.e a 3 month to date sum. I have tried numerous calculations but the best I can return is an circular reference error. Here is my formula:
=calculate(sum([RS+-]),DATESINPERIOD(Cal[Date],LASTDATE(Cal[Date]),-3,Month))
Here is the xlsx file.
I couldn't download the file but what you are after is what Rob Collie calls the 'Greatest Formula in the World' (GFITW). This is untested but try:
= CALCULATE (
SUM ( WKRelStrength[RS+-] ),
FILTER (
ALL ( Cal ),
Cal[Date] <= MAX ( Cal[Date] )
&& Cal[Date]
>= MAX ( Cal[Date] ) - 90
) )
Note, this will give you the previous 90 days which is approx 3 months, getting exactly the prior 3 calendar months may be possible but arguably is less optimal as you are going to be comparing slightly different lengths of time (personal choice I guess).
Also, this will behave 'strangely' if you have a total in that it will use the last date in your selection.
First of all, the formula that you are using is designed to work as a Measure. This may not work well for a Calculated Column. Secondly, it is better to do such aggregations as a Measure level, than at individual records.
Then again, I do not fully understand your situation, but if it is absolutely important for you to do this at a Record level, you may want to use the "Earlier" Function.
If you want to filter a function, based on a value in the correspontinf row, you just have to wrap your Column name with the Earlier Function. Try changing the LastDate to Earlier in your formula.

SSRS How to Compare Columns to First Column in Group

I'm trying to create what seems like should be a pretty simple matrix report and I'm hoping someone can help. I have dataset that returns sales region, Date, and sales amount. The requirement is to compare sales for the various time periods to the current date. I'm looking to get my matrix to look something like this:
CurrentSales Date2Sales CurrentVSDate2 Date3Sales CurrentVSDate3
1 1000 1500 -500 800 200
2 1200 1000 200 900 300
3 1500 1100 400 1400 100
I can get the difference from one column to the next, but I need all columns to reference the CurrentSales column. Any help would be greatly appreciated.
Currently my data set is pulling in a date, region, product and sales amount. I then have three parameters, CurrentDate, PreviousMonth, PreviousQuarter. The regions and products are my row groups and the dates are the column groups. Next I added a column inside the group with the following expression: =Sum(Fields!SalesAmount.Value)-Previous(Sum(Fields!SalesAmount.Value),"BookingDate"). I know this isn't correct because it compares the values to the previous date in the column group and I need the comparision to be to the First date in the column group.
Example:
Using Expressions you can:
=iif(Sum(Fields!SalesAmount.Value)= Previous(Sum(Fields!Date2Sales.Value)),
=iif(Sum(Fields!EndBalance.Value)=0, Nothing, Sum(Fields!EndBalance.Value)) You can also use Switch.
The easiest way to get this result would probably be in your query. Add a field to every row returned maybe called "Current Sales." Use a correlated subquery there to get the right value for comparison. Then your comparison can be as simple as =Fields!Sales.Value - Fields!CurrentSales.Value or similar.
There are some ways to do this at the report level, but they are more of a pain: my current favorite of those is to use custom code embedded in the report. Another approach is to use Aggregates of aggregates.