Column GrossCommission displays value 368.760000 but I need it to be 368.75.
Column NetDueCarrier displays value 31.240000 but I need it to be 31.25
Below is the calculation for those values:
SELECT
policyNumber,
CompanyCommission,
CASE WHEN [GrossPremium] < 0 THEN [BondAmount] * (-1) ELSE [BondAmount] END as [NetPenalLiability],
GrossPremium as WrittenPremium,
PayableFees,
(GrossPremium * (CompanyCommissionActual /100)) as GrossCommission,
BillDate,
PolicyType,
((GrossPremium - (GrossPremium * (CompanyCommissionActual / 100))) + PayableFees) as NetDueCarrier
If you need the value to the nearest 5 cent, then you could use something like this:
create table t (val decimal(9,4));
insert into t values
(31.20),(31.21),(31.22),(31.23),(31.24)
,(31.25),(31.26),(31.27),(31.28),(31.29);
select
val
, Nearest5 = round(val/.05,0)*.05
-- , Nearest5 = round(val*20,0)/20 -- alternate way of writing the expression
from t
rextester demo: http://rextester.com/UUNA22566
returns:
+---------+----------+
| val | Nearest5 |
+-------+----------+
| 31.20 | 31.20 |
| 31.21 | 31.20 |
| 31.22 | 31.20 |
| 31.23 | 31.25 |
| 31.24 | 31.25 |
| 31.25 | 31.25 |
| 31.26 | 31.25 |
| 31.27 | 31.25 |
| 31.28 | 31.30 |
| 31.29 | 31.30 |
+-------+----------+
You could simply wrap the calculation in a ROUND function:
ROUND(GrossPremium * (CompanyCommissionActual /100), 2) as GrossCommission,
...
ROUND((GrossPremium - (GrossPremium * (CompanyCommissionActual / 100))) + PayableFees, 2) as NetDueCarrier
More info here. This is assuming you're using SQL Server with a version >= 2008.
Related
I am doing a Snowflake query where I get RANK from each column.
Output below:
| Rank_A | Rank B |
| -------- | -------- |
| 1 | 3 |
| 2 | 4 |
| 5 | 4 |
Then I do the LEAST function to get the Minimum across those columns
https://docs.snowflake.com/en/sql-reference/functions/least.html
Output below:
| Rank_A | Rank B | LEAST |
| -------- | -------- | -------- |
| 1 | 3 | 1 |
| 2 | 4 | 2 |
| 5 | 4 | 4 |
Lastly, (This is where I am not sure of) How can I make another column that returns what column the Least function came from - so what I am trying to return in this new column is like so:
Expected output below:
| Rank_A | Rank B | LEAST | Column Name of Least Value |
| -------- | -------- | -------- | -------------------------- |
| 1 | 3 | 1 | Rank_A |
| 2 | 4 | 2 | Rank_A |
| 5 | 4 | 4 | Rank_B |
I have no idea how I would go about this? any and all help would be greatly appreciated.
I do not know where to begin. I was contemplating doing this all in python before ingesting the data table?
You could use DECODE to get the first LEAST value column name:
SELECT *
,LEAST(Rank_A, Rank_B)
,DECODE(LEAST(Rank_A, Rank_B),
Rank_A, 'Rank_A',
Rank_B, 'Rank_B') AS column_name
FROM tab;
Output:
We can use a CASE expression along with the LEAST() function:
SELECT
RANK_A,
RANK_B,
LEAST(Rank_A, Rank_B) AS LEAST,
CASE WHEN RANK_A = LEAST(Rank_A, Rank_B)
THEN 'RANK_A' ELSE 'RANK_B' END AS LEAST_VALUE
FROM yourTable;
In the event that RANK_A and RANK_B have the same value, the above query would arbitrarily return RANK_A as the column with the least value.
I have a table with a number of variables such as:
+-----------+------------+---------+-----------+--------+
| DateFrom | DateTo | Price | Discount | Cost |
+-----------+------------+---------+-----------+--------+
| 01jan17 | 01jul17 | 17 | 4 | 5 |
| 01aug17 | 01feb18 | 15 | 1 | 3 |
| 01mar18 | 01dec18 | 12 | 2 | 1 |
| ... | ... | ... | ... | ... |
+-----------+------------+---------+-----------+--------+
However I want to split this so I have:
+------------+------------+----------+-------------+---------+-------------+------------+----------+-------------+-------------+
| DateFrom1 | DateTo1 | Price1 | Discount1 | Cost1 | DateFrom2 | DateTo2 | Price2 | Discount2 | Cost2 ... |
+------------+------------+----------+-------------+---------+-------------+------------+----------+-------------+-------------+
| 01jan17 | 01jul17 | 17 | 4 | 5 | 01aug17 | 01feb18 | 15 | 1 | 3 |
+------------+------------+----------+-------------+---------+-------------+------------+----------+-------------+-------------+
There's a cool (not at all obvious) solution using proc summary and the idgroup statement that only takes a few lines of code. This runs in memory and you're likely to come into problems if the dataset is large, otherwise this works very well.
Note that out[3] relates to the number of rows in the source data. You could easily make this dynamic by adding a prior step that calculates the number of rows and stores it in a macro variable.
/* create initial dataset */
data have;
input (DateFrom DateTo) (:date7.) Price Discount Cost;
format DateFrom DateTo date7.;
datalines;
01jan17 01jul17 17 4 5
01aug17 01feb18 15 1 3
01mar18 01dec18 12 2 1
;
run;
/* transform data into 1 row */
proc summary data=have nway;
output out=want (drop=_:)
idgroup(out[3] (_all_)=) / autoname;
run;
I have a list of locations in a table. The first position begin in '2' and finish in '8', the second position begin in letter 'A' and finish in 'P' . Finally every location has seven elements like this example:
SELECT Location FROM WAREHOUSE_LOCATIONS
Location |
---------|
2A-1 |
2A-2 |
2A-3 |
2A-4 |
2A-5 |
2A-6 |
2A-7 |
2B-1 |
2B-2 |
2B-3 |
2B-4 |
2B-5 |
2B-6 |
2B-7 |
2C-1 |
...
3A-1 |
...
4A-1 |
...
etc...
I want to order this locations like this:
Location |
---------|
2A-1 |
2B-1 |
2C-1 |
2D-1 |
....
2P-1 |
2A-2 |
2B-2 |
2C-2 |
...
2P-2 |
2A-3 |
2B-3 |
2C-3 |
How can i do that?
You could do:
ORDER BY CAST(RIGHT(Location, LEN(Location) - CHARINDEX('-', Location)) AS INT),
LEFT(Location, CHARINDEX('-', Location) - 1)
I have this table of stage-wise activities and their corresponding flags.
STAGE | ACTIVITY | FLAG | STATUS |
S1 | A1 |S1_A1_FLAG | ST_S1_A1 |
S1 | A2 |S1_A2_FLAG | ST_S1_A2 |
: : : :
SN | A1 |SN_A1_FLAG | ST_SN_A1 |
SN | A2 |SN_A2_FLAG | ST_SN_A2 |
: | : | : | : |
SN | AN |SN_AN_FLAG | ST_SN_AN |
I have to create a view that will have following structure...
STAGE| A1 | A2 | ... | AN |
---------------------------------------------------
S1 |S1_A1_FLAG| S1_A2_FLAG | ... | S1_AN_FLAG |
| ST_S1_A1 | ST_S1_A2 | ... | ST_S1_AN |
----------------------------------------------------
S2 |S2_A1_FLAG| S2_A2_FLAG | ... | S2_AN_FLAG |
| ST_S2_A1 | ST_S2_A2 | ... | ST_S2_AN |
----------------------------------------------------
: : : : :
----------------------------------------------------
SN |SN_A1_FLAG| SN_A2_FLAG | ... | SN_AN_FLAG |
| ST_SN_A1 | ST_SN_A2 | ... | ST_SN_AN |
Here 'flag' and 'status' are string values and have to be displayed in a single cell.
Also, using "hard-coded" case-when statements are not a viable option as there are a few hundred stages each containing at least a dozen activities.
As I am new to pivots in sql, any help regarding the same would be welcome
SELECT * from
(select Stage,Activity, flag+' ' + astatus as AStatus
FROM tblStage
)tb
PIVOT
(
Min(AStatus)
FOR Activity IN([A1],[A2])
)p;
Check these :
To Pivot on dynamic columns.
Also Check Pivot on Varchar Values.
I've seen other questions about SQL If-then-else stuff, but I'm not seeing how to relate it to what I'm trying to do. I've been using SQL for about a year now but only basic stuff and never this.
If I have a SQL table that looks like this
| Name | Version | Category | Value | Number |
|:-----:|:-------:|:--------:|:-----:|:------:|
| File1 | 1.0 | Time | 123 | 1 |
| File1 | 1.0 | Size | 456 | 1 |
| File1 | 1.0 | Final | 789 | 1 |
| File2 | 1.0 | Time | 312 | 1 |
| File2 | 1.0 | Size | 645 | 1 |
| File2 | 1.0 | Final | 978 | 1 |
| File3 | 1.0 | Time | 741 | 1 |
| File3 | 1.0 | Size | 852 | 1 |
| File3 | 1.0 | Final | 963 | 1 |
| File1 | 1.1 | Time | 369 | 2 |
| File1 | 1.1 | Size | 258 | 2 |
| File1 | 1.1 | Final | 147 | 2 |
| File2 | 1.1 | Time | 741 | 2 |
| File2 | 1.1 | Size | 734 | 2 |
| File2 | 1.1 | Final | 942 | 2 |
| File3 | 1.1 | Time | 997 | 2 |
| File3 | 1.1 | Size | 997 | 2 |
| File3 | 1.1 | Final | 985 | 2 |
How can I write a SQL IF, ELSE statement that creates a new column called "Replication" that follows this rule:
A = B + 1 when x = 1
else
A = B
where A = the number we will use for the next Number
B = Max(Number)
x = Replication count (this is the number of times that a loop is executed. x=i)
The results table will look like this:
| Name | Version | Category | Value | Number | Replication |
|:-----:|:-------:|:--------:|:-----:|:------:|:-----------:|
| File1 | 1.0 | Time | 123 | 1 | 1 |
| File1 | 1.0 | Size | 456 | 1 | 1 |
| File1 | 1.0 | Final | 789 | 1 | 1 |
| File2 | 1.0 | Time | 312 | 1 | 1 |
| File2 | 1.0 | Size | 645 | 1 | 1 |
| File2 | 1.0 | Final | 978 | 1 | 1 |
| File1 | 1.0 | Time | 369 | 1 | 2 |
| File1 | 1.0 | Size | 258 | 1 | 2 |
| File1 | 1.0 | Final | 147 | 1 | 2 |
| File2 | 1.0 | Time | 741 | 1 | 2 |
| File2 | 1.0 | Size | 734 | 1 | 2 |
| File2 | 1.0 | Final | 942 | 1 | 2 |
| File1 | 1.1 | Time | 997 | 2 | 1 |
| File1 | 1.1 | Size | 997 | 2 | 1 |
| File1 | 1.1 | Final | 985 | 2 | 1 |
| File2 | 1.1 | Time | 438 | 2 | 1 |
| File2 | 1.1 | Size | 735 | 2 | 1 |
| File2 | 1.1 | Final | 768 | 2 | 1 |
| File1 | 1.1 | Time | 786 | 2 | 2 |
| File1 | 1.1 | Size | 486 | 2 | 2 |
| File1 | 1.1 | Final | 135 | 2 | 2 |
| File2 | 1.1 | Time | 379 | 2 | 2 |
| File2 | 1.1 | Size | 943 | 2 | 2 |
| File2 | 1.1 | Final | 735 | 2 | 2 |
EDIT: Based on the answer by Sean Lange, this is my 2nd attempt at a solution:
SELECT COALESCE(MAX)(Number) + CASE WHEN Replication = 1 then 1 else 0, 1) FROM Table
The COALESCE is in there for when there is no value yet in the Number column.
The IF/Else construct is used to control flow of statements in t-sql. You want a case expression, which is used to conditionally return values in a column.
https://msdn.microsoft.com/en-us/library/ms181765.aspx
Yours would be something like:
case when x = 1 then A else B end as A
As SeanLange pointed out in this case it would be better to use an CASE/WHEN but to illustrate how to use If\ELSE the way to do it in sql is like this:
if x = 1
BEGIN
---Do something
END
ELSE
BEGIN
--Do something else
END
I would say the best way to know the difference and when to use which is if you are writing a query and want a different field to appear based on a certain condition, use case/when. If a certain condition will cause a series of steps to happen then use if/else