Creating a Summary Tracking Report from Details - sql

I need to create a summary monthly tracking report from detailed data. A sample of the data is as follows:
Company | Country | Join Date
Company A | USA | 1/1/2011
Company B | Ireland | 5/5/2011
Company C | Italy | 7/11/2011
Company D | Germany | 6/14/2011
I need to create a report that would give me the number of members that joined in a given month from a given country, in the format below:
Country 1 | Total (January) | Total (Feb) | Total (Mar, etc) |Sum (Monthly Totals)
Country 2 | Total (January) | Total (Feb) | Total (Mar, etc) |Sum (Monthly Totals)
Country 3 | Total (January) | Total (Feb) | Total (Mar, etc) |Sum (Monthly Totals)
At the bottom of each column I would need the sum for each month for all countries. Also, this report would need to be a rolling report, so when the user generates it, it will provide the most recent information.

You're going to want to do this in two parts.
First, you're going to want to get the relevant data from the database. Probably best accomplished by a statement such as:
SELECT country, year(join_date) as year, month(join_date) as month, count(*)
FROM trackingTable
WHERE join_date between :start_date and :end_date
GROUP BY country, year(join_date), month(join_date)
ORDER BY country, year, month
For your given data, this will generate the following sample:
Country Year Month Count
==================================
Germany 2011 6 1
Ireland 2011 5 1
Italy 2011 7 1
USA 2011 1 1
Second (and I'm assuming that you're creating/formatting the report in an outside language), read through the data sequentially. Given the ORDER BY, each country will be a separate sequence. Psuedo-code for your needs:
for each row in set {
if different country {
total up row, start new line
}
add month cell to column;
add month cell to month grand total;
}
total up grand total row, print/save

Related

Monthly average without aggregating on monthly level

The table below is aggregating order_value
using data from the last 6 month, without creating an additional subquery I would like to calculate the monthly average of all orders over the last 6 month (see desired output table below).
Table
customer_id | order_timestamp | order_value
A 2020-01-01 10:30:51 100
Desired output table
customer_id | sum_orders | monthly_avg_orders
A 100.000 16.666
This is what I already have
select
customer_id,
sum(order_value)
from table
group by customer_id

postgresql - how to calculate the percentage of number of entries in one table w.r.t to no of entries in another table

I have two tables(A & B) in my database(c). both tables contains two coulmns as shown below
column name Datatype
eventtime timestamp without time zone
serialnumber numeric
Table A contains total number of products (good and downgraded -- but in these table they are not defined as affected/downgraded) produced in each day. And table B contains total number of only downgraded products.
I want to make an quality process control chart using the percentage of downgraded products w.r.t total number of products produced (using serialnumber to join for example).
could some one tell me how can i get the percentage value for each day (also for each hour)
Use date_trunc() to group rows by desired period, e.g.:
select
a.r_date::date date,
downgraded,
total,
round(downgraded::numeric/total* 100, 2) percentage
from (
select date_trunc('day', eventtime) r_date, count(*) downgraded
from b
group by 1
) b
join (
select date_trunc('day', eventtime) r_date, count(*) total
from a
group by 1
) a
using (r_date)
order by 1;
date | downgraded | total | percentage
------------+------------+-------+------------
2015-05-05 | 3 | 4 | 75.00
2015-05-06 | 1 | 4 | 25.00
(2 rows)

SQL - Splitting / Prorating a metric

I have a table where a household ID can have multiple customer ID's. But revenue is linked to household ID. So when looking at the table with both household ID and customer ID, revenue is double counted. Sample below.
HHID 123 has 3 customers, total revenue is only $900 for the household but if I sum grouped by HHID, it's $2700. Similarly, HHID 456 has 2 customers and HHID 789 has 1.
How do I create a column to split this revenue per customer ID. I can do this in multiple steps but I'm wondering if there is a way to do this in one query.
HH ID CUST ID REVENUE NEW_COL
123 5655 $900 $300
123 6678 $900 $300
123 8893 $900 $300
789 8988 $350 $350
456 2343 $400 $200
456 4555 $400 $200
I need the table to have both HH ID and CUST ID since some metrics are based on HH and some based on CUST (such as age). So I need to add a column like the above to the table so I can use it to sum revenue instead of the actual revenue column.
I'm on Teradata.

How to sum total amount for every month in a year?

I have a database in SQL Server 2012 and there is a table with dates in D.M.YYYY format like below:
ID | Date(date type) | Amount(Numeric)
1 3.4.2013 16.00
1 12.4.2013 13.00
1 2.5.2013 9.50
1 18.5.2013 10.00
I need to sum the total amount for every month in a given year. For example:
ID | Month | TotalAmount
1 1 0.00
...
1 4 29.00
1 5 19.50
I thought what I needed was to determine the number of days in a month, so I created a function which is described in determine the number of days, and it worked. After that I tried to compare two dates(date type) and got stuck; there are some examples out there, but all of them about datetime.
Is this wrong? How can I accomplish this?
I think you just want an aggregation:
select id, month(date) as "month", sum(amount) as TotalAmount
from t
where year(date) = 2013
group by id, month(date)

Combining two tables in a query and creating new columns from that

I'm having issues with a query that I'm not ENTIRELY sure can be done with the way the database is set up. Basically, I'll be using two different tables in my query, let's say Transactions and Ticket Prices. They look like this (With some sample data):
TRANSACTIONS
Transation ID | Ticket Quantity | Total Price | Salesperson | Ticket Price ID
5489 250 250 Jim 8765
5465 50 150 Jim 1258
7898 36 45 Ann 4774
Ticket Prices
Ticket Price ID | Quantity | Price | Bundle Name
8765 1 1 1 ticket, $1
4774 12 15 5 tickets, $10
1258 1 3 1 ticket, $3
What I'm aiming for is a report, that breaks down each salesperson's sales by bundle type. The resulting table should be something like this:
Sales Volume/Salesperson
Name | Bundle A | Bundle B | Bundle C | Total
Jim 250 0 50 300
Ann 0 36 0 36
I've been searching the web, and it seems the best way of getting it like this is using various subqueries, which works well as far as getting the column titles properly displayed, but it doesn't work as far as the actual numerical totals. It basically combines the data, giving each salesperson a total readout (In this example, both Jim and Ann would have 250 sales in Bundle A, 36 in Bundle B, etc). Is there any way I can write a query that will give me the proper results? Or even something at least close to it? Thanks for any input.
You can use the PIVOT statement in Oracle to do this. A query might look something like this:
WITH pivot_data AS (
SELECT t.salesperson,p.bundle_name,t.ticket_quantity
FROM ticket_prices p, transactions t
where t.ticket_price_id = p.ticket_price_id
)
SELECT *
FROM pivot_data
PIVOT (
sum(ticket_quantity) --<-- pivot_clause
FOR bundle_name --<-- pivot_for_clause
IN ('1 ticket, $1','5 tickets, $10', '1 ticket, $3' ) --<-- pivot_in_clause
);
which would give you results like this: