Query Calculation at lower granularity - report

So I have a report that shows. Product, account type, account tier, balance, # of deposits.
My problem is that I need the tier to be applied to the individual accounts based off of their balance. So if your account is less than 99 then 'TIER 1', 100-199 then 'TIER 2' and > 200 then 'TIER 3'. The issue with this is the tier needs to be summarized. So the logic needs to be at account granularity but then just show the different combinations of product, account type and account tier. The balance also needs to be the total of everyone within that group.
example:
PRODUCT | TYPE | TIER | BALANCE | # OF DEPOSITS
A | 1 | T1 | $95,000 | 4
A | 1 | T2 | $80,000 | 10
A | 1 | T3 |$100,000 | 2
A | 2 | T1 | $50,000 | 45
A | 2 | T2 | $70,000 | 13
A | 2 | T3 |$250,000 | 100
B | 1 | T1 | $65,000 | 45
B | 1 | T2 | $15,000 | 25
etc...
I hope this at least shows what I am trying to accomplish with this report.
Please feel free to ask questions for more clarification.
Thank you in advance-
EDIT: This is the output I am currently getting.
PRODUCT | TYPE | TIER | BALANCE | # OF DEPOSITS
A | 1 | T1 | $1MIL | 100
A | 2 | T1 | $1.5MIL | 520
A | 3 | T6 | $2MIL | 650

I assuming the column 'Tier' is generated by you and not from the data source.
[Tier]
CAST
WHEN [Balance] < 99 THEN 'Tier1'
WHEN [Balance] between 100 and 199 THEN 'Tier2'
WHEN [Balance] > 200 THEN 'Tier3'
END
[Total Balance]
total([Balance] for [Product],[Type],[Tier])
Set the [Total Balance] Aggregate Function property to 'Calculated'.

If I understand your question correctly, a crosstab to aggregate the three criteria might be the best solution.
or
A clunky but effective way might be to create a two queries (I assume you are using report studio). The first to assign each account a tier, and in the next query you can add the total aggregate.

Related

How to get most recent data from DynamoDB for each primary partition key in PartiQL

inspired from this How to get most recent data from DynamoDB for each primary partition key?
I have a table in dynamodb. It stores account stats. It's possible that the account stats will be updated several times per day. So table records may look like:
+------------+--------------+-------+-------+
| account_id | record_id | views | stars |
+------------+--------------+-------+-------+
| 3 | 2019/03/16/1 | 29 | 3 |
+------------+--------------+-------+-------+
| 2 | 2019/03/16/2 | 130 | 21 |
+------------+--------------+-------+-------+
| 1 | 2019/03/16/3 | 12 | 2 |
+------------+--------------+-------+-------+
| 2 | 2019/03/16/1 | 57 | 12 |
+------------+--------------+-------+-------+
| 1 | 2019/03/16/2 | 8 | 2 |
+------------+--------------+-------+-------+
| 1 | 2019/03/16/1 | 3 | 0 |
+------------+--------------+-------+-------+
account_id is a primary partition key. record_id is a primary sort key
How I can get only latest records for each of the account_ids? So from the example above I expect to get:
+------------+--------------+-------+-------+
| account_id | record_id | views | stars |
+------------+--------------+-------+-------+
| 3 | 2019/03/16/1 | 29 | 3 |
+------------+--------------+-------+-------+
| 2 | 2019/03/16/2 | 130 | 21 |
+------------+--------------+-------+-------+
| 1 | 2019/03/16/3 | 12 | 2 |
+------------+--------------+-------+-------+
This data is convenient to use for a reporting purposes.
Execute the following PartiQL query for each account_id:
SELECT * FROM <Table> WHERE account_id='3' AND record_id > '2021/11' ORDER BY record_id DESC
PartiQL has no LIMIT keyword, so will return all matching records.
You can reduce overfetching by constraining the record_id date to the extent possible. If only the current date is of interest, for example, the sort key expression would be record_id > 2021/12/01.
As in the referenced example, you must execute one query for each account_id of interest. Batching operations are supported.

Kusto query to calculate number of users who experienced X number of crashes in Y days since release

My kusto data table records an event any time someone using the product experiences a crash. I want to calculate how many users experienced at least "X" number of crashes in "Y" number of days since the product was released.
So if the product was released on January 1st, and the table of crash events looks like:
| Date | User Id | Days Since Release |
| 1/1 | A | 0 |
| 1/1 | A | 0 |
| 1/1 | B | 0 |
| 1/2 | A | 1 |
| 1/3 | B | 2 |
| 1/4 | C | 3 |
Then the results would be:
| Days Since Release | Number of Crashes | Number of users |
| 0 | 1 | 2 | // Users A and B experienced 1 crash on 0th day
| 0 | 2 | 1 | // User A experienced 2 crashes on 0th day
| 1 | 1 | 2 | // Users A and B experienced at least 1 crash on 1st day
| 1 | 2 | 1 | // User A experienced at least 2 crashes on 1st day
| 1 | 3 | 1 | // User A experienced at least 3 crashes on 1st day
| 2 | 1 | 2 | // Users A and B experienced at least 1 crash on 2nd day
| 2 | 2 | 2 | // Users A and B experienced at least 2 crashes on 2nd day
| 2 | 3 | 1 | // User A experienced at least 3 crashes on 2nd day
| 3 | 1 | 3 | // Users A, B and C experienced at least 1 crash on 3rd day
| 3 | 2 | 2 | // Users A and B experienced at least 2 crashes on 3rd day
| 3 | 3 | 1 | // User A experienced at least 3 crashes on 3rd day
I tried to do this using the activity_engagement function but have not been able to perform the aggregations needed on number of crashes "X" and days since release "Y".
Please see the query below, I believe this is what you're looking for.
The idea is to expand the counts for each user from the day of the crash and until the total DaysSinceRelease (since if a crash happened on day #1, it should also be considered in all days following #1). Then, we also expand range(1, NumCrashes, 1) since if a user had 3 crashes, these should also be counted in the at-least-1 and at-least-2 bins.
let totalDaysSinceRelease = 3;
datatable(Date:datetime, UserId:string, DaysSinceRelease:long)
[
datetime(2020-01-01), "A", 0,
datetime(2020-01-01), "A", 0,
datetime(2020-01-01), "B", 0,
datetime(2020-01-02), "A", 1,
datetime(2020-01-03), "B", 2,
datetime(2020-01-04), "C", 3
]
| summarize NumCrashes = count() by UserId, DaysSinceRelease
| order by UserId asc, DaysSinceRelease asc
| extend NumCrashes = row_cumsum(NumCrashes, UserId != prev(UserId))
| extend DaysSinceRelease = range(DaysSinceRelease, totalDaysSinceRelease, 1)
| mv-expand DaysSinceRelease to typeof(long)
| summarize NumCrashes=max(NumCrashes) by UserId, DaysSinceRelease
| extend NumCrashes = range(1, NumCrashes, 1) | mv-expand NumCrashes to typeof(long)
| summarize dcount(UserId) by DaysSinceRelease, NumCrashes
| order by DaysSinceRelease asc, NumCrashes asc

SQLite query select between 2 records on same condition

i am trying to count (in minutes) between two records on 1 table and WHERE clause is the same condition.
_id | venue_id | act_time | status |
1 | 1 | 13:30 | 0 |
2 | 1 | 15:40 | 1 |
3 | 2 | 13:03 | 0 |
4 | 2 | 16:06 | 1 |
when i exec query like this :
SELECT _id, venue_id, status, (julianday(act_time IN (SELECT act_time FROM reports WHERE venue_id='1' AND status='1')) - julianday(act_time))*1440 AS duration FROM reports WHERE venue_id='1' AND status='0'
but, the result show the wrong calculation
Please help me what is the correct query for this problem?
so, if i count the duration between 15:40 - 13:30 (at venue_id='1') = 130 minutes.
thank you.
The IN operator checks whether the value on the left side is contained in the set of values on the right side, and returns a boolean result (0 or 1).
You just want to use the act_time value directly; drop act_time IN.

select sql table rows as columns for survey application

I am developing a survey application, a very simple one that has two tables.
table_survey_answers
+------------+------------+----------------+
| customerid | questionID | answer |
+------------+------------+----------------+
| 1 | 100 | Good |
| 1 | 101 | Acceptable |
| 1 | 102 | Excellent |
| 2 | 100 | Not acceptable |
| 2 | 101 | Acceptable |
| 2 | 102 | Good |
+------------+------------+----------------+
table_questions
+------------+-----------------------------------+
| QuestionID | Question |
+------------+-----------------------------------+
| 100 | Kindly rate our customer service? |
| 101 | How fast is our product delivery? |
| 102 | Quality of the Product A? |
+------------+-----------------------------------+
Now I want display survey result as follow in asp.net gridview.
+------------+-----------------------------------+-----------------------------------+---------------------------+
| CustomerID | Kindly rate our customer service? | How fast is our product delivery? | Quality of the Product A? |
+------------+-----------------------------------+-----------------------------------+---------------------------+
| 1 | Good | Acceptable | Excellent |
| 2 | Not Acceptable | acceptable | Good |
+------------+-----------------------------------+-----------------------------------+---------------------------+
I already created tables to get survey responses. Only thing I want export the result in gridview as explained above format.
Use Pivot which will transpose your rows to columns
SELECT *
FROM (SELECT customerid,
answer,
Question
FROM table_questions a
JOIN table_survey_answers b
ON a.QuestionID = b.questionID) a
PIVOT (Max(answer)
FOR Question IN([Kindly rate our customer service?],
[How fast is our product delivery?],
[Quality of the Product A?])) piv
SQL FIDDLE DEMO

Select single row per unique field value with SQL Developer

I have thousands of rows of data, a segment of which looks like:
+-------------+-----------+-------+
| Customer ID | Company | Sales |
+-------------+-----------+-------+
| 45678293 | Sears | 45 |
| 01928573 | Walmart | 6 |
| 29385068 | Fortinoes | 2 |
| 49582015 | Walmart | 1 |
| 49582015 | Joe's | 1 |
| 19285740 | Target | 56 |
| 39506783 | Target | 4 |
| 39506783 | H&M | 4 |
+-------------+-----------+-------+
In every case that a customer ID occurs more than once, the value in 'Sales' is also the same but the value in 'Company' is different (this is true throughout the entire table). I need for each value in 'Customer ID to only appear once, so I need a single row for each customer ID.
In other words, I'd like for the above table to look like:
+-------------+-----------+-------+
| Customer ID | Company | Sales |
+-------------+-----------+-------+
| 45678293 | Sears | 45 |
| 01928573 | Walmart | 6 |
| 29385068 | Fortinoes | 2 |
| 49582015 | Walmart | 1 |
| 19285740 | Target | 56 |
| 39506783 | Target | 4 |
+-------------+-----------+-------+
If anyone knows how I can go about doing this, I'd much appreciate some help.
Thanks!
Well it would have been helpful, if you have put your sql generate that data.
but it might go something like;
SELECT customer_id, Max(Company) as company, Count(sales.*) From Customers <your joins and where clause> GROUP BY customer_id
Assumes; there are many company and picks out the most number of occurance and the sales data to be in a different table.
Hope this helps.

Resources