0

I am trying to find a way to SUM data in one column of my datbase on a SELECT statement if the Date column has the same day. My table would have three columns: Ticker, Mentions and Data. Ticker for this example would be BTC, there might be 10 rows and those ten rows have different mention counts(INT) for each one. Then the Date: 4 might have 2021-02-24, 5 might have 2021-02-25 and 1 might have 2021-02-23.

What I would like my SELECT statement to do is check that if the DAY is the same that it SUMs Mentions for that day and returns it as one INT. So in this case 3 INTs would be returned. A sum of the 24th, a sum of the 25th and then the single mention count of the 23.

Currently my SELECT looks like this:

SELECT Mentions 
FROM crypto_currency_trend_data 
WHERE Ticker = '" + ticker + "' AND YEAR(Date) = '" + current_year + "' 
ORDER BY Date DESC

I have read about SUM(IF()) but I am not really sure how to apply it in this situation. Any advice is greatly appreciated.

3
  • 1
    Use SUM(Mentions) and GROUP BY Date? Commented Feb 25, 2021 at 5:38
  • That's pretty basic SQL, am I missing something? Commented Feb 25, 2021 at 5:39
  • I have read about SUM(IF()) but I am not really sure how to apply it in this situation. SUM(IF({all conditions}, column_to_sum, 0)). But I'd recommend CASE instead of IF - it looks more clear: SUM( CASE WHEN {all conditions} THEN column_to_sum ELSE 0 END)). And according GROUP BY, of course. Commented Feb 25, 2021 at 5:45

2 Answers 2

1

Give this sql fiddle a try, Barmar basically stated the answer:

http://sqlfiddle.com/#!9/468645/1

SELECT 
  ticker,
  sum(Mentions) as NumMentions,
  date
FROM 
  crypto_currency_trend_data 
group by 
  ticker, 
  date
Sign up to request clarification or add additional context in comments.

Comments

0

I have tested a code with small exemplary data as follows:

import sqlite3
import pandas as pd
if __name__ == '__main__':
    conn = sqlite3.connect(":memory:")
    data = {"Ticker": ["BTC", "BTC", "BTC", "BTC", "BTC", "BTC", "BTC", "BTC", "BTC", "BTC"],
            "Mentions": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            "Date": ["2021-02-24", "2021-02-24", "2021-02-24", "2021-02-24",
                     "2021-02-25", "2021-02-25", "2021-02-25", "2021-02-25", "2021-02-25",
                     "2021-02-23"]            
            }
    
    df = pd.DataFrame(data)
    # write the tables
    df.to_sql("crypto_currency_trend_data", conn, index=False)
    ticker = "BTC"
    current_year = 2021
    sql = f"""SELECT COUNT(Date) AS COUNT, Date FROM crypto_currency_trend_data WHERE Ticker="{ticker}" AND strftime('%Y', Date)="{current_year}" GROUP BY Date"""
    df_count = pd.read_sql_query(sql, conn)
    print(df)
    print(df_count)

Results:

  Ticker  Mentions        Date
0    BTC         0  2021-02-24
1    BTC         1  2021-02-24
2    BTC         2  2021-02-24
3    BTC         3  2021-02-24
4    BTC         4  2021-02-25
5    BTC         5  2021-02-25
6    BTC         6  2021-02-25
7    BTC         7  2021-02-25
8    BTC         8  2021-02-25
9    BTC         9  2021-02-23
   COUNT        Date
0      1  2021-02-23
1      4  2021-02-24
2      5  2021-02-25

You will be able to adjust the provided according to your needs.

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.