I have the following input data(A sample only):
ListID
date
Value
0
2022-10-17
0
1
2022-10-17
43.050430504
3
2022-10-17
40.000000000
4
2022-10-17
38.636363636
5
2022-10-17
20.714285714
I am little bit confused about two below query results.
First Query:
SELECT
ListID,
CASE
WHEN date>'2022-07-22'
THEN avg(value)
ELSE NULL
END AS 'Value_Before_Rate_Change'
FROM
TB01 where date like '2022%' and ListID=1;
Output first query:
Value_Before_Rate_Change
NULL
Second Query
select avg(value)
from TB01
where date like '2022%'
and ListID=1
and date>'2022-07-22';
Output second query:
avg(value)
57.773696518595
Can someone show me why I am always getting NULL as an result when I use CASE.
Update:
I used below group by as well. But same result
SELECT
ListID,
CASE
WHEN date>'2022-07-22'
THEN avg(value)
ELSE NULL
END AS 'Value_Before_Rate_Change'
FROM
TB01 where date like '2022%' and ListID=1 group by ListID;
select listID
,avg(case when date > '2022-07-22' then value end) as Value_Before_Rate_Change
from t
group by listID
listID
Value_Before_Rate_Change
0
0.0000000000000000
1
43.0504305040000000
3
40.0000000000000000
4
38.6363636360000000
5
20.7142857140000000
Fiddle
Related
I've a sample data
id date user_id customer_id status
1 2022-06-23 1 12 no response
2 2022-06-23 1 12 no response
3 2022-06-24 1 12 no response
4 2022-06-23 2 15 no response
4 2022-06-23 2 15 successful
5 2022-06-23 3 16 call later
I need to fetch those kind of records where a user_id called the same customer_id on the same day got only no response status more than once but not any other statuses.
The result for the above example would be
id
1
2
You can use aggregation and the conditions in the HAVING clause:
SELECT user_id, customer_id, date
FROM tablename
GROUP BY user_id, customer_id, date
HAVING COUNT(*) > 1 AND SUM(status <> 'no response') = 0
If you want the respective rows of the table use the above query with the operator IN:
SELECT *
FROM tablename
WHERE (user_id, customer_id, date) IN (
SELECT user_id, customer_id, date
FROM tablename
GROUP BY user_id, customer_id, date
HAVING COUNT(*) > 1 AND SUM(status <> 'no response') = 0
);
See the demo.
I have a database table (return_period) having records
id ReturnPeriod Value Date
1 10 10X 11/1/2012
2 20 20x 11/1/2012
3 30 30x 11/1/2012
4 10 10xx 12/1/2013
5 20 20xx 12/1/2013
6 30 30y 1/1/2015
7 30 303 1/1/2015
and expecting an output table like below:
Date Rp10_Value Rp20_Value Rp30_Value
11/1/2012 10x 20x 30x
12/1/2013 10XX 20XX
1/1/2015 30y
1/1/2015 303
I want records based on the dates(want the multiple records).Is there a way I can write a query to this type of requirement.Thanks
select date,
case when rp=10 then value else null end as Rp10_Value,
case when rp=10 then value else null end as Rp20_Value,
case when rp=10 then value else null end as Rp30_Value
from
(
SELECT date, value, ReturnPeriod
FROM Table2 where ReturnPeriod=10
union all
SELECT date, value, ReturnPeriod
FROM Table2 where ReturnPeriod=20
union all
SELECT date, value, ReturnPeriod
FROM Table2 where ReturnPeriod=30
) ;
This is a pivot. In MySQL, you can use conditional aggregation:
select rp.date,
max(case when returnperiod = 10 then value end) as rp10_value,
. . .
from return_period rp
group by rp.date;
EDIT:
I see you have duplicates. The same idea applies, but you need to include a sequence number:
select rp.date,
max(case when returnperiod = 10 then value end) as rp10_value,
. . .
from (select rp.*,
row_number() over (partition by date, returnperiod order by date) as seqnum
from return_period rp
) rp
group by rp.date, seqnum;
I want to retrieve/compute the price on a given date for different assets, depending on the side of the transaction. Prior to 2000, I have mid quotes, afterwards I have bid and ask or offer quotes, so I would like the price to be the average of these two quotes. More precisely:
SELECT date, price,
CASE WHEN side='' THEN 'price_mid'
WHEN side='A' THEN 'price_ask'
WHEN side='B' THEN 'price_bid'
WHEN side='O' THEN 'price_offer'
END as prices
FROM table
WHERE asset = 'a';
How can I then compute the price in a new column, having the price_mid (prior to 2000) and (price_bid+price_ask)/2 or (price_bid+price_offer)/2 afterwards?
E.g.: Let's say I have the following situation:
date price prices
1 1 price_mid
2 1.1 price_mid
3 0.9 price_bid
3 1.2 price_ask
4 1.3 price_offer
4 1.1 price_bid
And I would like to have:
date final_price
1 1
2 1.1
3 1.05
4 1.2
I understand you need the average for only some dates. Maybe the following does what you want:
SELECT date, AVG(price) as AvgValue
FROM prices
WHERE date >= 2
AND prices in ('price_ask','price_offer','price_bid')
GROUP BY date
UNION
SELECT date, price as AvgValue
FROM prices
WHERE date < 2
AND prices = 'price_mid'
GROUP BY date
UNION
SELECT date, price as AvgValue
FROM prices p
WHERE date >= 2
AND prices = 'price_mid'
AND NOT EXISTS (SELECT 1 FROM prices p2 where p2.date = p.date AND p2.prices in ('price_ask','price_offer','price_bid'))
GROUP BY date
ORDER BY DATE ASC
Something like this
SELECT `date`, AVG(price) AS final_price FROM tbl GROUP BY 1
Tried something, it did not work, nevertheless maybe that helps..
This is a changed version of the query you already have, but it does not save a kind of state, it only has all the values on the right places.
SELECT
date,
CASE WHEN side = '' THEN price
ELSE NULL
END as price_mid,
CASE WHEN side = 'A' THEN price
ELSE NULL
END as price_ask,
CASE WHEN side = 'B' THEN price
ELSE NULL
END as price_bid,
CASE WHEN side = '' THEN price
ELSE NULL
END as price_offer
FROM
table
WHERE
asset = 'a';
What should give you:
date price_mid price_ask price_bid price_offer
1 1 NULL NULL NULL
2 1.1 NULL NULL NULL
3 NULL NULL 0.9 NULL
3 NULL 1.2 NULL NULL
4 NULL NULL NULL 1,4
4 NULL NULL 1.1 NULL
Now on that table you can check with IS NULL or take the SUM()or the MAX() per date, so you can switch through your cases on one line.
My Data is
ID SCORE
1 55
1 -1
1 25
1 -1
1 -1
1 35
2 25
2 -1
2 65
2 55
2 21
2 -1
Now i want to add/sum the score of each id ignoring -1 and i am trying with this code which is not working
SELECT SUM(CASE when SCORE>-1 THEN SUM(SCORE) ELSE 0 END)
FROM jbit WHERE htno='$id'
Here i am already using WHERE so how can i use another WHERE, if i use multiple Where in single query it may effect other processes.. please help me out
Help me out friends
if there is only two column then there is no need to use SUM, you can try below
SELECT id, IF(SCORE=-1,0,SCORE) AS scoreSum
FROM table1
GROUP BY id
Working DEMO
alternative ( not tested )
SELECT id, SUM(IF(SCORE=-1,0,SCORE)) AS scoreSum
FROM table1
WHERE htno =$id
GROUP BY id
SELECT SUM(score) AS score_sum
FROM jbit
WHERE htno='$id'
AND score <> -1 ;
SELECT SUM(`SCORE`)
FROM `jbit`
WHERE `htno` = '$id'
`SCORE` > 0;
Though, I'd suggest you to change '$id' to just $id if the column type of htno is INTEGER.
SELECT SUM(score) FROM <tablename> WHERE score != -1
Suppose I have the following table:
id flag date
1 1 2012-01-01
2 1 2012-02-01
3 1 2012-03-01
4 0 2012-04-01
5 0 2012-05-01
6 0 2012-06-01
Is there anyway I can get maximum date for rows with flag as 1 and minimum date for row with flag as 0 using the same query?
Edited:
I want the result to looks something like this:
max min
2012-03-01 2012-04-01
Thank you.
You can try something like
SELECT MAX(CASE WHEN FLAG = 1 THEN Date ELSE NULL END) MaxFlag1,
MIN(CASE WHEN FLAG = 0 THEN Date ELSE NULL END) MinFlag0
FROM [Table]
try
select flag,
case when flag = 1 then max([date])
when flag = 0 then min([date])
end as date
from your_table
group by flag
You can use a UNION (ALL) to combine the results of two statements. The only restriction on those statements is that they both return the same amount and same type of columns.
Applied to your example, this would become
SELECT MAX(date) FROM YourTable WHERE flag = 1
UNION ALL SELECT MIN(date) FROM YourTable WHERE flag = 0