Sum if date exists for this month - sql

I'am trying to work out the AmountRequested for all completed cases for this month.
I'am using SQL 2008
If there is a date in the column DateCompleted for this month then give me the sum of AmountRequested for those columns
Here is my code it keeps falling over saying "Operand type clash: date is incompatible with int"
SUM(case WHEN CONVERT(DATE,pm.DateCompleted,103)
= MONTH(GETDATE()) AND YEAR(pm.DateCompleted) = YEAR(GETDATE())
THEN pm.AmountRequested
ELSE 0 end) AS [LoanAmount]

CONVERT(DATE,pm.DateCompleted,103) = MONTH(GETDATE())
Here you are trying to compare date with integer so the error.
Ex.
CONVERT(DATE,pm.DateCompleted,103) may return '2016-10-11' and MONTH(GETDATE()) will return 10. You trying to equate '2016-10-11' = 10 so the error is generated
Why not use the same MONTH function for your column also
Sum(CASE
WHEN Month(pm.DateCompleted) = Month(Getdate())
AND Year(pm.DateCompleted) = Year(Getdate()) THEN pm.AmountRequested
ELSE 0
END) AS [LoanAmount]

Related

DB2 use of labeled duration not valid with multiple date intervals

I'm trying to refactor a MySQL query to run on DB2/iSeries and I'm getting the error Use of labeled duration not valid.
Looking at the documentation I feel like the usage below should be working.
Am I missing something?
SELECT
IFNULL(SUM(CASE WHEN CURDATE() BETWEEN n.start_date AND n.expire_date
THEN 1 ELSE 0 END), 0) AS current,
IFNULL(SUM(CASE WHEN CURDATE() - 365 DAY BETWEEN n.start_date AND n.expire_date
THEN 1 ELSE 0 END), 0) AS prior,
IFNULL(SUM(CASE WHEN '2018-12-31' - 7 DAY BETWEEN n.start_date AND n.expire_date
THEN 1 ELSE 0 END), 0) AS full
FROM salesnumbers;
The issue is likely your date intervals. Try using CURRENT DATE instead of CURDATE(). Also, you may list date intervals +/- some amount directly in DB2.
SELECT
COUNT(CASE WHEN CURRENT DATE BETWEEN n.start_date AND n.expire_date
THEN 1 END) AS current,
COUNT(CASE WHEN CURRENT DATE - 1 YEAR BETWEEN n.start_date AND n.expire_date
THEN 1 END) AS prior,
COUNT(CASE WHEN DATE('2018-12-31') - 7 DAY BETWEEN n.start_date AND n.expire_date
THEN 1 END) AS full
FROM salesnumbers;
Note that I replaced your conditional sums with conditional counts. This leaves the code slightly more terse, because we don't have to spell out an explicit ELSE condition (the default being NULL).

SUM with CASE and LEFT Syntax

With the code below, I'm trying to get the total Net_Value and Eaches for the three years 2015-2017. However I cannot, for the life of me figure out what the syntax error is
[Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near '2015'.
SELECT
SUM(CASE WHEN LEFT(STR(FP_Based_on_Comm_Date),4) = '2015' THEN Net_Value ELSE 0 END) AS 2015Sales,
SUM(CASE WHEN LEFT(STR(FP_Based_on_Comm_Date),4) = '2016' THEN Net_Value ELSE 0 END) AS 2016Sales,
SUM(CASE WHEN LEFT(STR(FP_Based_on_Comm_Date),4) = '2017' THEN Net_Value ELSE 0 END) AS 2017Sales,
SUM(CASE WHEN LEFT(STR(FP_Based_on_Comm_Date),4) = '2015' THEN Eaches ELSE 0 END) AS 2015Eaches,
SUM(CASE WHEN LEFT(STR(FP_Based_on_Comm_Date),4) = '2016' THEN Eaches ELSE 0 END) AS 2016Eaches,
SUM(CASE WHEN LEFT(STR(FP_Based_on_Comm_Date),4) = '2017' THEN Eaches ELSE 0 END) AS 2017Eaches
FROM DB;
The field itself is a numerical field, and am trying to use it as a string. Those values typically come through as 2015001 where the format is yyyymmm
The problem are the column alias you're using - a valid SQL Server / T-SQL identifier cannot begin with a number.
If you really want to keep this, you must put these in square brackets like this:
SELECT
SUM(CASE
WHEN LEFT(FP_Based_on_Comm_Date, 4) = 2015
THEN Net_Value
ELSE 0
END) AS [2015Sales],
and you need to apply this to all your column aliases.
Assuming your dates are stored as dates, why aren't you using the YEAR() function. I would also suggest putting the year at the end of the column alias.
SELECT SUM(CASE WHEN YEAR(FP_Based_on_Comm_Date) = 2015 THEN Net_Value ELSE 0 END) AS Sales_2015
I think it is bad idea to use identifiers that need to be escaped -- they just make queries harder to write and to read.
And, if you just care about the results, why not use GROUP BY:
select year(FP_Based_on_Comm_Date), sum(net_value) as sales, sum(eaches) as eaches
from db
group by year(FP_Based_on_Comm_Date),
order by year(FP_Based_on_Comm_Date);

SQLite strftime function issue with timezone

I have following table structure where are saved some dates:
I tried to group results by hours using strftime sqlite function, but i found the problem if datetime value is stored like:
2015-01-21 11:49:16CET
In this case is value not converted. But if i erased "CET" value to have something like this:
2015-01-21 10:44:09
I got the correct result.
Query:
SELECT strftime('%H', dc.date) as hr,
COUNT(*) AS DIALS_CNT,
SUM(CASE WHEN dc.call_result = 'APPT' THEN 1 ELSE 0 END) AS APPT_CNT,
SUM(CASE WHEN dc.call_result = 'CONV_NO_APPT' THEN 1 ELSE 0 END) AS CONVERS_CNT,
SUM(CASE WHEN dc.call_result = 'CANNOT_REACH' THEN 1 ELSE 0 END) AS CANNOT_REACH_CNT
FROM dialed_calls dc
GROUP BY strftime('%H', dc.date);
Should i remove timezone from date column or how can i solve it please?
Many thanks for any advice.

Dynamically Creating Column Counts Per Day of Month

OK, so I have this query to find number of types per day like below. (Courtesy of MarkD here Counting Items/Rows in DB as Columns Grouped by Another Column)
select type,
sum(case when MyDate = '' then 1 else 0 end) as "10/1",
sum(case when MyDate = '' then 1 else 0 end) as "10/2
...etc
from MyTabe
group by type
However, I want to dynamically create date columns and have the count generated for each column, otherwise I would end up manually adding a new column for each day of the month.
I guess the best way to get the output I wanted was to do the following:
define startDate = to_date('2013-10-01', 'yyyy-mm-dd')
select type,
sum(case when MyDate = &&startDate then 1 else 0 end) as "1"
, sum(case when MyDate = &&startDate +1 then 1 else 0 end) as "2"
, sum(case when MyDate = &&startDate +2 then 1 else 0 end) as "3"
, sum(case when MyDate = &&startDate +3 then 1 else 0 end) as "4"
...etc for as many days of current month I am running
, sum(case when MyDate = &&startDate +29 then 1 else 0 end) as "30"
, sum(case when MyDate = &&startDate +30 then 1 else 0 end) as "31"--This would be commented out for Nov
from MyTabe
group by type
order by type
;
This way if I want to run this for Nov, Dec, Jan, and so on, I can just change the variable at the top and run the query. This is what I was looking for; however, I still wonder if it would be possible to generate the columns dynamically, but the more I look at it, the more I think that would require a pivot table.

sql server 2008 r2: case statement in where clause for current fiscal year

I'm trying to write code where I want to only see requests from the current fiscal year. Our fiscal year starts July 1st and ends June 30th
But when I write the following code
SELECT
group_name
,SUM(CASE WHEN status = 'HOLD'THEN 1 ELSE 0 END) AS HOLD
,SUM(CASE WHEN status = 'CL'THEN 1 ELSE 0 END) AS CL
,SUM(CASE WHEN status = 'OP'THEN 1 ELSE 0 END) AS OP
FROM dbo.View_Request
WHERE CASE WHEN datepart(mm, GetDate()) > 6 THEN /*It is past June in this year*/
datepart(mm,dateadd(second,open_date,'19700101')) >= 7
AND datepart(yy,dateadd(second,open_date,'19700101')) = datepart(yy, GetDate())
ELSE /*It is June 30th or earlier in the year*/
CASE WHEN datepart(mm,dateadd(second,open_date,'19700101')) <= 6 THEN
datepart(yy,dateadd(second,open_date,'19700101')) = datepart(yy, GetDate())
ELSE
datepart(yy,dateadd(second,open_date,'19700101')) = datepart(yy, GetDate())-1
END
END
GROUP BY group_name
I get the vague error message:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near '>'.
How do I fix this code to only examine entries from the current fiscal year
Your first case is a bit funny:
CASE WHEN datepart(mm, GetDate()) > 6 THEN /*It is past June in this year*/
datepart(mm,dateadd(second,open_date,'19700101')) >= 7
AND datepart(yy,dateadd(second,open_date,'19700101')) = datepart(yy, GetDate())
See that second and third line?? What are they??
Your CASE statement should always be:
CASE WHEN (condition) THEN (return value)
WHEN (condition 2) THEN (return value 2)
...
ELSE (return value x)
END
Those two lines really don't fit in there - after the WHEN keyword, you should have just a simple expression that returns a single value - not two lines of code.....