Prepend Integer Result with string text of "1:" to create ratio - sql

The following query returns a single number, representing the n in a 1:N ratio. Instead of just returning a single number, how can I prepend the integer value with the following text 1: to give the result the correct ratio format?
select round((sum (case when unified_rollup_level_1 = "Company A" and person_type = "Employee" and worker_status = "Active" then 1 else 0 end)) /
(sum (case when unified_rollup_level_1 = "Company A" and person_type = "Employee" and worker_status = "Active" and job_level IN ("08", "09") then 1 else 0 end)),0)

Assuming it would be for MySQL:
select
CONCAT("1:", round((sum (case when unified_rollup_level_1 = "Company A"
and person_type = "Employee"
and worker_status = "Active"
then 1
else 0
end)) /
(sum (case when unified_rollup_level_1 = "Company A"
and person_type = "Employee"
and worker_status = "Active"
and job_level IN ("08", "09")
then 1
else 0
end)),0))
I hope it helps.

You can prepend text by just doing this:
SELECT "Foo" + stuff FROM table;
In this case, if stuff was 3, the result would be "Foo3" (without the quotes).
So in your case:
SELECT "1:" + ... -- the rest of it goes here

Related

Facing issue to get the appropriate count

I have a view which "web.individual_usage_vw".
Whose total count is = 39057.
SQL Query: select count(*) from web.individual_usage_vw;
In this view, it has few columns in which it has numeric data.
So I need to fetch all the records from view where data is > 1.
So I used below query:
select count(*) from web.individual_usage_vw
where "Business & economy" != 0
and "Executive rewards" != 0
and "Health & benefits" != 0
and "Investment" != 0
and "Corporate marketing" != 0
and "Retirement" != 0
and "In General" != 0
and "Mergers & acquisitions" != 0
and "Corporate strategy operations" != 0
and "Broad-based rewards" != 0
and "Leadership" != 0
and "Talent" != 0
and "Other" != 0;
This result me with count 0
Whereas
select count(*) from web.individual_usage_vw
where "Business & economy" = 0
and "Executive rewards" = 0
and "Health & benefits" = 0
and "Investment" = 0
and "Corporate marketing" = 0
and "Retirement" = 0
and "In General" = 0
and "Mergers & acquisitions" = 0
and "Corporate strategy operations" = 0
and "Broad-based rewards" = 0
and "Leadership" = 0
and "Talent" = 0
and "Other" = 0;
Result me with count = 36228
I am not able to debug this error.
There is nothing wrong with the results. The second query is fetching rows only when all your columns (in the predicate) are having 0. In your first query, you are fetching rows where all columns have non zero values. The missing records (delta) might be having one or more columns where the result is zero.
For example
C1, C2
0 0
1 0
1 1
0 1
select * from t1 where c1 = 0 and c2=0; -- returns one row
select * from t1 where c1 != 0 and c2!=0; -- returns one row and not three
Hence the difference. If you need to return all rows that are not returned in your second query then use NOT EXISTS
Roughly, this is what you need.
SELECT columns
FROM table
WHERE NOT EXISTS (SELECT *
FROM table
WHERE all columns = 0) ;

Update a column by comparing two different columns in the same table

I have a table TEST with columns VALUE,VALUE_SIM,SIM_STATUS,ID. I want to update the column SIM_STATUS for the ID = 288. I also want to display the columns after updating.
The conditions are :
1. SIM_STATUS = 0 when VALUE = VALUE_SIM.
2. SIM_STATUS = 1 when VALUE < VALUE_SIM.
3. SIM_STATUS = 2 when VALUE > VALUE_SIM.
I wrote the following query but it is showing an error.
("UPDATE TEST"
"SET SIM_STATE = ( CASE WHEN VALUE = VALUE_SIM THEN SIM_STATE = 0 END )"
"SET SIM_STATE = ( CASE WHEN VALUE < VALUE_SIM THEN SIM_STATE = 1 END )"
"SET SIM_STATE = ( CASE WHEN VALUE > VALUE_SIM THEN SIM_STATE = 2 END )"
"where ID = 288 ");
The query that you want is:
UPDATE TEST
SET SIM_STATE = (CASE WHEN VALUE = VALUE_SIM THEN 0
WHEN VALUE < VALUE_SIM THEN 1
WHEN VALUE > VALUE_SIM = 2
END)
WHERE NUMBER = 288;
Your query has several syntax errors. I don't even know if you intend for the double quotes to be part of the query.
This would do i guess
UPDATE TEST
SET
SIM_STATE =
CASE WHEN VALUE < VALUE_SIM THEN 1
+ CASE WHEN VALUE > VALUE_SIM THEN 2
+ CASE WHEN VALUE = VALUE_SIM THEN 0
WHERE ID = 1

Combining Case Statements in a View

I have these two case statements and can not for the life of me figure out how to combine them to show in a MSSQL view. Any help would be great.
CASE WHEN [ordertype] = '2' THEN [CommissionAmt1] * - 1 ELSE [CommissionAmt1] END
and
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN 0 ELSE CommissionAmt1 END
Just adding the first case to wherever the CommissionAmt1 is referenced in the second statement.
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CASE WHEN [ordertype] = '2' THEN
[CommissionAmt1] * - 1
ELSE
[CommissionAmt1]
END
END
Or going the other way. It was hard to understand which way the calculation needs to be performed. The only hint was []
CASE WHEN [ordertype] = '2' THEN
(
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CommissionAmt1
END
) * - 1
ELSE
CASE WHEN (is_member('Buyer') = 1 OR is_member('CustomerService') = 1) THEN
0
ELSE
CommissionAmt1
END
END
Either way, you would be able to save some calculations by sub querying the dependent value.
SELECT
*,
ValueWithDependant=CASE WHEN (Dependant>0) THEN (SomeValue / Dependant) ELSE NULL END
FROM
(
SELECT
X,Y,Z,
Dependant=CASE WHEN SomeValue=1 THEN 1 ELSE 0 END
FROM
SomeTable
)AS DETAIL

Crystal Reports v14 not finding all null values

Alright, I've run into this before and have gotten everything to work correctly in the past. I have an SQL code that was created that now needs to be turned into a crystal report. The SQL shows 956 lines, but Crystal is only showing 886.
Here is the SQL code:
SELECT
I4240,
I4201,
I4202,
I4203,
I4204,
I4206,
I4213,
I4214,
I4225,
I4208,
I4299
FROM
MT.INVENTORY
WHERE
(
I4202 IN ('UNKNONWN','VERIFY MFR','OTHER','VARIOUS','TBD','NA','N/A') OR
(
I4203 IN ('UNKNONWN','VERIFY MODEL NUMBER','OTHER','VARIOUS','TBD','NA','N/A','MISCELLANEOUS')
OR I4203 IS NULL
OR LENGTH(I4203)=0
) OR
(
I4204 IN ('UNKNOWN DESCRIPTION','VERIFY DESCRIPTION','OTHER','VARIOUS','TBD','NONE - NO STD USED','NA','N/A','MISCELLANEOUS')
OR I4204 IS NULL
OR LENGTH(I4204)=0
)
) AND
I4240 NOT IN ('MT','STD','NESD')
ORDER BY I4240,I4202,I4203,I4204
and the record selection formula from CR:
(
{Inventory.I4240} <> 'mt' and
{Inventory.I4240} <> 'std' and
{Inventory.I4240} <> 'nesd'
)
AND
(
(
{Inventory.I4202} = 'UNKNONWN' OR
{Inventory.I4202} = 'VERIFY MFR' OR
{Inventory.I4202} = 'OTHER' OR
{Inventory.I4202} = 'VARIOUS' OR
{Inventory.I4202} = 'TBD' OR
{Inventory.I4202} = 'NA' OR
{Inventory.I4202} = 'N/A'
)
OR
(
{Inventory.I4203} = 'UNKNONWN' OR
{Inventory.I4203} = 'VERIFY MODEL NUMBER' OR
{Inventory.I4203} = 'OTHER' OR
{Inventory.I4203} = 'VARIOUS' OR
{Inventory.I4203} = 'TBD' OR
{Inventory.I4203} = 'NA' OR
{Inventory.I4203} = 'N/A' OR
{Inventory.I4203} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4203}) OR
LENGTH(trim({Inventory.I4203})) < 1 OR
INSTR(trim({Inventory.I4203}), "") = 0 OR
TRIM({Inventory.I4203}) = ""
)
OR
(
{Inventory.I4204} = 'UNKNOWN DESCRIPTION' OR
{Inventory.I4204} = 'VERIFY DESCRIPTION' OR
{Inventory.I4204} = 'OTHER' OR
{Inventory.I4204} = 'VARIOUS' OR
{Inventory.I4204} = 'TBD' OR
{Inventory.I4204} = 'NONE - NO STD USED' OR
{Inventory.I4204} = 'NA' OR
{Inventory.I4204} = 'N/A' OR
{Inventory.I4204} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4204}) OR
LENGTH(trim({Inventory.I4204})) < 1 OR
INSTR({Inventory.I4204}, "") = 0 OR
TRIM({Inventory.I4204}) = ""
)
)
Any help would be appreciated.
In Crystal, if a particular field can be null then you need to check for that condition as the very first thing you do with it, otherwise the entire formula will error out and nothing will be evaluated.
So in your case, fields I4203 and I4204 need the isnull() check moved to the top of their respective sections at the very least. If I4240 and I4202 can be null, then you should handle those conditions as well.
Also, you have a couple references to the word "UNKNONWN"; is that a typo?
The issue was that the report options were set to use null as default, but for some reason in the record selection formula it was set to exception. Once that was set to the default setting it worked without any issues and the counts were correct.

DB2 SQL Select Statement & Group By

I am having trouble getting my query to group correctly. I need to include the TELLERACTIVITY.ENTRY_DATE date column so I can filter by the date later on. However, I do not want it to group by the TELLERACTIVITY.ENTRY_DATE date because it gives me more results than I want.
SELECT DISTINCT ACTIVITYINFO.MEMBER_NBR AS "Member Number"
,ACCOUNT.ACCOUNT_NBR AS "Account Number"
,TELLERACTIVITY.FOCUS_TELLER_ID AS "Teller ID"
,TELLERACTIVITY.ENTRY_DATE AS "Date"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 1 THEN 1
ELSE 0
END )) AS "Branch 1"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 2 THEN 1
ELSE 0
END )) AS "Branch 2"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 3 THEN 1
ELSE 0
END )) AS "Branch 3"
,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 4 THEN 1
ELSE 0
END )) AS "Branch 4"
FROM TELLERACTIVITY
JOIN OPERATOR
ON TELLERACTIVITY.FOCUS_TELLER_ID = OPERATOR.OPR_NBR
JOIN ACTIVITY
ON TELLERACTIVITY.ACTIVITY_ID = ACTIVITY.ACTIVITY_ID
AND TELLERACTIVITY.FOCUS_TELLER_ID = ACTIVITY.FOCUS_TELLER_ID
AND TELLERACTIVITY.ENTRY_DATE = ACTIVITY.ENTRY_DATE
JOIN ACTIVITYINFO
ON ACTIVITY.ACTIVITY_ID = ACTIVITYINFO.ACTIVITY_ID
JOIN ACCOUNT
ON ACTIVITYINFO.MEMBER_NBR = ACCOUNT.MEMBER_NBR
AND ACTIVITYINFO.ACCOUNT_NBR = ACCOUNT.ACCOUNT_NBR
WHERE TELLERACTIVITY.FOCUS_TELLER_ID < 6000
GROUP BY ACTIVITYINFO.MEMBER_NBR
,ACCOUNT.ACCOUNT_NBR
,OPERATOR.BRANCH_NBR
,TELLERACTIVITY.FOCUS_TELLER_ID
,TELLERACTIVITY.ENTRY_DATE
ORDER BY ACTIVITYINFO.MEMBER_NBR