Re: [sqlite] How to use CASE statement to SUM() some numbers

2019-11-15 Thread Jim Morris
Maybe something like:

CREATE VIEW "Sum of Expenses Between two Dates" AS 
SELECT Date,
sum( CASE
  WHEN Date BETWEEN date('now', '-1 months') AND date('2019-11-04', 
'-1 days') THEN Expense
  ELSE 0
  END) as 'Sum of Expenses:'
  FROM Expenses;

On 11/15/2019 12:22 PM, David Raymond wrote:
> So why do you need a case? What will not work with the simple:
>
> select sum(Expense)
> from Expenses
> where Date between date('now', '-1 months') and date('2019-11-04', '-1 days');
>
> ___
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] How to use CASE statement to SUM() some numbers

2019-11-15 Thread David Raymond
So why do you need a case? What will not work with the simple:

select sum(Expense)
from Expenses
where Date between date('now', '-1 months') and date('2019-11-04', '-1 days');

___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] How to use CASE statement to SUM() some numbers

2019-11-15 Thread Csanyi Pal

Hi,

I have a simple database, the 'Incomes_Expenses.db' on my system.

It's SQL is here:

--
-- File generated with SQLiteStudio v3.2.1 on P nov. 15 20:39:18 2019
--
-- Text encoding used: UTF-8
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;

-- Table: Expenses
CREATE TABLE Expenses (Id INTEGER PRIMARY KEY, Date DATE, Expense REAL, 
Currency TEXT);
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (1, 
'2019-10-15', 421.35, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (2, 
'2019-10-18', 560.5, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (3, 
'2019-10-19', 37.0, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (4, 
'2019-10-20', 632.0, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (5, 
'2019-10-21', 124.5, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (6, 
'2019-10-23', 1200.0, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (7, 
'2019-10-31', 278.43, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (8, 
'2019-11-01', 250.3, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (9, 
'2019-11-02', 429.72, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (10, 
'2019-11-03', 310.11, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (11, 
'2019-11-04', 197.0, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (12, 
'2019-11-05', 257.0, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (13, 
'2019-11-06', 4230.0, '$');
INSERT INTO Expenses (Id, Date, Expense, Currency) VALUES (14, 
'2019-11-08', 452.38, '$');


-- Table: Incomes
CREATE TABLE Incomes (Id INTEGER PRIMARY KEY, Date DATE, Income REAL, 
Currency TEXT);
INSERT INTO Incomes (Id, Date, Income, Currency) VALUES (1, 
'2019-11-04', 1573.0, 'USD');


-- View: Sum of Expenses Beteen two Dates
CREATE VIEW "Sum of Expenses Beteen two Dates" AS SELECT Date,
   CASE
 WHEN Date BETWEEN date('now', '-1 months') AND date('2019-11-04', 
'-1 days') THEN SUM(Expense)

 ELSE 'Sorry, it is not between the two dates.'
 END as 'Sum of Expenses:'
 FROM Expenses;

COMMIT TRANSACTION;
PRAGMA foreign_keys = on;



I wish to SUM some Expenses which meets some condition.
The condition follows.
The Date of an Expense must be between two dates.

The CASE statement should add to the SUM the value of an Expense if and 
only if the Date of an Expense is BETWEEN two dates.


The two dates are like this:
the 1. Date is in the previous month, and
the 2. Date is in the Incomes Table and is in this month minus 1 day.

Eg. 1. Date is say date('now', '-1 months')
and 2. Date is say date('2019-11-04').

So I tried with this Query to get the SUM of Expenses between 1. and 2. 
Date like this:



SELECT Date,
   CASE
 WHEN Date BETWEEN date('now', '-1 months') AND date('2019-11-04', 
'-1 days') THEN 'It is between the two dates.'

 ELSE 'Sorry, it is not between the two dates.'
 END as 'Information:'
 FROM Expenses


This shows only wether is a Date between two dates or is not.

The folloing code shows some results, but the resulted Sum is not valid:


sqlite3 Incomes_Expenses.db
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> SELECT * FROM 'Sum of Expenses Beteen two Dates';
2019-10-14|Sorry, it is not between the two dates.
2019-10-15|10132.7
2019-10-18|10132.7
2019-10-19|10132.7
2019-10-20|10132.7
2019-10-21|10132.7
2019-10-23|10132.7
2019-10-31|10132.7
2019-11-01|10132.7
2019-11-02|10132.7
2019-11-03|10132.7
2019-11-04|Sorry, it is not between the two dates.
2019-11-05|Sorry, it is not between the two dates.
2019-11-06|Sorry, it is not between the two dates.
2019-11-08|Sorry, it is not between the two dates.
sqlite>


It should gives this result: 4440,91 $ but not the 10132.7 $.


Can one uses the CASE statement to get a SUM of Expenses which Dates are 
between two given dates?


--
Best, Pali
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users