1
DECLARE 
var1 INTEGER :=0;
var2 INTEGER :=0;
BEGIN

SELECT DISTINCT
<whatever here>

CASE
WHEN trunc(thisDate) - TRUNC(thatDate) BETWEEN 0 AND 10 THEN var1+1
WHEN trunc(thisDate2) - TRUNC(thatDate2) BETWEEN 11 AND 20 THEN var2+1 
ELSE 0 
END 

FROM
<Rest of query here>

basically what I want to be able to do is to add 1 to the local variable then print out the value of that variable as part of my select statement using a count (or sum of the count) or something everytime the difference in the ages falls within those categories

I'm not sure how to add to the local variable basically.

2
  • I suspect that the OP actually wants an incrementing value for each range. SQL Server allows this by letting the variable be assigned a new variable for each row. I suspect that he is trying to reproduce this functionality. Commented Nov 20, 2014 at 16:52
  • Very good spot, @Allan (I've messed up when +1 you comment -- sorry). Dan, maybe you should provide some sample input data and the expected outcome. Commented Nov 20, 2014 at 16:58

4 Answers 4

2
SELECT 
    sum(CASE WHEN trunc(thisDate) - TRUNC(thatDate) BETWEEN 0 AND 10 THEN 1 ELSE 0 END) + var1,
    sum(CASE WHEN trunc(thisDate2) - TRUNC(thatDate2) BETWEEN 11 AND 20 THEN 1 ELSE 0 END) + var2
    into var1, var2
FROM
<Rest of query here>
Sign up to request clarification or add additional context in comments.

4 Comments

check that your else condition will return var1 instead of 0
@Aramillo I'm not sure I understand your comment. I suppose I only forgot to sum
sorry my bad english, but what i mean is that in case that condition is false you return 0 + var2, and OP wants only 0
@Aramillo I think that after adding a sum it's not actual anymore.
1

Try analytic functions

BEGIN
SELECT DISTINCT
<whatever here>

sum(CASE
WHEN trunc(thisDate) - TRUNC(thatDate) BETWEEN 0 AND 10 THEN 1
end) over() var1,
sum(CASE
WHEN trunc(thisDate2) - TRUNC(thatDate2) BETWEEN 11 AND 20 THEN 1 
END) over() var2

FROM
<Rest of query here>

Comments

1

Given a comment by Allan, maybe you are looking for something like than instead:

SELECT V.*,
    case when t1 is not null then
          count(t1) over (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
         when t2 is not null then
          count(t2) over (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
         else
          0
    end varX
    FROM (
    SELECT DISTINCT

    <whatever here>

    ROWNUM n,
    CASE WHEN trunc(thisDate) - TRUNC(thatDate) BETWEEN 0 AND 10 THEN 1 END t1,
    CASE WHEN trunc(thisDate) - TRUNC(thatDate) BETWEEN 11 AND 20 THEN 1 END t2


    FROM
    <Rest of query here>

    ) V

Some example for you to check if this is what you are looking for: http://sqlfiddle.com/#!4/2de0d/2

The inner query is basically yours, plus

  • a ROWNUM column (maybe not necessary if you somehow ORDER BY your rows)
  • a marker set to 1 if the row is in the first range, NULL otherwise
  • a marker set to 1 if the row is in the second range, NULL otherwise

The outer query use the analytic function COUNT() OVER(...) to respectively count the numbers of markers between the current row and the first of the result set. The row number n is used here. Replace that by something more relevant if your data are already ordered.

Comments

0

I don't think you can update some variable while executing a query. All you can do is fetch the result value into some variable.

Slight variation over Multisync's answer, using COUNT instead of SUM:

SELECT 
    count(CASE WHEN trunc(thisDate) - TRUNC(thatDate) BETWEEN 0 AND 10 THEN 1 END) ,
    count(CASE WHEN trunc(thisDate2) - TRUNC(thatDate2) BETWEEN 11 AND 20 THEN 1 END)
    into var1, var2
FROM
<Rest of query here>

This will actually return a one-row result with the count of values is the first range in the first column, and the count of values in the second range in the second column. Using the INTO var1,var2 clause, PL/SQL will implicitly fetch those values into your local variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.