I have been searching for a long time how to do this, however due to the words involved when searching it is incredibly hard to find something close to what I am trying to find out!
How can I use the sql CASE expression within a create view?
Could someone please show me the correct syntax?
Below is how mine looks at the moment, but it is not working correctly.
create view vw_price as
select vehicle.price
(case when price between 0 and 999 then ‘0-999’
when price between 1000 and 1999 then ‘1000-1999’
when price between 2000 and 2999 then ‘2000-2999’
when price between 3000 and 3999 then ‘3000-3999’
when price between 4000 and 4999 then ‘4000-4999’
when price between 5000 and 5999 then ‘5000-5999’
when price between 6000 and 6999 then ‘6000-6999’
when price between 7000 and 7999 then ‘7000-7999’
when price between 8000 and 8999 then ‘8000-8999’
when price between 9000 and 9999 then ‘9000-9999’
end) as price_group from vehicle;
The below syntax should work to create the required view. I think you are just missing a comma before the case statement.
USE [<<<database_name>>>]
GO
CREATE VIEW vw_price
AS
select price,
(case
when price between 0 and 999 then '0-999'
when price between 1000 and 1999 then '1000-1999'
when price between 2000 and 2999 then '2000-2999'
when price between 3000 and 3999 then '3000-3999'
when price between 4000 and 4999 then '4000-4999'
when price between 5000 and 5999 then '5000-5999'
when price between 6000 and 6999 then '6000-6999'
when price between 7000 and 7999 then '7000-7999'
when price between 8000 and 8999 then '8000-8999'
when price between 9000 and 9999 then '9000-9999'
end) "price_group" from vehicle;
GO
Related
I'm looking to create a prior rolling 4 quarter Median. Some entries have less than 4 quarters, some have more. I want this by Employee. Needs to account for different tenure for different employees.
Result for 2021-1 should represent the prior 4 quarters median (and not account for current quarter).
I was able to figure out a rolling average with partitioning but not sure how to tackle a rolling median.
Thanks!
Employee ID
Quarter
Sales
EXPECTED RESULT
A
2020-1
1000
NULL
A
2020-2
2000
1000
A
2020-3
3000
1500
A
2020-4
4000
2000
A
2021-1
5000
2500
A
2021-2
4000
3500
B
2020-3
8000
NULL
B
2020-4
7000
8000
B
2021-1
6000
7500
B
2021-2
5000
7000
B
2021-3
1000
6500
C
2021-1
5000
NULL
C
2021-2
0
5000
C
2021-3
4000
2500
I would like to add for a given product_id a price range, in 500 increments. For example a product with the price of 450 should have the price range of 500 and a product with a price 2450 should have the price range of 2000.
Main table
product_id price
32828 2593
23224 456
34344 1000
58283 2420
43585 550
Output table
product_id price price_range
32828 2593 3000
23224 456 500
34344 1000 1000
58283 2420 2000
43585 550 600
you could use case when for manage the range as you prefer
select case when price between 0 and 500 then 500
when price between 501 and 600 then 600
when price between 601 and 1500 then 1000
when price between 1501 and 2500 then 2000
END range
On a form, I have a chart that shows the Increase History of each employee (What rating they were given each year, and how much of an increase they got)
I can't post an image since I have less than 10 reputation, but I hope it's clear.
What I need is a similar graph, but for the history of each employee's salary. I have a StartingSalary field in IncreaseEmployeesQ, and an Increase field. It's complicated, all I've managed to do is this:
SELECT IncreaseEmployeesQ.LocalID, Sum([Increase]+[StartingSalary]) AS CurrentSalary
FROM IncreaseEmployeesQ
GROUP BY IncreaseEmployeesQ.LocalID;
But what this does is add the StartingSalary each time, since it's repeated in each year. and it gives me one value for each employee, instead of one value for each year so I can have a chart that tracks the progress of the employee's salary.
I tried going to the original IncreaesT instead of the query that has it and EmployeesT (IncreaseEmployeesQ), thinking maybe I can have a calculated field if I add a StartingSalary field in IncreaseT (It's originally in EmployeesT) and then link it in relationships and enforce referential integrity, but I kept getting an error message. After some research I gathered that the reason is because the two tables have different Primary keys, so I resorted to the Query.
Is the chart I'm aiming to get to even possible? a chart that shows how each employee's salary has been progressing since 2010? (that's as far back as my data goes)
-assuming that a query is the right way to get this done- The query I'm working on looks like this:
LocalID Increase Years StartingSalary
1 1000 2013 7000
1 500 2014 7000
1 0 2015 7000
1 500 2016 7000
2 0 2013 5000
2 500 2014 5000
2 500 2015 5000
2 0 2016 5000
What I want it to look like (so I make a chart later) is this:
LocalID Increase Years StartingSalary CurrentSalary
1 1000 2013 7000 8000
1 500 2014 7000 8500
1 0 2015 7000 8500
1 500 2016 7000 9000
2 0 2013 5000 5000
2 500 2014 5000 5500
2 500 2015 5000 6000
2 0 2016 5000 6000
If it turns out like this, I can make a chart that has the Years and the CurrentSalary for each employee.
But all I've managed to do is the code above, which gives me this result
LocalID Increase Years StartingSalary CurrentSalary
1 1000 2013 7000 30000
1 500 2014 7000 30000
1 0 2015 7000 30000
1 500 2016 7000 30000
2 0 2013 5000 21000
2 500 2014 5000 21000
2 500 2015 5000 21000
2 0 2016 5000 21000
I hope everything is clear now
You want a cumulative sum. One way to do this in MS Access uses a correlated subquery:
SELECT ieq.*,
(ieq.StartingSalary +
(SELECT SUM(increase)
FROM IncreaseEmployeesQ as ieq2
WHERE ieq2.LocalID = ieq.LocalId AND ieq2.Years <= ieq.Years
)
) as CurrentSalary
FROM IncreaseEmployeesQ as ieq
I need a solution similar to this:
DAX running total (or count) across 2 groups
However slightly more complex.
I have the following:
(apologies for the layout - i can't post pictures)
Name Date Monthly Rev Total Rev Margin( % Rev)
Proj 1 1/08/2014 0 7000 15%
Proj 1 1/09/2014 1000 7000 15%
Proj 1 1/10/2014 1000 7000 15%
Proj 1 1/11/2014 1000 7000 15%
Proj 1 1/12/2014 0 7000 15%
Proj 1 1/01/2015 0 7000 15%
Proj 1 1/02/2015 2000 7000 15%
Proj 1 1/03/2015 2000 7000 15%
Proj 2 1/11/2014 0 16000 10%
Proj 2 1/12/2014 1500 16000 10%
Proj 2 2/12/2014 1500 16000 10%
Proj 2 3/12/2014 1500 16000 10%
Proj 2 4/12/2014 1500 16000 10%
Proj 2 5/12/2014 2000 16000 10%
Proj 2 6/12/2014 2000 16000 10%
Proj 2 7/12/2014 0 16000 10%
Proj 2 8/12/2014 2000 16000 10%
Proj 2 9/12/2014 2000 16000 10%
Proj 2 10/12/2014 2000 16000 10%
Monthly rev is the revenue received in a month, total is the total project value and margin is the percentage of revenue. The table is linked to a dates table by Date.
I need to show margin by date (there are other descriptive columns in the table for slicing) however the margin calc is not straightforward.
In an excel table it would look something like this:
Cumm simple margin | Completion| Cumm complex margin | Margin earnt
0 0% 0 0
150 20% 30 30
300 40% 120 90
450 60% 270 150
450 60% 270 0
450 60% 270 0
750 80% 600 330
1050 100% 1050 450
0 0% 0 0
150 11% 17 17
300 22% 67 50
450 33% 150 83
600 44% 267 117
800 56% 444 178
1000 67% 667 222
1000 67% 667 0
1200 78% 933 267
1400 89% 1244 311
1600 100% 1600 356
Where:
Simple margin is calculated on a cumulative basis as % of monthly Rev
Percentage complete of the project is calculated based on "active" months where revenue is earned
Cumulative simple margin is multiplied by the % complete
Actual margin earned in a particular month is the difference between two months.
Note that Monthly revenue is not necessarily continuous.
No idea how to recreate this in power pivot, any suggestions would be well received.
Cheers
Assuming
That your Project 2 data should run monthly from 1/11/2015 to 1/09/2015 (rather than individual December dates)
You have your data in a table called 'ProjectMargins'
Your DateDim table is called 'Reporting Dates'
Then these are the DAX Measures you need (although there may be simpler methods for achieving these results):
[MonthlyRev]:=SUM(ProjectMargins[Monthly Rev])
[ActiveMonth]:=CALCULATE(COUNTROWS('ProjectMargins'),FILTER('ProjectMargins',[MonthlyRev]>0))
[AllActiveMonths]:=CALCULATE([ActiveMonth],ALL('Reporting Dates'[Date]))
[Completion]:=DIVIDE(CALCULATE([ActiveMonth],FILTER(ALL('Reporting Dates'[Date]),'Reporting Dates'[Date] <= MAX(ProjectMargins[Date]))),[AllActiveMonths])
If you need to calculate TotalRev, from your Monthly Rev, Rather than it appearing in the original source table:
[TotalRev]:=IF(ISBLANK(MAX(ProjectMargins[Margin( % Rev)])),BLANK(),CALCULATE([MonthlyRev],ALL('Reporting Dates'[Date])))
[Rev%]:=MAX(ProjectMargins[Margin( % Rev)])
[Cumm Simple Margin]:=CALCULATE([MonthlyRev]*[Rev%],FILTER(ALL('Reporting Dates'[Date]),'Reporting Dates'[Date] <= MAX(ProjectMargins[Date])))
[Cumm Complex Margin]:=[Completion]*[Cumm Simple Margin]
[Previous Month Cumm Complex]:=CALCULATE([Cumm Complex Margin], DATEADD('Reporting Dates'[Date],-1,MONTH))
[Margin Earnt]:=IF([Cumm Complex Margin]>0,[Cumm Complex Margin]-[Previous Month Cumm Complex],BLANK())
NOTE: This assumes that the margin is never negative.
Ensure that the date field from the DateDim table is used in your pivot, not the date field from the Fact table.
I have a table report of production, and I want to bridge between minus value and positive value on QTY field.
I want to make a new column with positive value, and another column with negative value selected from the QTY field.
mtl_trx qty uom
1 20 1230 KG
2 39 950 KG
3 45 100 LBR
4 91 250 KG
5 118 -500 KG
6 125 -284 KG
7 137 -120 KG
8 143 -80 KG
If I understand correctly, you want to select two columns, one showing the positiv values, one the negative ones? Use a case construct to decide whether to show a value or not.
select mtl_trx, qty, uom,
case when qty > 0 then qty end as qty_pos,
case when qty < 0 then qty end as qty_neg
from mytable;
select mtl_trx, qty, uom,
case when qty > 0 then qty else 0 end as positive,
case when qty < 0 then qty else 0 end as negative
from production;