I have a table which store DoB of users. Now I need a report(crystal report) that shows how many users are there in ranges of age.
For example, the report need to shows:
Age: 1-10: 50 users
Age: 11-20: 30 users
Age: 21-30: 60 users
And so on...
The report should follow rules below :
The range of age can be changed easily (the range in example above is 10)
The last range is calculated automatically based on the oldest user
Actually I don't have any idea How to do it for now. I really appreciate any help.
Thanks!
You can define the ranges using arithmetic. The exact syntax might vary by database, but it is basically:
select (floor( (a - 1) / 10 ) * 10 + 1) || '-' || (floor( (a - 1) / 10 ) * 10 + 10) as age_range,
count(*)
from t
group by (floor( (a - 1) / 10 ) * 10 + 1) || '-' || (floor( (a - 1) / 10 ) * 10 + 10)
order by min(age);
The expression floor( (a - 1) / 10 ) * 10 + 1 is just a mathematical expression that gets the first year of the range -- so 1-10 are in one group, 11-20, and so on.
Related
Assuming a table with the following format
Team Score
---- -----
A 10
B 20
A 30
B 40
A 50
C 60
I would like to compute statistics, e.g. mean over the "kth" game a given team played, e.g. if k = 1, the mean is (10 + 20 + 60) / 3. How can one accomplish this using big query? Is there a much simpler way for special case k=1 vs. general case?
Consider below - it assumes you have column that represents game number or game date - something that defines game order - in this example I use column named game but you should replace it with your column
select avg(score) avg_score from (
select * from your_table
qualify row_number() over(partition by team order by game) <= 1
)
I have a dataset (in SQL) where I need to calculate the market value over the entire period. I want to automate this calculation in SQL. The Initial value is 2805.00 per month payable for 36 months. But the value is escalated at 5.5% after each block of twelve months. I have included a picture below to show the values and how they escalate. In terms of what fields I have in SQL, I have the length of the term (36 months [months]). I also have an escalation percentage (5.5% in this case [percentage]) and the starting value [starting_value], 2805.00 in this case. The total value (Expected Result [result]) is 106 635.72. I am after what the calculation should look like in SQL so that I can apply it across all market points.
Here is a fast performance formula to calculate the expected result [result] if you need to calculate every, annual salary with respect to the first one:
[result] = (N * (N + 1) / 2) * [increment] * 12 + M * (N + 1) * [increment] + [starting_value] * [months]
Where:
N = TRUNC([months] / 12) - 1// TRUNC --> Integer value of dividing [months] / 12
M = [months] MOD 12 // Rest of integer division of [months] / 12
[increment] = [percentage] * [starting_value] / 100
On the other hand, if you need to calculate each annual salary with respect to its predecessor you will need the formula below:
∑y=0TRUNC([months]/12)−1{([percentage]/100 + 1)y * 12 * [starting_value]} + ([percentage]/100 + 1)TRUNC([months]/12) + 1 * ([months] MOD 12) * [starting_value]
This is a bit confusing but there is no way to place formulas in Stack overflow. Moreover, if you need to run this in some DBMS you should assure someone that allows you to make loops, as the sum will need it.
You would need to adapt both formulas to the DBMS, taking into account the comments I placed before. Hope this to be helpful.
I have a column in my table named as "FundedAmount" and I want to create a new column while running my query on SQL named as "Commissions" and this newly created column needs to have the tier wise percentages summed up according to the equation as follows:
5%(0$ - $5000) + 1%($5001 - $10,000) + 0.5%($10,001 - $20,000) + 0.25%(Remaining Amount)
So, For Instance in the case of the example below the "Commissions" column would have value:
5%(0$ - $5000) + 1%($5001 - $10,000) + 0.5%($10,001 - $20,000) + 0.25%(Remaining Amount)
250 + 50 + 5.515 = 305.515
Fix your tiering table so it looks like:
low high percent
0 5000 0.05
5000 10000 0.01
. . .
Note that I changed the bounds so all amount are included, including fractional amounts.
Then you can calculate this using a subquery like this:
select t.*,
(select sum( (least(tt.high, t.fundedamount) - tt.low) * tt.percent )
from tiers tt
where t.fundedamount > tt.low
) as commission
from t;
I have a table with a column which contains odd and even numbers (ex. 1, 2, 3, 4, 5 etc).
I have to create an output table or view with two columns, odd_numbers and even_numbers, like this:
odd_numbers even_numbers
1 2
3 4
5 6
How do I do that? The condition is easy (where mod (num,2) = 0 or <> 0) but how do I populate the view?
I can't figure it out.
One method uses conditional aggregation:
select min(num), max(num)
from t
group by floor( (num - 1) / 2)
Note that this doesn't use mod(). It basically divides the number by 2 to identify the row (the - 1 is just so the min is odd and the max even).
EDIT:
MS Access (and some other databases) does integer division, so you can just use group by (num - 1) / 2.
If some number can be missing, then:
select max(iif(mod(num, 2) = 1, num, null),
max(iif(mod(num, 2) = 0, num, null)
from t
group by floor( (num - 1) / 2)
This runs in Access (and most other dbs):
SELECT
Value AS Odd,
Value + 1 AS Even
FROM
Table1
WHERE
Value Mod 2 <> 0
Having this table, I would like to find the rows with Val fitting my Indata.
Tol field is a tolerance (varchar), that can be either an integer/float or a percentage value.
Row Val Tol Outdata
1 24 0 A
2 24 5 B
3 24 10 C
4 32 %10 D
5 32 1 E
Indata 30 for example should match rows 3 (24+10=34) and 4 (32-10%=28.8).
Can this be done in mySQL? CREATE FUNCTION?
This is going to be rather difficult to do in MySQL with that table and column design. How do you plan to differentiate what sort of comparison should be done? By doing a string comparison to see if your varchar field contains a percentage sign?
I would suggest breaking your tolerance field into (at least) two int/float columns, say tol and tol_pct. For flexibility, I would represent tol_pct as a decimal (10% => .10). Then, you can do a query that looks like:
select *
from table
where
(Indata between Val - tol and Val + tol)
or (Indata between Val * (1 + tol_pct) and Val * (1 - tol_pct))
I don't have a MySQL install to test it on, but this example is converted from Oracle sql syntax. You have to use string functions to determine if the tol is a percent and act accordingly to calculate the min and max range for that field. Then you can use a between clause.
select *
from (select t.*,
case when substr(tol, 1, 1) = '%' then
t.val * (1 + convert('.' + substr(tol, 2), number))
else
t.val + convert(tol, number)
end maxval,
case when substr(tol, 1, 1) = '%' then
t.val * (1 - convert('.' + substr(tol, 2), number))
else convert(t.val - tol, number)
end minval
from mytable
) t
where 30 between minval and maxval
;
Sure it can be done. But let me tell you that you better review your database design as it conflicts with normalization quite a bit. See http://en.wikipedia.org/wiki/Database_normalization for a quick overview.