I'm trying to improve a routine in my program where I'm counting the number of occurrences of the same date.
So maybe an example:
Time Other_stuff Entry
1332334357 .....
1332334367 .....
1332334386 .....
Till now I've used a statement like :
SELECT date(time, 'unixepoch') FROM reviewHistory
WHERE lastInterval <= 21 AND nextInterval > 21
Now I basically want to generate a histogram of the entries falling on the same date
2012-03-21 20
2012-03-20 13
2012-03-19 50
And so on. However I don't see an easy way to do this so I figured I would simply do a SELECT DISTINCT and store those values and then query for each distinct entry with the conditionals and check the row count. It'd save me processing time and I/O access time however I haven't been able to find a statement that accomplishes this. I thought something like this would work but it doesn't. Example:
SELECT time FROM reviewHistory WHERE
(SELECT date(time, 'unixepoch') FROM reviewHistory
WHERE lastInterval <= 21 AND nextInterval > 21) = "2012-03-21"
It doesn't error but it also doesn't return anything and I feel pretty sure that this is something that I should be able to accomplish in a few SQLite statements and not have to manage application side.
UPDATE: Lol it just came to me after I posted it. Though I still want to believe there's a more graceful way to accomplish the tallying problem in SQL.
SELECT time FROM
(SELECT date(time, 'unixepoch') AS time
FROM reviewHistory WHERE lastInterval <= 21 AND nextInterval > 21)
WHERE time = "2012-03-21"
I'm not sure if I understand your question, but to get all dates and the counts, one query should be sufficient:
SELECT date(time, 'unixepoch'), COUNT(*)
FROM reviewHistory
WHERE lastInterval <= 21 AND nextInterval > 21
GROUP BY date(time, 'unixepoch')
Related
I'm using SQLite to deal with tons of data (like 100gb of data).
I need to seach the value of one column in other table in the fastest way possible.
For example, I need to find the following values of Table 1
[COD]
C62
K801
And then find them in Table 2:
[COD_2]
C60-C63
K80-K81
My desired result is something like:
[COD_1] [COD_2]
C62 C60-C63
K801 K80-K81
Since I have a lot of data, it is inefficient to do something like:
SELECT *
FROM TABLE_1, TABLE_2
WHERE COD_1 LIKE '%' || COD_2 || '%';
Instead, I was trying to do this:
SELECT *
FROM TABLE_1
WHERE COD_1 IN (SELECT COD_2 FROM TABLE_2);
Of course that this doesn't result because the codes are not exactly the sames. Is there a way to search for similar values of one column (something like the LIKE operator) in other table by using IN? Or other way that doesn't cross TABLE_1 and TABLE_2?
Thank you!!!
useful to me.
Based on the small data set shown, and my presumed answer to #Shawn's question (K801 is a typo and is meant to be K80 or K81) I assume the following problem description:
Find a row in COD_2 such that the value in COD_1 is between {value1}-{value2} in COD_2; the - being significant and dependable.
I cannot speak to speed, but I would approach it this way:
SELECT value1, value2
from COD_1,COD_2
where value1 between substr(value2,1,instr(value2,'-')-1) and substr(value2,instr(value2,'-')+1)
The thought being: split the value from COD-2 into a "start" and an "end" value.
I am trying to get previous years in SQLite where clause using sql clause. It did not work. Could somebody help me?
Here is what I am trying to get:
select * from Spending where strftime('%Y',Spending_DT )<= strftime('%Y', date('now'))-3;
it is the number that causes the problem. It seems to me that substract 3 works in select clause such as:
select strftime('%Y', date('now'))-3;
output=2013
but it did not work when the same phrase using in where clause
many many thanks
Jing
Try casting both sides of your comparator like so:
Example
select * from test;
spending_Dt
-----------
2012-03-03
2013-03-03
2014-03-03
2015-03-03
2016-03-03
Query
select strftime('%Y', spending_dt) as years
from test
where cast(strftime('%Y', spending_dt) as int) <= cast(strftime('%Y', date('now')) as int)-3;
Result
years
----------
2012
2013
I have a database table containing a row of dates in DateTime format. what I need to do is get all the distinct weeks numbers of the available dates. for example if I have the following dates:
03-JAN-13
04-JAN-13
09-JAN-13
the sql query would give me the following weeks numbers: 1 and 2.
PS: afterwards I will put these values in a dropdownlist (no problem with that step).
So can anybody tell me how to do it?
You did not specify what RDBMS you are using but you could use the following to get week numbers.
SQL Server you would use DatePart():
select distinct datepart(week, dates) WeekNo
from yourtable
See SQL Fiddle with Demo
In MySQL you could use Week():
select distinct week(dates)
from yourtable
See SQL Fiddle with Demo
In Oracle, you could use to_char():
select distinct to_char(dates, 'W') WeekNo
from yourtable
See SQL Fiddle with Demo
In PostgreSQL you can use the following:
select distinct extract(WEEK from dates) WeekNO
from yourtable
See SQL Fiddle with Demo
Replace the yourtable with your table name and dates with your date column.
Edit #1: If you are using MS Access then you can still use DatePart() (this was tested in MS Access 2003):
SELECT distinct datepart("ww", dates) as WeekNo
FROM yourtable;
In your on load event, or wherever you want, you will put this VBA code
Me!myCombo.RowSource = "yourquerytexthere;"
(Or whatever query you go with..probably whichever one you're using from your last question that Remou answered for you).
I think in this question, you already know what you want to query, you are just asking about setting the control.
That code is just
Me!myCombo.RowSource = "yourquerystring"
like
Me!myCombo.RowSource = "SELECT distinct datepart("ww", dates) FROM yourtable;"
Where Me!myCombo is the name of your combobox.
Annual ISO week# table - Oracle SQL query:
-- ISO_WK# --
SELECT mydate
, TRUNC(mydate, 'w') wk_starts
, TRUNC(mydate, 'w') + 7 - 1/86400 wk_ends
, TO_NUMBER (TO_CHAR (mydate, 'IW')) ISO_WK#
FROM
(
SELECT TRUNC(SYSDATE, 'YEAR')-1 + LEVEL AS mydate
FROM dual
CONNECT BY LEVEL <=
(-- First day of curr year - first day of past year --
SELECT TRUNC(SYSDATE, 'YEAR')-TRUNC(ADD_MONTHS (SYSDATE, -12), 'YEAR') "Num of Days"
FROM dual
)
)
/
Ok, I got the first part of my question answered, so here's the second part. :-) In a PLSQL query, I have criteria that looks like this:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(:DateEnd)
Now, I don't want to use :DateEnd itself -- I want to add 1 day so that when it compares the datetime to midnight, I get midnight of the next day. Unfortunately, when I do
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(:DateEnd + 1)
I get "ORA-06553: PLS-306: wrong number or types of arguments in call to 'CONVERT_DATE_TO_ID'". ":DateEnd + interval '1' day" gives me "ORA-30081: invalid data type for datetime/interval arithmetic" (where :DateEnd is bound to 31-MAY-2012). If I do "convert_date_to_id(add_months(:DateEnd, 1))", it works fine. Any thoughts? Thanks.
ETA: I should clarify that this is an SSRS 2008 R2 project, and DateBegin and DateEnd are defined in the report parameters as DateTime parameters. My current workaround involves setting the :DateEnd query parameter equal to the #DateEnd report parameter + 1, but I'm worried that someday I'll forget to document this properly and confuse the heck out of whomever's trying to maintain the report (and it might be me). I don't want to pass string parameters, as suggested before.
I'm thinking that the parameters being DateTime is root of the problem. Microsoft DateTime datatypes are way more granular than Oracle's in that it supports fractional seconds and Oracle DATE format does not (Oracle TIMESTAMP does however).
Since ADD_MONTHS just spits back whatever it's passed in DATE datatype (i.e. passed TIMESTAMP becomes DATE). So maybe you can convert the parameter and add the day that way:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate < ml.convert_date_to_id(CAST(:DateEnd as DATE)+1)
Alternatively, forget about conversions and date arithmetic on the parameters and subtract a day from the second clinicalDate:
where Doc3.clinicalDate >= ml.convert_date_to_id(:DateBegin)
and Doc3.clinicalDate - 1 < ml.convert_date_to_id(:DateEnd)
Assuming that ml.convert_date_to_id takes a DATE as an input parameter rather than a VARCHAR2 that represents a date, and assuming that the :DateEnd bind variable is a VARCHAR2, you would need something like
ml.convert_date_to_id( to_date( :DateEnd, 'DD-MON-YYYY' ) + 1 )
or
ml.convert_date_to_id( to_date( :DateEnd, 'DD-MON-YYYY' ) + interval '1' day )
Use to_date to convert the value. For example:
select &date + 1 from dual
Informing to_date('29052012','ddmmyyyy') works fine
Informing '29-may-2012' gives ORA-01722: invalid number
I was trying to do this:
SELECT SUM(price) as total FROM ticketLine WHERE dateRegistered > '2011-07-16 17:00:00'
The idea is (was) to get a total for all lines from 17:00 till now but I get a grand total for all lines. The where clause has no effect. I googled how to do a filter on a timestamp but got no relevant info.
Can you point out what I'm doing wrong here?
Sorry it took so long to answer this but here is the way to do it:
SELECT SUM(price) as total FROM ticketLine WHERE dateRegistered > DATETIME('2011-07-16 17:00:00')
Not sure(I have never used SQLite, but I worked with MySQL and SQL Server), but there shoud be some functions to work with dates and time
Try this
SELECT SUM(price) as total FROM ticketLine WHERE strftime('%s', dateRegistered) > strftime('%s', '2011-07-16 17:00:00')
I hope it is helpfull.