Need to solve Group by - sql

SELECT WK_NBR,region_cd,plant_cd, POS_GAP_1DAY
FROM(SELECT wk_nbr,region_cd,plant_cd,
round((CASE WHEN SUM(gap_1day)>0 THEN SUM(gap_1day) ELSE 0 END ),2) AS POS_GAP_1DAY
FROM
table a
group by wk_nbr,region_cd,plant_cd)
In the above query the problem I am facing is POS_GAP_1DAY should sum up only the positive numbers in the column but the above query will not give the right answer since I check SUM(gap_1day)>0.
The problem is I need to group by only wk_nbr,region_cd,plant_cd.
Please suggest

Try change
round((CASE WHEN SUM(gap_1day)>0 THEN SUM(gap_1day) ELSE 0 END ),2) AS POS_GAP_1DAY
to
ROUND(SUM(CASE WHEN SIGN(gap_1day) > 0 THEN gap_1day ELSE 0 END), 2) AS POS_GAP_1DAY

done on the spot, so I don't know if it works, but...
SELECT wk_nbr,region_cd,plant_cd,
round(SUM(CASE WHEN GAP_1DAY>0 then GAP_1DAY else 0 end),2) AS POS_GAP_1DAY
FROM
table a
group by wk_nbr,region_cd,plant_cd
doesn't do what you want?

Related

SQL statement selecting a count of a column

I have this SQL query:
SELECT
COUNT(SDDOCO) AS Total
FROM
KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE
SDDRQJ = Day10
which returns a count of all the orders for today.
I am trying to get a second column so that I have this:
To get orders that are not completed would be: SDNXTR < '562'. How would I add this to my sql query so that I can accomplish this goal? Let me know if you need anymore information and thanks in advance for your responses.
You have two options here:
SELECT
COUNT(SDDOCO) AS Total,
SUM(CASE WHEN SDNXTR < '562' THEN 1 ELSE 0 END) AS Not_Finished_Yet_With_SUM,
COUNT(CASE WHEN SDNXTR < '562' THEN 1 ELSE NULL END) AS Not_Finished_Yet_With_COUNT,
FROM
KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE
SDDRQJ = Day10
You can use case statement to count that SDNXTR < '562' values like below:
SELECT count(SDDOCO) as Total,
sum(case when SDNXTR < '562' then 1 else 0 end) as not_yet_finished
FROM KAIPRDDTA.F4211LA, KAIPRDDTA.Dates
WHERE SDDRQJ = Day10

Possible to do math inside of a sum statement?

I'm by no means an expert in DB2 so apologies if this is a novice question. I am looking for a specific requirement. I am summing through a table based on a case statement but am wondering if we could subtract an amount if a field is present in the row.
SELECT CODE,
SUM(CASE SUBSTR(DATE,4,2)
WHEN '01' THEN AMT
END) as JanuaryTotal
FROM Table1
I'm looking to see if we can simply, if another field is present, if we can instead of summing it, simply subtract it from the total that is currently in place.
SELECT CODE,
SUM(CASE SUBSTR(DATE,4,2)
WHEN '01' THEN CASE WHEN TYPE = 'X'
THEN - AMT // some possible way to do this?
ELSE AMT
END) as JanuaryTotal
FROM Table1
Again, apologies. i know this code doesnt run but just giving a visual of something to see if it's possible to do.
SELECT CODE, SUM(
CASE
WHEN SUBSTR(DATE,4,2) = '01' THEN AMT
WHEN TYPE = 'X' THEN 0 - AMT
ELSE AMT
END
) as JanuaryTotal
FROM Table1
Sometimes it helps to indent. Also, it is not quite clear what to do if the sub-string is not 01 -- I'm guessing you don't want to add anything.
SELECT CODE,
SUM(CASE SUBSTR(DATE,4,2)
WHEN '01' THEN
CASE
WHEN TYPE = 'X' THEN -1 * AMT
ELSE AMT
END
ELSE 0 -- SUBSTR(DATE,4,2) is not '01'
END) as JanuaryTotal
FROM Table1
The simplest final query is to flatten out your conditions so they stand alone:
SELECT CODE, SUM(CASE
WHEN SUBSTR(DATE,4,2) AND TYPE = 'X' THEN 0-AMT
WHEN SUBSTR(DATE,4,2) AND THEN AMT END) as JanuaryTotal
FROM Table1
Note that there are two forms of CASE:
CASE <expression>
WHEN <value> THEN ...
WHEN <value> THEN ...
and
CASE
WHEN <condition> THEN ...
WHEN <condition> THEN ...
I switched your query from using the first form to the second form.
SELECT CODE, SUM(
CASE
WHEN SUBSTR(DATE,4,2) = '01' and TYPE = 'X' THEN -AMT
else AMT
END
) as JanuaryTotal
FROM Table1
group by CODE

SQL If syntax error

I have lil - one sql stuck error (1064).
SELECT sum(if(d.o_amount<='10')) as ten_s
I know we have here simple syntax error, but how i can fix it?
Thanx!
I guess you mean:
SELECT sum(d.o_amount) as ten_s FROM table_name WHERE d.o_amount <= 10
Even you can use a CASE expression. Just an other perspective.
Query
select sum(case when d.o_amount <= 10 then d.o_amount else 0 end) as ten_s
from your_table_name;
Depending of the rest of your query:
you can just add a restriction on amount value:
SELECT sum(d.o_amount) as ten_s
FROM table_name
WHERE d.o_amount <= 10
Or use a case expression to keep all rows but only sum thoses you need:
SELECT sum(case when d.o_amount <= 10 then d.o_amount else 0 end) as ten_s
Use CASE
SELECT SUM(CASE WHEN d.o_amount<=10 THEN d.o_amount ELSE 0 END) as ten_s

Oracle sql case statement

I need some help with case statement.
my code:
select c.name, c.date_entered, i.transaction, i.planned_date,
case (c.date_entered - i.planned_date)
when (c.date_entered - i.planned_date) > 0 then 'YES'
when (c.date_entered - i.planned_date) < 0 then 'NO'
end RESPECT
from company c,transaction i
but it give me error ORA-00907; missing right parenthesis
thank you to help me with.
select c.name, c.date_entered, i.transaction, i.planned_date,
case when c.date_entered - i.planned_date > 0
then 'YES'
when c.date_entered - i.planned_date < 0
then 'NO'
end as RESPECT
from company c, transaction i
You should also think about the output of when c.date_entered - i.planned_date = 0 or change one of the cases to <= or >=.

SQL SUM with Where Condition

sum(raceprice) / sum(bidding_price) * sum( bidding_price Where bidding_type = 'small')
- bet_price as Totla_profit
I keep getting error message about that sum condition, don't know what is wrong with it, please help.
Replace
sum(bidding_price Where bidding_type = 'small')
with
sum(case when bidding_type = 'small'
then bidding_price
else 0
end)