Getting Month from date field - KDB - kdb

I have sales data stored in a database. The sales_date field contains the date on which the sale took place. I want to extract this data grouped by month, so that I will get the aggregate data for Jan, Feb, etc. Is there a way I can do this without having to extract the entire data and then doing it manually?

Something like the following should work. If the data is partitioned on disk, remember to include the partition in the where clause.
q)tbl:([]dt:20?(2013.01.01;2013.02.01;2013.01.03);sales:20?100000)
q)select sum sales by `month$dt from tbl
dt | sales
-------| ------
2013.01| 701075
2013.02| 298200
q)select avg sales by `month$dt from tbl
dt | sales
-------| --------
2013.01| 50076.79
2013.02| 49700

You could also use the simpler form:
q)t:([] date:10?.z.Z)
q)select date.month from t
month
-------
2013.10
2013.08
2004.09
2010.03
...

Related

How to get date range between dates from the records in the same table?

I have a table with employment records. It has Employee code, status, and date when table was updated.
Like this:
Employee
Status
Date
001
termed
01/01/2020
001
rehired
02/02/2020
001
termed
03/03/2020
001
rehired
04/04/2021
Problem - I need to get period length when Employee was working for a company, and check if it was less than a year - then don't display that record.
There could be multiple hire-rehire cycles for each Employee. 10-20 is normal.
So, I'm thinking about two separate selects into two tables, and then looking for a closest date from hire in table 1, to termination in table 2. But it seems like overcomplicated idea.
Is there a better way?
Many approaches, but something like this could work:
SELECT
Employee,
SUM(DaysWorked)
FROM
(
SELECT
a1.employee,
IsNull(DateDiff(DD, a1.[Date],
(SELECT TOP 1 [Date] FROM aaa a2 WHERE a2.employee = a1.employee AND a2.[Date] > a1.[Date] and [status] <> 'termed' ORDER BY [Date] )
),DateDiff(DD, a1.[Date], getDate())) as DaysWorked
FROM
aaa a1
WHERE
[Status] = 'termed'
) Totals
GROUP BY
Totals.employee
HAVING SUM(DaysWorked) >= 365
Also using a CROSS JOIN is an option and perhaps more efficient. In this example, replace 'aaa' with the actual table name. The IsNull deals with an employee still working.

Apply parted attribute on a column without loosing sorted attribute on another column after splay in kdb

We have a table trade which is sorted on time column
q)trade:([] date:2020.04.05; time:asc 1000000?09:30:00.000 + til 21600000; sym:1000000?`GOOG`AMZN`FB; price:1000000?10.; size:1000000?1000000);
We are trying to partition it on date and apply sorted attribute on time column and parted attribute on sym column.
But, if we use .Q.en and set to partition the table then sorted attribute has to be lost.
trade:`sym xasc trade;
update `p#sym from `trade;
`:/parpath/2020.04.05/trade/ set .Q.en[`:/sympath/;trade];
And, if we use .Q.dpft then also sorted attribute is lost.
.Q.dpft[`:/parpath;2020.04.05;`sym;`trade];
How can we apply parted attribute on sym column with loosing sorted attribute of time column?
You can't have both `p on sym and `s on time in the general case. For example, this table
q)show t:([]sym:`AMZN`GOOG`AMZN;time:09:00:00 10:00:00 11:00:00)
sym time
-------------
AMZN 09:00:00
GOOG 10:00:00
AMZN 11:00:00
is sorted by time already, but you can't apply `p on sym. If you rearrange the rows so that `p is applicable on sym you will lose ascending order of the time values:
sym time
-------------
AMZN 09:00:00
AMZN 11:00:00
GOOG 10:00:00
or
sym time
-------------
GOOG 10:00:00
AMZN 09:00:00
AMZN 11:00:00
Having said that, parted and sorted at the same time is never a requirement if all you want is save a table as part of a partitioned table on disk, for example. You just need to make sure sym is parted and time is sorted within each sym. In other words `sym`time xasc t is enough.

powerbi single date filter on temporal table (effective date from and to)

I have a table which holds a history of data changes with a date_from and date_to field.
I also have a dates table (a record for each date).
example
emp table
----------------------------------------
empnum name date_from date_to
----------------------------------------
1 alan 20200101 20200107
2 bill 20200101 20200407
3 cory 20200101 99991231
2 bill2 20200409 99991231
in a power bi report i want to see the records at a point in time, so i want a single date filter and when a date is selected it does a between date_from and date_to on the emp table
There is no relationship between the two tables. I have worked out how to do it by creating a third table unpivoting the data into a 2 column table of {emp_num,date} thus connecing the two table, and this works, but the data sizes are two big when you have 100k in the emp table etc, and im sure there is a better way.
Is there a way i can filter the emp table in power bi using the date from the date slicer, when these have no relationship. I tried a visualtion filter on the table but you can only hardcode dates in these not reference a table. (that i can see anyway)
thanks

Postgres query to get date and total amount grouped by day

I have a table Logs with fields
Amount,date
I need to get the sum of amount and months grouped by each day
I need to migrate my sqlite code to postgresql but i find the code migration kind of hard.The sqlite code is as follows
SELECT SUM(amount),transaction_date FROM log WHERE user_id = 1 AND strftime('%Y', transaction_date) = '2019' GROUP BY strftime('%d', transaction_date);
What i need to is date and total amount grouped by day for the year 2019
You need extract() function instead of strftime() to get the year and cast transaction_date to date only if it is timestamp, if it is of data type date remove the casting ::date:
SELECT
SUM(amount),
transaction_date::date
FROM log WHERE user_id = 1 AND extract(year from transaction_date) = 2019
GROUP BY transaction_date::date

Sum with aggregate date postgresql

I have a table that has a column date and value, what I need is to sum a value showing just one date column.
Ex:
I have this:
date value
2018-01-01 150
2018-01-23 140
what I need:
date sum(value)
2018-01 290
Simple solution to get sums per month:
SELECT to_char(date, 'YYYY-MM') AS mon, sum(value) AS sum_value
FROM tbl
GROUP BY 1;
For large tables it's cheaper to group on date_trunc('month', date) instead.
Related:
Concatenate multiple result rows of one column into one, group by another column
Group and count events per time intervals, plus running total
How to get the date and time from timestamp in PostgreSQL select query?