Count() and VIEWS in SQL Server Express - sql-server

dbo.WScnt? are views
The tcnt1 is defined as (INT, null) in WScnt1 view. The tcnt1 values is a result from using COUNT(*) function.
Same for all other tcnt? values in each WScnt? view.
Statement:
SELECT
*,
CONVERT(decimal(7, 2), ((tcnt2 / tcnt1) * 100)) AS Pct1,
CONVERT(decimal(7, 2), ((tcnt4 / tcnt3) * 100)) AS Pct2
FROM
dbo.WScnt1, dbo.WScnt2, dbo.WScnt3, dbo.WScnt4;
RESULT:
tcnt1 tcnt2 tcnt3 tcnt4 Pct1 Pct2
--------------------------------------------
38347 12287 673 125 0.00 0.00
Question: why do I get 0.00 for Pct1 and Pct2?

Changed code not using VIEWS:
declare #myInt1 as NUMERIC(9,0);
set #myInt1 = (select COUNT(*) from dbo.Weekly);
Thanks marc_s!

Related

How to insert decimal after the first character in whole number in SQL

I'm creating a report which displays the grades of student but the problem is the system only accepts whole number. And I need to display grades with decimal.
I tried using CONVERT(decimal(10, 2), #VALUE / 1000.00) but the result is not what i need.
Here's my code
, (CASE WHEN (SELECT TRNSFR_SRC_TYPE FROM PS_TRNS_CRSE_SCH WHERE EMPLID in (dtl.EMPLID) AND MODEL_NBR = dtl.MODEL_NBR AND ACAD_CAREER = dtl.ACAD_CAREER) = 'M' AND CRSE_ID <> '' THEN
(CASE WHEN (SELECT GRADE_POINTS FROM PS_GRADE_TBL WHERE CRSE_GRADE_INPUT = dtl.CRSE_GRADE_OFF AND GRADING_SCHEME = dtl.GRADING_SCHEME AND GRADING_BASIS = dtl.GRADING_BASIS) <> 0 THEN CONVERT(decimal(10, 2), (SELECT CAST(ROUND(GRADE_POINTS, 2) AS NUMERIC(12,2)) FROM PS_GRADE_TBL WHERE CRSE_GRADE_INPUT = dtl.CRSE_GRADE_OFF AND GRADING_SCHEME = dtl.GRADING_SCHEME AND GRADING_BASIS = dtl.GRADING_BASIS)) / 1000.00
ELSE
convert(decimal (10, 2) ,dtl.CRSE_GRADE_OFF / 1.00)
I expect the output should be 1.00 or 1.50 or 5.00. If the value is 500 then it should output of 5.00 another example is 150 then it should display as 1.50.
Try editing formatting options (Textbox Properties) of the specified column. It is generally related with formatting not the SQL itself.
Try clicking on the field, going to the Properties Pane an under Number > Format, changing the format to: 0.00. I entered a few numbers and this was working for me.
SSRS Properties Pane
Thank you guys for all the help, I found the solution to my problem by using CAST
cast(cast(CRSE_GRADE_INPUT as decimal(10,2))* 0.01 as decimal(10,2))

Fixing divide by zero error with case statement

I have inherited some code with a divide by zero error.
I keep getting the error and not sure if it is the code or where I am placing it.
Should this line be added or replace a line?
Tried adding and replacing with this case statement:
CASE WHEN SUM(PREOP_MME) = 0 THEN 0 ELSE SUM(PREOP_MME) / SUM(COUNT_CASES) END AS PREOP_MME).
SELECT
PROCEDURE_NM
,SUM(PreOp_MME) / SUM(COUNT_CASES) PreOp_MME
,SUM(IntraOp_MME) / SUM(COUNT_CASES) IntraOp_MME
,SUM(PostOp_MME / PostOp_LOS) PostOp_MME
,SUM(Discharge_MME) / SUM(COUNT_CASES) Discharge_MME
,SUM(TOTAL_OVERALL_MME) / SUM(COUNT_CASES) Overall_MME
FROM (
SELECT DISTINCT
Spine.LOG_ID
,Spine.SERV_AREA_ID [Service Area ID]
,Spine.LOC_ID [Revenue Location ID]
,Spine.PRIMARY_PHYS_ID [Provider ID]
,Spine.SURGERY_DATE
,Spine.COUNT_CASES
,Spine.PROCEDURE_NM
,Spine.PostOp_LOS
,(PreOp.SUM_SIG * PreOp.PreOp_MME) AS PreOp_MME
,(IntraOp.SUM_SIG * IntraOp.IntraOp_MME) AS IntraOp_MME
,(PostOp.SUM_SIG * PostOp.PACU_MME) AS PostOp_MME
,DischMeds.DOSE_MME AS Discharge_MME
,(PreOp.SUM_SIG * PreOp.PreOp_MME) +
(IntraOp.SUM_SIG * IntraOp.IntraOp_MME) +
(PostOp.SUM_SIG * PostOp.PACU_MME) + DischMeds.DOSE_MME as TOTAL_OVERALL
You should check for the divisor ( denominator) and not for the divided (numerator) is not equal to 0 so for the first column you coudl try
SELECT
PROCEDURE_NM,
case when SUM(COUNT_CASES) = 0
then 0
else SUM(PreOp_MME)/ SUM(COUNT_CASES) end PreOp_MME,
.....
Just use COALESCE
Here I build a small demo as example
SELECT id,
SUM(`value1`) / SUM(`value2`) as _before,
COALESCE (SUM(`value1`) / SUM(`value2`) , 0) as _after
FROM Table1
GROUP by id;
Thank you for your answers.
I used the CASE WHEN SUM(FIELD) = 0 THEN 0 ELSE SUM(FIELD) / SUM(FIELD) END AS
My error was because I was not accounting for zeros in each instance, only the first. Must be time to go home!

sql server decimal data type issue

Decimal data type in table Length = 9,Precision = 10,Scale = 2,
actual value in #Bvisits = 8.00
declare #Bvisits decimal,#ActualVisit decimal,#Ptax decimal = 1.995;
select #Bvisits = BalanceVisit from PakacgeTb where PackageID = 25306;
set #ActualVisit = #Bvisits - #Ptax;
select #ActualVisit as VIP
result is 6.
But as i use it in real stored procedure it updates result as 4.67 instead of 6 why is the issue occurring even if i use 1.9 instead of 1.995 still same issue occurs.
Try like this :
declare #Bvisits decimal(10,3),#ActualVisit decimal(10,3),#Ptax decimal(10,3) = 1.995;
select #Bvisits = BalanceVisit from PakacgeTb where PackageID = 25306;
set #ActualVisit = #Bvisits - #Ptax;
select #ActualVisit as VIP

SQL Update a table, use Case expression, then sum a column to get the total

i'm updating a table with a case expression and then having trouble finding a way to sum a column to get the total, would appreciate any input.
thanks in advance.
Dave
--Declare & Set Variables--
DECLARE #10001 INT = 20
DECLARE #10002 INT = 40
DECLARE #10003 INT = 60
DECLARE #10004 INT = 80
DECLARE #10005 INT = 100
--Update--
UPDATE COA_DATE
SET Amount =
CASE
--Sales Volume
When GL_Account_No = 11000 Then #10001 * #10005
When GL_Account_No = 11001 Then #10001 * #10004
When GL_Account_No = 11002 Then #10001 * #10002
When GL_Account_No = 11003 Then #10001 * #10003
When GL_Account_No = 11004 Then #10001 * #10005
END
---create a total of column 'GL_Account_No' ie.
sum(11000+11001+11002+11003+11004) = row name 'Sales Total'
---desired output
--GL_Account_No Amount
--11000 120,000
--11001 96,000
--11002 48,000
--11003 72,000
--11004 120,000
--Sales Total 456,000
Aggregations are usually better handled in some sort of reporting layer, rather than embedded in your SQL query: the language is designed to work with sets, and mixing data in your result set (some detail-level rows, a grand total row) generally leads to more trouble than it's worth.
Like all things that are not "usually" a good idea, there are exceptions. SQL Server has the ROLLUP operator for this circumstance: searching for "SQL ROLLUP" will find you a few good articles to get you started (such as this one or this one).
After running the UPDATE statement in your original post, you can then retrieve the data from your table with code like the following (untested).
SELECT
gl_account_no
,SUM(Amount) AS Amount
FROM COA_DATE
WHERE gl_account_no IN (11000,11001,11002,11003,11004)
GROUP BY gl_account_no
WITH ROLLUP

Netezza Timesptamp failing

I'm trying to query the database as flows:
select count(distinct(TE_ID)) from TE where LAST_UPDATE_TIME >= '2013-01-08-00:00:00.000000' and LAST_UPDATE_TIME < '2013-01-09-00:00:00.000000'
However the error I receive is:
11:25:09 [SELECT - 0 row(s), 0.000 secs] [Error Code: 1100, SQL State: HY000] ERROR: Bad timestamp external representation '2013-01-08-00:00:00.000000'
... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec [0 successful, 0 warnings, 1 errors]
The timestamp you are giving has an extra dash.
Yours: select cast('2013-01-08-00:00:00.000000' as timestamp)
Should be: select cast('2013-01-08 00:00:00.000000' as timestamp)
To control it might be a good idea to explicitly cast like the example below:
to_timestamp('2013-01-08 00:00:00.000000','YYYY-MM-DD HH:MI:SS.US')
HH = Hour
MI = Minute
SS = Second
US = Microseconds
Try this:
select count(distinct(TE_ID)) from TE where LAST_UPDATE_TIME >= '2013-01-08 00:00:00.000000' and LAST_UPDATE_TIME < '2013-01-09 00:00:00.000000'

Resources