To get last month from user selected date - zend-db

I want to select last month from user. But it shows only data empty set. Doesn't return any data. What is wrong with the below code?
{$select->where("DATE_FORMAT(DATE(receive_dt),'%Y-%m') = DATE_SUB(DATE('".$v."'),INTERVAL -1 MONTH)");}

This is ok for about question want.so i updated it like this....
$select->where("DATE_FORMAT(DATE(receive_dt),'%Y-%m') = DATE_FORMAT(DATE(DATE_SUB(DATE('" . $v . "-1'),INTERVAL 1 MONTH)),'%Y-%m')");

Related

Carefully select and retrieve one from a large set of data

I don't know what to do when I want to retrieve one item in each column from data like the following.
|name|power|
test1|15
test1|120
test1|40
test2|50
test2|80
test2|100
test3|1
test3|4
test3|10
The results I want.
|name|power|
test1|120
test2|100
test3|10
What I tried.
pickup_one = (
beaconPickUp_three
.withColumn('r_max_date', f.max('power').over(w.partitionBy()))
.filter(f.col('r_max_date') == f.col('power'))
)
or
PickUp_one = beaconPickUp_three.groupBy(f.col("name")).agg({"power": "max"})
I need your help.

How to Fetch Month Part if Date is in mmYYY format(mm-->Month,YYYY--->Year)?

I need to fetch 'month' from one column in which date stores in mmYYYY(032019) format.I need to fetch the month column and if months value is in 01,02,03(Jan,Feb,march) then ,i need to generate new column as 2018-19.if value is 072019 then need to generate new value as 2019-20. Im trying below code but not getting desired result. Can someone please guide me how should i implement this.
CASE WHEN month(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy'))) IN (01,02,03) THEN
CONCAT(CONCAT(year(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy'))))-1,'-'),
substr(year(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy')))),3,4))
ELSE CONCAT(CONCAT(year(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy')))),'-'),
SUBSTR(year(to_date(from_unixtime(unix_timestamp(dt), 'dd-MM-yyyy'))))+1,3,4)) end
because of my dt column is not in 'dd-mm-yyyy' format so its not giving correct value due to some scenario i don't want to use direct sub-string function to fetch month part from dt. I want to fetch it using month function or any other function which can fetch month part and then accordingly generate output column based on month part.
Sample Data.
dt=012019
output : 2018-19
dt=022019
output : 2018-19
dt=032019
output : 2018-19
dt=042019
output : 2019-20
dt=052019
output : 2019-20
dt=062019
output : 2019-20
below code worked for me.
CASE WHEN month(to_date(from_unixtime(unix_timestamp(dt,'MMyyyy')))) IN (01,02,03)
THEN concat(concat(year(to_date(substr(from_unixtime(unix_timestamp(dt, 'MMyyyy')),1,10)))-1,'-')
,substr(year(to_date(from_unixtime(unix_timestamp(dt, 'MMyyyy')))),3,4))
ELSE concat(concat(year(to_date(from_unixtime(unix_timestamp(dt, 'MMyyyy')))),'-'),
substr(year(to_date(substr(from_unixtime(unix_timestamp(dt, 'MMyyyy')),1,10)))+1,3,4)) END

SQL-null value showing in the output when there is a value present in the database

I am extracting values from two different tables in the database. I should get only one line of output but instead I am getting two lines. One of the output line is perfect but the other line has one null value which clearly should not be null. My code is as below:
SELECT DISTINCT DEATH_RATE.COUNTRY_NAME,
DEATH_RATE.DATA_YEAR,
DEATH_RATE.DEATH_RATE_VALUE,
TIME_TO_EXPORT.EXPORT_VALUE
FROM DEATH_RATE, TIME_TO_EXPORT
WHERE TIME_TO_EXPORT.COUNTRY_NAME IN ('Belgium')
AND TIME_TO_EXPORT.COUNTRY_NAME = DEATH_RATE.COUNTRY_NAME
AND DEATH_RATE.DATA_YEAR = 2012
AND DEATH_RATE.DATA_YEAR = DEATH_RATE.DATA_YEAR ;
The output I am getting is as follows:
COUNTRY_NAME DATA_YEAR DEATH_RATE EXPORT_VALUE
1.Belgium 2012 423.5 9
2.Belgium 2012 423.5 null
First line is fine and only that should have been the output. Where is the second line and the null coming from ??
Thanks
The last condition of your query is a bit useless (the condition is always true when the value is not null):
DEATH_RATE.DATA_YEAR = DEATH_RATE.DATA_YEAR
I think you intended that :
TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR
(I assume that the fields have the same name)
In your select, you were thus getting all the TIME_TO_EXPORT records related to 'Belgium' whatever the value of DATA_YEAR.
So your query becomes :
SELECT DISTINCT DEATH_RATE.COUNTRY_NAME,
DEATH_RATE.DATA_YEAR,
DEATH_RATE.DEATH_RATE_VALUE,
TIME_TO_EXPORT.EXPORT_VALUE
FROM DEATH_RATE, TIME_TO_EXPORT
WHERE TIME_TO_EXPORT.COUNTRY_NAME IN ('Belgium')
AND TIME_TO_EXPORT.COUNTRY_NAME = DEATH_RATE.COUNTRY_NAME
AND DEATH_RATE.DATA_YEAR = 2012
AND TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR ;
Replace your last line with this:
AND TIME_TO_EXPORT.DATA_YEAR = DEATH_RATE.DATA_YEAR;
DEATH_RATE are TIME_TO_EXPORT are not properly join. and of course last line has no meaning.

Storing Select Query result in Variable Ruby on Rails

I wanted to know how we could store the result of my select query on a variable.
#ppt2 = Ppt.select('slide_name').where('id=?',4)
#ppt1 = Ppt.update_all({:time2=>#ppt2},['id like ?','1'])
Here, slide_name and time2 are both text attributes of the same table ppt.
What happens on the above execution is that the time2 field in id=1 gets the value "#ppt2" whereas I want it to get the value of slide_name from id=4 which does not get stored in #ppt1.
In other words, how do I store the value of the select query in #ppt2 so that it can be used in the next line?
Any help is appreciated.
Call the slide_name method on your first result.
#ppt2 = Ppt.select('slide_name').find(4).slide_name

How do I pass parameter as Date in vb.net web?

I have a mail webform where I have to show the user only (-1) one day behind messages, so how do pass (yesterday)date as parameter and retrieve the only records of one day back ?
This query is for 'ALL' messages, but I need to filter (yesterday) one day back messages and add a hyperlink or add in a dropdown ?
select MSG_SRNO,MSG_SUBJECT,MSG_ID,MSG_CHKD,
DOF_SENT,DOF_SEEN from MESSAGES_MAILBOX where USER=1234
In sql, try
SELECT DATEADD(day,-1, GETDATE());
This should work:
DateTime yesterday = DateTime.Now.AddDays(-1);
string query = string.Format("select MSG_SRNO,MSG_SUBJECT,MSG_ID,MSG_CHKD,DOF_SENT,DOF_SEEN from MESSAGES_MAILBOX where DATEPART(year,DOF_SENT) = {0} AND DATEPART(month,DOF_SENT) = {1} AND DATEPART(day,DOF_SENT) = {2}",yesterday.Year,yesterday.Month, yesterday.Day);