Date comparison on Kayako Query Language - KQL - querying

I am using Kayako Querying Language and trying to get the query to return all closed/resolved, closed/unresolved, and review tickets from a specified month. When I run the query, every ticket ever comes up, and it seems to ignore the date function I am using. What am I doing wrong?
SELECT COUNT(*) AS Total
FROM ‘Tickets'
WHERE ‘Tickets.Creation Date’ = month(June 2016) AND ‘Tickets.Status’ = ‘Closed’ OR ‘Tickets.Status’ = ‘Resolved’ OR ‘Tickets.Status’ = ‘Unresolved'
GROUP BY ‘Tickets.Status'
Thanks.

Replace the date comparison in your WHERE clause for this:
WHERE 'Tickets.Creation Date':Month = 6 and 'Tickets.Creation Date':Year = 2016
Be also careful with the quotes, you sometimes use ‘ and others '

Related

String concatenation query returning wrong results

Trying to automate this part of Teradata query:
c."currency" = CONCAT('FOO_', '2021') -- c."currency" = 'FOO_2021'
trying to replace the 2021 with the currently selected fiscal-year, so it is not hard-coded:
c."currency" = CONCAT('FOO_', c."fiscal-year")
c."fiscal-year" is of type VARCHAR(20).
Problem - the first query returns about 5 times more results than the second one, so I am doing something wrong, I guess. Tried with TO_CHAR(c."fiscal-year") and so on, did not succeed.

MS Access Query date range when last date not found

I have the following update query in MS Access 2013
UPDATE WXObs SET WXObs.SnowFlag = 1
WHERE (((WXObs.StationID) ="451409") And(
(WxObs.ObsDate) Between #1/3/2003# AND #3/29/2003# OR
(WxObs.ObsDate) Between #11/16/2003# AND #5/7/2004# OR
(WxObs.ObsDate) Between #10/30/2004# AND #4/30/2005#));
This works until the end date in the range is not found. For instance, if 5/7/2004 is not in the data set, then the update continues to the next end date, in this case 4/30/2005.
I would prefer it ended on the last date in the range. For instance, if the data ended on 4/21/2004, that would be last field updated between 11/16/ and 5/7/2004. The query would then continue to update again beginning on 10/30/2004.
I have tried < and <=
Thanks
You're missing some parentheses that are affecting the evaluation order, causing the behavior that you're reporting.
What you want is for each of the BETWEEN portions to be evaluated completely before the OR option is evaluated, and you need to make sure that evaluation is done by surrounding the BETWEEN expressions in parentheses to guarantee the evaluation order.
This should correct it (untested, as you've not provided the test data necessary to create a test case).
UPDATE WXObs SET WXObs.SnowFlag = 1
WHERE
(WXObs.StationID ="451409")
And
(
(WxObs.ObsDate Between #1/3/2003# AND #3/29/2003#) OR
(WxObs.ObsDate Between #11/16/2003# AND #5/7/2004#) OR
(WxObs.ObsDate Between #10/30/2004# AND #4/30/2005#)
);

How to count the number of rows with a date from a certain year in CodeIgniter?

I have the following query.
$query = $this->db->query('SELECT COUNT(*) FROM iplog.persons WHERE begin_date LIKE '2014%'');
I need to count the number of columns with a begin_date in the year 2014.
When I run this script I'm getting an error:
Parse error: syntax error, unexpected '2014' (T_LNUMBER) in C:\xampp\htdocs\iPlog2\application\controllers\stat.php on line 12
I was trying to change my CI script to
$query = $this->db->query('SELECT COUNT(*) FROM iplog.persons WHERE begin_date LIKE "2014%"');
but it caused an error.
You mean, count ROWS:
So for that, just count the number of rows you have based on a condition:
$year = '2014'
$this->db->from('iplog');
$this->db->like('begin_date', $year);
$query = $this->db->get();
$rowcount = $query->num_rows();
First, you have a simple typo regarding the use of single quotes. Your complete sql string should be double quoted so that your value-quoting can be single quoted.
Second, you are using inappropriate query logic. When you want to make a comparison on a DATE or DATETIME type column, you should NEVER be using LIKE. There are specific MYSQL functions dedicated to handling these types. In your case, you should be using YEAR() to isolate the year component of your begin_date values.
Resource: https://www.w3resource.com/mysql/date-and-time-functions/mysql-year-function.php
You could write the raw query like this: (COUNT(*) and COUNT(1) are equivalent)
$count = $this->db
->query("SELECT COUNT(1) FROM persons WHERE YEAR(begin_date) = 2014")
->row()
->COUNT;
Or if you want to employ Codeigniter methods to build the query:
$count = $this->db
->where("YEAR(begin_date) = 2014")
->count_all_results("persons");
You could return all of the values in all of the rows that qualify, but that would mean asking the database for values that you have no intention of using -- this is not best practice. I do not recommend the following:
$count = $this->db
->get_where('persons', 'YEAR(begin_date) = 2014')
->num_rows();
For this reason, you should not be generating a fully populated result set then calling num_rows() or count() when you have no intention of using the values in the result set.
Replace quotes like this :
$query = $this->db->query("SELECT COUNT(*) FROM iplog.persons WHERE begin_date LIKE '2014%'");
Double quote your entire query, then simple quote your LIKE criteria.

ORA-01861 literal does not match format string on SELECT statement

Good day.
I am executing a query and encountering:
ORA-01861 literal does not match format string error.
I executed this query and IT WORKED.
SELECT * FROM GCACC_OPERATION_DETAIL WHERE id_notice in (75078741)
AND id_analytical_center in (100000002)
AND interface_date = '2013-06-30'
AND generic_client = 'someGenClient'
AND document_class = 'DOCCLA0001'
AND accounting_tag_identifier = 1
AND generated_actual_acc_doc
IN (select id_accounting_document
from gcacc_accounting_document
where document_status = 'DOCSTA0001');
My other query is written below which DID NOT WORK.
SELECT * FROM GCACC_OPERATION_DETAIL WHERE id_notice IN (75078741)
AND id_analytical_center in (100000002)
AND generic_client = 'someGenClient'
AND document_class = 'DOCCLA0001'
AND accounting_tag_identifier = 1
AND interface_date = '2013-06-30'
AND ind_pending_process = 1
AND operation_type
IN (select cod_develop from gcacc_operation_type where ind_operation = 'B');
This is really weird because the error happens in the date part but I am writing the same syntax for the date part. I maybe missing something silly here and fresher eyes are needed. Thanks in advance!
I don't know what the specific problem is, but assuming "interface_date" is a DATE type, it is bad practice to use literals in a query for a date. This makes the assumption that the default NLS_DATE_FORMAT agrees with your date literal. That will come back to bite you. To ensure that your date constraint is portable, change to this:
AND interface_date = to_date('2013-06-30','YYYY-MM-DD')

LINQ to SQL selecting records and converting dates

I'm trying to select records from a table based on a date using Linq to SQL. Unfortunately the date is split across two tables - the Hours table has the day and the related JobTime table has the month and year in two columns.
I have the following query:
Dim qry = From h As Hour In ctx.Hours Where Convert.ToDateTime(h.day & "/" & h.JobTime.month & "/" & h.JobTime.year & " 00:00:00") > Convert.ToDateTime("01/01/2012 00:00:00")
This gives me the error "Arithmetic overflow error converting expression to data type datetime."
Looking at the SQL query in SQL server profiler, I see:
exec sp_executesql N'SELECT [t0].[JobTimeID], [t0].[day], [t0].[hours]
FROM [dbo].[tbl_pm_hours] AS [t0]
INNER JOIN [dbo].[tbl_pm_jobtimes] AS [t1] ON [t1].[JobTimeID] = [t0].[JobTimeID]
WHERE (CONVERT(DateTime,(((((CONVERT(NVarChar,[t0].[day])) + #p0) + (CONVERT(NVarChar,COALESCE([t1].[month],NULL)))) + #p1) + (CONVERT(NVarChar,COALESCE([t1].[year],NULL)))) + #p2)) > #p3',N'#p0 nvarchar(4000),#p1 nvarchar(4000),#p2 nvarchar(4000),#p3 datetime',#p0=N'/',#p1=N'/',#p2=N' 00:00:00',#p3='2012-01-31 00:00:00'
I can see that it's not passing in the date to search for correctly but I'm not sure how to correct it.
Can anyone please help?
Thanks,
Emma
The direct cause of the error may have to do with this issue.
As said there, the conversions you use are a very inefficient way to build a query. On top of that, it is inefficient because the expressions are not sargable. I.e. you are using a computed value from database columns in a comparison which disables the query analyzer to use indexes to jump to individual column values. So, you could try to fix the error by doctoring the direct cause, but I think it's better to rewrite the query in a way that only the single column values are used in comparions.
I've worked this out in C#:
var cfg = new DateTime(12,6,12);
int year = 12, month = 6, day = 13; // Try some more values here.
// Date from components > datetime value?
bool gt = (
year > cfg.Year || (
(year == cfg.Year && month > cfg.Month) || (
year == cfg.Year && month == cfg.Month && day > cfg.Day)
)
);
You see that it's not as straightforward as it may look at first, but it works. There are much more comparisons to work out, but I'm sure that the ability to use indexes will easily outweigh this.
A more straightforward, but not sargable, way is to use sortable dates, like 20120101 and compare those (as integers).