지출-액수.csv monthly sum by Item using R

# Install and load the necessary packages. Below two lines can be dropped after installing once.
install.packages("RSQLite")
install.packages("DBI")
library(RSQLite)
library(DBI)

# Define the path to your SQLite database
db_path <- "/home/jbyungrokim/CSV/CSV.db"

# Connect to the SQLite database
conn <- dbConnect(RSQLite::SQLite(), dbname = db_path)

# Adjust the query to handle the MM/DD/YY format
query <- "
SELECT 
    strftime('%Y-%m', '20' || substr(Date_Transaction, 7, 2) || '-' || substr(Date_Transaction, 1, 2) || '-' || substr(Date_Transaction, 4, 2)) AS YearMonth, 
    SUM(Pay_Amount) AS Total_Spent
FROM 
    '지출-액수_2024_08_17'
WHERE 
    Item = 'Grocery'
GROUP BY 
    YearMonth
ORDER BY 
    YearMonth;
"

# Execute the query and store the result in a data frame
monthly_sum <- dbGetQuery(conn, query)

# Print the result
print("Monthly Sum of Pay_Amount for Grocery Items by Year and Month:")
print(monthly_sum)

# Close the connection to the SQLite database
dbDisconnect(conn)

Leave a Reply

Your email address will not be published. Required fields are marked *