How convert Mon.YY to YearNN - datetime

I want to convert a string in Stata like Jan.01 (string, format %9s) to a datetime like 2001m1 (int, format %tm)
fecha ID tc_t3
Jan.01 1 575
Feb.01 1 575
Mar.01 1 580
Apr.01 1 .
May.01 1 605
Jun.01 1 615
Jul.01 1 630
```

This falls entirely within cases documented by help datetime
* Example generated by -dataex-. For more info, type help dataex
clear
input str6 fecha
"Jan.01"
"Feb.01"
"Mar.01"
"Apr.01"
"May.01"
"Jun.01"
"Jul.01"
end
* no dates in 1999 or earlier
. gen mdate = monthly(fecha, "M20Y")
* any dates in latter part of 20th century
. gen MDATE = monthly(fecha, "MY", 2025)
. format mdate MDATE %tm
. list
+--------------------------+
| fecha mdate MDATE |
|--------------------------|
1. | Jan.01 2001m1 2001m1 |
2. | Feb.01 2001m2 2001m2 |
3. | Mar.01 2001m3 2001m3 |
4. | Apr.01 2001m4 2001m4 |
5. | May.01 2001m5 2001m5 |
|--------------------------|
6. | Jun.01 2001m6 2001m6 |
7. | Jul.01 2001m7 2001m7 |
+--------------------------+

Related

How to query dates using sqlite between

Im trying to query a range between dates but
i have tried using the date datatype,store the values in the date column as string and also use the date function but not getting the desired results
CREATE TABLE PvcTable (
date TEXT NOT NULL,
Wardname TEXT NOT NULL,
Puname TEXT NOT NULL,
PvcReceived TEXT,
PRIMARY KEY (
date,
Wardname,
Puname
)
);
the expected result is when i query let say
SELECT * from pvctable
where date between '2019-1-1' and '2019-12-1'
order by WARDNAME
i should get all the records between jan - dec 2019, but instead i get
this.only 3 records return.
date Wardname Puname PvcReceived
2019-10 01Alagarno 010KANGARWAPRISCHII 58
2019-11 02Baga 001MILEFOUR 58
2019-12 02Baga 002DARBASHATA 58
It is important to make sure that the dates in the table have the proper format YYYY-MM-DD which is comparable.
From the sample data you posted I see that there is no DD part in the dates, which is fine if you don't need it, because YYYY-MM is also comparable.
But if there is no DD part then in your query you should not compare the date column with dates containing this part, but with dates in the format YYYY-MM.
So change to this:
SELECT * from pvctable
where date between '2019-01' and '2019-12'
order by WARDNAME
See the demo.
Results:
| date | Wardname | Puname | PvcReceived |
| ------- | ---------- | ------------------- | ----------- |
| 2019-01 | 01Alagarno | 001ALAGARNOPRISCH | 58 |
| 2019-10 | 01Alagarno | 010KANGARWAPRISCHII | 58 |
| 2019-11 | 02Baga | 001MILEFOUR | 58 |
| 2019-12 | 02Baga | 002DARBASHATA | 58 |

how to combine R data frames with non-exact criteria (greater/less than condition)

I have to combine two R data frames which have trade and quote information. Like a join, but based on a timestamp in seconds. I need to match each trade with the most recent quote. There are many more quotes than trades.
I have this table with stock quotes. The Timestamp is in seconds:
+--------+-----------+-------+-------+
| Symbol | Timestamp | bid | ask |
+--------+-----------+-------+-------+
| IBM | 10 | 132 | 133 |
| IBM | 20 | 132.5 | 133.3 |
| IBM | 30 | 132.6 | 132.7 |
+--------+-----------+-------+-------+
And these are trades:
+--------+-----------+----------+-------+
| Symbol | Timestamp | quantity | price |
+--------+-----------+----------+-------+
| IBM | 25 | 100 | 132.5 |
| IBM | 31 | 80 | 132.7 |
+--------+-----------+----------+-------+
I think a native R function or dplyr could do it - I've used both for basic purposes but not sure how to proceed here. Any ideas?
So the trade at 25 seconds should match with the quote at 20 seconds, and the trade #31 matches the quote #30, like this:
+--------+-----------+----------+-------+-------+-------+
| Symbol | Timestamp | quantity | price | bid | ask |
+--------+-----------+----------+-------+-------+-------+
| IBM | 25 | 100 | 132.5 | 132.5 | 133.3 |
| IBM | 31 | 80 | 132.7 | 132.6 | 132.7 |
+--------+-----------+----------+-------+-------+-------+
Consider merging on a calculated field by increments of 10. Specifically, calculate a column for multiples of 10 in both datasets, and merge on that field with Symbol.
Below transform and within are used to assign and de-assign the helper field, mult10. In this use case, both base functions are interchangeable:
final_df <- transform(merge(within(quotes, mult10 = floor(Timestamp / 10) * 10),
within(trades, mult10 = floor(Timestamp / 10) * 10),
by=c("Symbol", "mult10"),
multi10 = NULL)
Now if the 10 multiple does not suffice for your needs, adjust to level you require such as 15, 5, 2, etc.
within(quotes, mult10 <- floor(Timestamp / 15) * 15)
within(quotes, mult10 <- floor(Timestamp / 5) * 5)
within(quotes, mult10 <- floor(Timestamp / 2) * 2)
Even more, you may need to use the reverse, floor or ceiling for both data sets respectively to calculate highest multiple of quote's Timestamp and lowest multiple of trade's Timestamp:
within(quotes, mult10 <- ceiling(Timestamp / 15) * 15)
within(trades, mult10 <- floor(Timestamp / 5) * 5)

SQLITE order by numeric and not alphabetic

When I order my database SQLITE by Classement I have this :
Classement | Nom
1 | clem
10 | caro
11 | flo
12 | raph
2 | prisc
3 | karim
4 | prout
I would like to get :
Classement | Nom
1 | clem
2 | prisc
3 | karim
4 | prout
10 | caro
11 | flo
12 | raph
Here is my code :
SELECT t.Classement
FROM tableau t
WHERE 1 = (SELECT 1 + COUNT (*) FROM tableau t2 WHERE t2.Classement < t.Classement OR ( t2.Classement == t.Classement AND t2.Nom < t.Nom ))
Can anyone help me ?
Thank you!
I guess column Classement is not an integer but character. So try this:
SELECT * FROM tableau ORDER BY cast(Classement as integer);
You get alphabetic order if the values are strings.
To change the table so that all Classement values are numbers, ensure that the column type is not a text type, and use this:
UPDATE tableau SET Classement = CAST(Classement AS NUMBER);

MS Access date diff from one row and previous row of different fields

I need to find the difference in weeks from marked Previous End Date and next Start Date.
Visit Type | Start Date | End Date | Weeks since previous visit
------------+---------------+---------------+------------------------------
Check-Up | 19-Jan-15 | 19-Feb-15 |
Check-Up | 27-Jan-15 | 27-Jan-15 | xxx
Check-Up | 22-Jan-15 | 22-Feb-15 |
Check-Up | 21-Jan-15 | 21-Jan-1 |
I need to find the diff bw 19 feb of End date and 27 jan of Start date . A simple datediff is not working. can someone help now ?

use gather of a column with Group By in sql

I have a table in sql that store statistics of visits, and have below columns:
referredURL
searchedWord(words that lead the user to this page)
Hittime
I want a report of this table with below column:
- refferedURL
- recenReferDate
- allSearchedWors
select ReferredURL , MAX (HitTime) as 'Recent refer date'
from statTbl
GROUP BY ReferredURL
I have problem for gathering allSearchedWords, how can I gather all of the searchedWord of a group and use it as a column?
If you're using SQL Server try
SELECT t.ReferredURL,
MAX (t.HitTime) 'Recent refer date',
(
STUFF((SELECT DISTINCT ',' + SearchedWord
FROM statTbl
WHERE ReferredURL = t.ReferredURL
FOR XML PATH('')) , 1 , 1 , '' )
) 'Searched Words'
FROM statTbl t
GROUP BY t.ReferredURL;
If you need all words to be present even duplicates ditch DISTINCT from the subquery.
Here is SQLFiddle example for SQL Server
If it is MySql then
SELECT t.ReferredURL,
MAX(t.HitTime) 'Recent refer date',
GROUP_CONCAT(DISTINCT(SearchedWord)) 'Searched Words'
FROM statTbl t
GROUP BY t.ReferredURL;
Here is SQLFiddle example for MySql
Sample data
| REFERREDURL | HITTIME | SEARCHEDWORD |
-----------------------------------------------------------
| url1 | May, 22 2013 00:00:00+0000 | apple |
| url1 | May, 22 2013 12:00:00+0000 | banana |
| url1 | May, 22 2013 18:00:00+0000 | pear |
| url1 | May, 22 2013 18:05:00+0000 | apple |
| url2 | May, 22 2013 23:00:00+0000 | apple |
Sample output
| REFERREDURL | RECENT REFER DATE | SEARCHED WORDS |
----------------------------------------------------------------
| url1 | May, 22 2013 18:05:00+0000 | apple,banana,pear |
| url2 | May, 22 2013 23:00:00+0000 | apple |

Resources