I want to express the following in SQL DB2. I want to add up value of sales based on one of two conditions.
I think I need a CASE statement, but not sure. Let me know if pseudo code is clear..
SELECT Total_Service_Starts, Total_Service_Stops
If ServiceFile.SERVICE_START > 20200101
Then Total_Service_Starts = SUM(ServiceFile.Rate * ServiceFile.Units)
If ServiceFile.SERVICE_STOP > 20200101
Then Total_Service_Stops = SUM (ServiceFile.Rate * ServiceFile.Units)
thank you!
You are right, case is what you want. Try it this way:
select
sum(case when ServiceFile.SERVICE_START > 20200101
then (ServiceFile.Rate * ServiceFile.Units)
else 0 end) as "TotalServiceStarts",
sum(case when ServiceFile.SERVICE_STOP > 20200101
then (ServiceFile.Rate * ServiceFile.Units)
else 0 end) as "TotalServiceStops"
from ServiceFile
where service_start > 20200101
or service_stop > 20200101;
Related
How I can achieve below,
I have to check last 30 days from the latest statement date w.r. to account using following formula
if ((sum of credits for 30 days from the latest statement date w.r. to account/
sum debits for 30 days from the latest statement date w.r. to account)
-sum debits for 30 days from the latest statement date w.r. to account)>0 then YES else NO
below are the two columns from two different tables based on which i need to derived formula for each account. And accounts present in both tables A and B.
STATEMENT_DATE_LATEST; --from TableA
LATEST_BAL_IN_USD; -- from TableB
Note: "/" is Divided by and "-" is Minus sign in above statement
If I understood it correctly, would something like this help?
with temp as
(select a.account,
sum(case when b.transaction_amount >= 0 then b.transaction_amount end) sum_credits,
sum(case when b.transaction_amount < 0 then b.transaction_amount end) sum_debits
from table_a a join table_b b on a.account = b.account
where b.statement_date > a.statement_date_latest + 30
group by s.account
)
select t.account,
case when (t.sum_credits / t.sum_debits) - t.sum_debits > 0 then 'YES'
else 'NO'
end result
from temp t;
--56/25000*100 = 0.244 = 0.2 % as percentage column answer
I am trying to write out this math problem as a query in pl/sql.
The table looks like this
Test_Name , Total# Pass, Fail, Percent, View_name
I need to write out a query that divides the total number of records that fail (56) against the total number of records with in the table (25000) times 100.
How can this be queried?
Sounds like you want something like this
select 100.0 * count(case when fail = true then 1 else null end) / count(*)
from table_name;
I have a huge Hive table consisting of ten product fields, date fields for the purchases, and an identifier. The product fields are named like prod1, prod2, ... , prod10 and refer to the last ten products purchased. For most IDs, we don't have purchase history all the way back to ten products.
I'd like to construct a distribution of population rates for each of the prod<X> fields, to show the breakdown of purchase history across the entire dataset.
Currently, I'm running a bash script that runs ten consecutive queries against the table like:
hive -e "select count(1) from db.tbl where prod<X> != '';"
... and saving the output to a file. This seems clunky and inefficient. Is there a better way to specify Hive counts on a range of fields with a range of field conditions? I've tried to come up with a strategy using groupby or even mapping a range of fields, but can't quite wrap my head around specifying the != '' condition for each field.
Thanks in advance for any direction.
select id,
sum(case when prod1='' then 0 else 1 end),
sum(case when prod2='' then 0 else 1 end),
sum(case when prod3='' then 0 else 1 end),
sum(case when prod4='' then 0 else 1 end),
sum(case when prod5='' then 0 else 1 end),
sum(case when prod6='' then 0 else 1 end),
sum(case when prod7='' then 0 else 1 end),
sum(case when prod8='' then 0 else 1 end),
sum(case when prod9='' then 0 else 1 end),
sum(case when prod10='' then 0 else 1 end)
from table group by id;
I have an encountered problem in my query in Oracle SQL. I don't know how to get the sum of this query:
select call_type, channel
,count (case when status="no answer" then 1 end else 0) as cnt_no_answer
,count (case when status="answered" then 1 end else 0) as cnt_answer
from app_account.cc_call;
Please Help me. Thanks!
To get the answered and not answered records sum instead of count. To get the number of all records that are either answered or not answered use count(status). To get the count of all records, i.e. also records with status null use count(*). Strings need single quotes, not double quotes. The case statement needs END.
EDITED (there were too many END used):
select call_type, channel
, sum(case when status='no answer' then 1 else 0 end) as cnt_no_answer
, sum(case when status='answered' then 1 else 0 end) as cnt_answer
, count(status) as cnt_all_stated
, count(*) as cnt_all_records
from app_account.cc_call
group by call_type, channel;
try this again, i edit it:
SELECT call_type, channel,
sum (CASE WHEN status='no answer' THEN 1 ELSE 0 END) AS cnt_no_answer,
sum (CASE WHEN status='answered' THEN 1 ELSE 0 END) AS cnt_answer
FROM app_account.cc_call
GROUP BY call_type, channel;
Check this out:
SELECT CALL_TYPE,
CHANNEL,
COUNT (CASE WHEN UPPER(STATUS) = UPPER('no answer') THEN 1 ELSE NULL END)
AS CNT_NO_ANSWER,
COUNT (CASE WHEN UPPER(STATUS) = UPPER('answered') THEN 1 ELSE NULL END)
AS CNT_ANSWER
FROM APP_ACCOUNT.CC_CALL
GROUP BY CALL_TYPE, CHANNEL;
select call_type, channel
, sum(case when status='no answer' then 1 else 0 end) as cnt_no_answer
, sum(case when status='answered' then 1 else 0 end) as cnt_answer
, count(status) as cnt_all_stated
, count(*) as cnt_all_records
from app_account.cc_call
group by call_type, channel;
why usind end twice
SELECT Call_type, Channel
,COUNT(CASE WHEN status="no answer" then 1 else 0 end ) as Cnt_no_answer
,COUNT(CASE WHEN status="answered" then 1 else 0 end ) as Cnt_answer
from App_account.cc_call
GROUP BY call_type,channel;
I think there is some error in table deceleration. Pleas give table structure. And you want SUM and YOU are COUNTING there I dnt think it is error but lack of overview. Please give some more details.
Hi Please help me out i Need to Sum the Values and then Minus Credit and Debit Note in Oracle 9i
ID Acc_TYP Amt
1 CR 100
2 CR 200
3 DB 50
4 DB 50
Using the Above Table Structure, I Need to Compute the Balance by (CR-DB), how to form the logic
in Single Query, Please help me to solve the issue , am very beginner in oracle. Thanks in Advance
select sum(
case
when acc_typ='CR' then amt
when acc_typ='DB' then -amt
else 0
end) as balance
from the_table
where acc_typ in ('CR', 'DB');
You should use CASE statement:
select sum(case when Acc_TYP='DB'
then -amt
else amt
end ) from t
Yet another Case-based solution, using a "simple case statement": http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/controlstructures.htm#i8305
select sum(case acc_type
when 'CR' then +amt
when 'DB' then -amt
end) balance
from ...
Try this:
select sum(n) from (
(select sum(amt) as n from table where acc_typ='CR')
union
(select -sum(amt) as n from table where acc_typ='DB')
)