I am using SQLite with python-2.7 . In my SQLite database i contains a date field that stored date in dd-MM-yyyy format like that.
31/02/2018
30/02/2017
01/06/2018
How can i sort it ascending order.
try this query:
SELECT date(substr(`date_column`,7,4)||'-'||substr(`date_column`,4,2)||'-'||substr(`date_column`,1,2)) as text_date FROM `table` order by text_date asc
first get all rows with
connect = sqlite3.connect('datanase_file')
cursor = connect.execute('select date from table_name')
rows = cursor.fetchall()
then convert the dates to seconds from epoch so you can sort them
for row in rows:
t = time.strptime(row[0],'%d/%m/%Y')
converted_time = time.mktime(t)
then add converted_time column with type INT to your database.
then update your database with
connect.execute('''UPDATE table_name SET converted_time = ? WHERE date = ?''',
(converted_time, row[0]))
then you can sort it with any program.
Related
I have a date filter for my dashboard and depending on which end of month date the user selects the dashboard displays the appropriate values for the selected month and all of that works correctly. However I would like my date filter to be sorted in DESC order for the user so they do not have to scroll down to the bottom to get the most recent month. I have sorted the dataset in DESC order for the query tied to my dashboard, I have gone to the DATA tab within Power BI and sorted the dataset in DESC order but no matter what I try the filter will not sort in DESC order. Any help is much appreciated.
Unfortunately in page level filters there is no option to sort date to descending order.
One option is to use a Date slicer and sort it descending.
But if you cant have date slicer then a work around is there to implement this.
For this you need to create two calculated columns. One a duplicate column of your date field and another to sort this field.
SortCol = Table[EOM Date]*-1
EOMDateCopy=LEFT([EOM Date],LEN([EOM Date]))
Sort the EOMDateCopy column with SortCol and drag EOMDateCopy in your page level filter.
Best Regards,
Shani
My suggestion is to build your own Calendar table. In "Data" View, create a new Table by writing DAX similar like this:
Calendar =
VAR _MinDate = MIN('YourFactTable'[Date])
VAR _MaxDate = MAX('YourFactTable'[Date])
VAR _BaseCalendar = CALENDAR(_MinDate,_MaxDate)
RETURN
GENERATE(
_BaseCalendar,
VAR _BaseDate = [Date]
VAR _Year = YEAR(_BaseDate)
VAR _Month = MONTH(_BaseDate)
VAR _Quarter = QUARTER(_BaseDate)
VAR _Day = DAY(_BaseDate)
RETURN
ROW (
"Calendar Year", _Year,
"Calendar Month", _Month,
"Calendar Year-Month", _Year&"-"&IF(LEN(_Month)=1,"0"&_Month,_Month),
"Calendar Quarter", FORMAT(_Quarter,"\Q0"),
"Date Order", 9999 - (_Year+_Month+_Day)
)
)
"Date Order", 9999 - (_Year+_Month+_Day) create a descending order for your date.
Next, build a relationship between your Calendar table and your Fact table by relating the "Date" column of your Calendar table and your date column (version_no in my case) of your Fact table. Make sure these two columns have exactly the same date format.
Finally, click on the column and set "Sort by column" as Date Order
You will see your date filter is sorted in desc.
Please make sure your date field is in proper format and then try to sort.
If it is already in date format, please give a bit more details so that we can check.
Best Regards,
Shani
Am new to Sql Alchemy. I have a raw sql which i need to execute by passing bind parameters. Resulting rows from the query, i need to update a particular column value. How do i do this in the efficient way?
Below are the columns in my table metrics
TABLE
id,total,pass,fail,category,ref_id
query = "Select * from table where id in(select max(id) from table ...)"
sql = text(query)
result = db.engine.execute(sql, CATEGORY=category)
for row in result:
//update here
So i have this complex query, that i need to execute as an inline query. Let's say i get three rows from my query and i need to update ref_id for all the 3 rows with a values. How can i achieve this preferably bulk update.
Am using python 2.7,SQLAlchemy==0.9.9,SQLAlchemy-Utils==0.29.8
I have a date for when someone last bought something (lastbuy) and have an end date (lastday) column and I want to create a new column with this new value which will be a whole number. FYI, lastday and lastbuy are both Date/Time data types. I tried the following:
Column_name = DATEDIFF(lastday, lastbuy, day)
_______________________________________________
Column_name = COUNTX('Table_name', [lastday] - [lastbuy])
I'm looking for a way to filter and display records using static values from a "Select list" page item. I have created a bar chart in APEX 5.0 using the following query:
select to_char(to_date(time_stamp,'YYYY-MM-DD-HH24:MI:SS'),'YYYY-MM-DD-HH24:MI:SS') as label, col2 as value from table1 where :P5_NEW_1 = col1 ;
The time_stamp column of table1 is of datatype varchar2 in my database and contains the date values in the format YYYY-MM-DD-HH24:MI:SS
e.g., values like below are stored in the time_stamp column
2015-08-26-10:17:15
2015-08-26-13:17:15
2015-09-17-12:45:54
2015-09-17-14:12:32
2015-10-06-10:01:42
2015-10-06-11:01:28
2015-10-06-05:01:28
and so on...
I have added a "Select list" item named "interval" on my form that contains a pre-populated list of values like 1hr, 6hrs.
Now I want to modify the above query such that following should happen:
For the value of 1hr selected from the drop down list, the query should check the time_stamp column and display the records for the last one hour
(i.e., records which fall in range to_char(sysdate - 1/24 ,'YYYY-MM-DD-HH24:MI:SS') to to_char(sysdate ,'YYYY-MM-DD-HH24:MI:SS')
And for the value of 6hrs selected from the drop-down list, the query should display records for the last 6 hours.
(i.e., records which fall in range to_char(sysdate - 6/24 ,'YYYY-MM-DD-HH24:MI:SS' to to_char(sysdate ,'YYYY-MM-DD-HH24:MI:SS') )
How do I modify my SQL query to add the above condition in my current SQL query, using static values from page item select list?
Hi you can try this:
SELECT to_char(to_date(time_stamp,'YYYY-MM-DD-HH24:MI:SS'),'YYYY-MM-DD-HH24:MI:SS') AS label, col2 AS VALUE
FROM table1
WHERE :P5_NEW_1 = col1 AND
to_date(time_stamp,'yyyy-mm-dd hh24:mi:ss') BETWEEN
(SYSDATE - (to_number(:SELECT_LIST_ID,'99')/24)) AND SYSDATE;
Just make sure that the static values has a return value equal to hr itself
For example, if static value is equal to 1HR then return value should be 1
:SELECT_LIST_ID is the select list id which holds the static values for the interval.
Hope this helps.
I have a data file in this format:
I want the columns to be grouped by month in a pivot table. When I pivot the data a column for each day is being created.
df = ex.read_excel("C:\\ExportReport.xlsx", "ExportReport")
table = pd.pivot_table(df, values='Forecast Qty', rows='Part', cols='Due Date', aggfunc=np.sum, fill_value=0)
Is there a way to tell pandas to group the columns by month?
Need to have a field that calculates the month. If this is going to span multiple years, will need to combine into one field.
df['YYYY-MM'] = df['Due Date'].apply(lambda x: x.strftime("%Y-%m"))
Then try yours, but change to the monthly field...
table = pd.pivot_table(df, values='Forecast Qty', rows='Part', cols='YYYY-MM', aggfunc=np.sum, fill_value=0)