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
Related
I am trying to return a count of claims in CASE statement: Here is my select statement but only am getting back 1 and 0 as my results instead of the count.
SELECT DISTINCT
GROUP.Group_ID,
GROUP.GROUP_NAME,
SUM(DISTINCT CASE
WHEN TRUNC(CLAIM.PROCESS_DATE)-TRUNC(CLAIM.RECEIVED_DATE) < 11 THEN '1'
ELSE '0'
END) AS "0-10_DAYS",
SUM(DISTINCT CASE
WHEN TRUNC(CLAIM.PROCESS_DATE)-TRUNC(CLAIM.RECEIVED_DATE) < 31 THEN '1'
ELSE '0'
END) AS "0-30_DAYS"
I am looking to return number of claims that fall into these buckets after calculating process days. I am new to SQL so any help will be appreciated.
Thank you!
you need to add the group by columns . GROUP.Group_ID, GROUP.GROUP_NAME,i.e in the group by clause.
I have a select clause with a case statement and I need to create another case statement comparing the column created by the previous case statement. Something like this:
select client
,discount
,(case when sales_avg>10000 then 30
when sales_avg>5000 then 20
else 0 end) discount_rule
,(case when discount < discount_rule then 1 else 0 end) status
from sales;
I get a message that discount_rule is unknown. How can I accomplish that?
You can use a Common Table Expression (CTE) and reference a CTE within a CTE as:
with CTE_discount_rule as
(
select client,
discount,
(case when sales_avg>10000 then 30
when sales_avg>5000 then 20
else 0 end) as discount_rule
from sales
),
CTE_Final_Status as
(
select client,
discount,
discount_rule,
(case when discount < discount_rule then 1 else 0 end) as status
from CTE_discount_rule
)
select * from CTE_Final_Status;
The simplest way is to use a subquery that returns the column discount_rule:
select t.client, t.discount, t.discount_rule,
case
when discount < discount_rule then 1
else 0
end status
from (
select client, discount,
case
when sales_avg > 10000 then 30
when sales_avg > 5000 then 20
else 0
end discount_rule
from sales
) t
I may be missing something obvious! Thanks in advance for any help.
I am trying to use a CASE statement in an inline SQL Statement. I only want to evaluate the expression once, so I am looking to put the expression in the CASE section, and then evaluate the result in each WHEN. Here is the example:
SELECT
MyTable.ColumnA,
CASE DateDiff(d, MyTable.MyDate, getDate())
WHEN <= 0 THEN 'bad'
WHEN BETWEEN 1 AND 15 THEN 'reasonable'
ELSE 'good'
END as MyCalculatedColumn,
MyTable.SomeOtherColumn
I know I can do this:
CASE
WHEN DateDiff(d, MyTable.MyDate, getDate()) <= 0 THEN 'bad'
WHEN DateDiff(d, MyTable.MyDate, getDate()) BETWEEN 1 AND 15 THEN 'reasonable'
ELSE 'good'
END
But in my first example, SQL does not seem to like this statement:
WHEN <= 0 THEN 'bad'
Note that the statement is inline with other SQL, so I can't do something like:
DECLARE #DaysDiff bigint
SET #DaysDiff = DateDiff(d, MyTable.MyDate, getDate())
CASE #DaysDiff
WHEN <= 0 THEN 'bad'
WHEN BETWEEN 1 AND 15 THEN 'reasonable'
ELSE 'good'
END
My actual DateDiff expression is much more complex and I only want to maintain its logic, and have it evaluated, only once.
Thanks again...
You can use apply for this purpose:
SELECT MyTable.ColumnA,
(CASE WHEN day_diff <= 0 THEN 'bad'
WHEN BETWEEN 1 AND 15 THEN 'reasonable'
ELSE 'good'
END) as MyCalculatedColumn,
MyTable.SomeOtherColumn
FROM MyTable CROSS APPLY
(VALUES (DateDiff(day, MyTable.MyDate, getDate()))) v(day_diff)
APPLY is a very handy way to add calculated values into a statement. Because they are defined in the FROM clause, they can be used in SELECT, WHERE, and GROUP BY clauses where column aliases would not be recognized.
I see your problem. CASE expression WHEN value can only do an equality check
You could try using a CTE (Common Table Expression), do everything except the case statement in the CTE and then put the CASE in the final SELECT at the end. I'm not sure whether it will prevent the expression being evaluated twice - thats kindof the optimisers problem, not yours (thats how I like to think about it)
WITH cteMyComplexThing AS(
SELECT MyTable.ColumnA,
DateDiff(d, MyTable.MyDate, getDate()) as ComplexThing,
MyTable.SomeOtherColumn
FROM MyTable
)
SELECT
ColumnA,
CASE
WHEN ComplexThing <= 0 THEN 'bad'
WHEN ComplexThing BETWEEN 1 AND 15 THEN 'reasonable'
ELSE 'good'
END as MyCalculatedColumn,
SomeOtherColumn
FROM cteMyComplexThing
The WHEN clause in a CASE statement needs both sides of the condition. <=0 cannot stand by itself.
CASE #DaysDiff
WHEN ? <= 0 THEN 'bad'
WHEN BETWEEN 1 AND 15 THEN 'reasonable'
ELSE 'good'
END
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
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?