sql sum column in having clause is not summing quarterly values - sql

i would like to sum (purchase_prd) which is quarterly for customerID where purchase value is >0 and <=500. I have same customer ids in multiple purchase_prd, and would also like to see how many records show for their customerID....how do I query this?
I have the following
select purchase_prd, count(*), customerID, sum(purchase_value)
from table a
where purcahse_prd between 201700 and 201712 /*data is quarterly, so 201700, 201703, 201706,201709, 201712*/
group by customerid, purchase_value
having purchase_value >0 and purchase_value<=500
my results show customerids in multiple quarters and the sums of purchase_value exceeds 500, each quarter is separate and not extracting the total of purchase_value for the entire year with the criteria of purchase_value >0 and <=500
my results are:
purchase_prd customer ID purchase_value
201700 714 776
201703 714 120
201706 714 50
201709 714 20
201712 714 100
I'd like 2017 summed for customerID 714 and selected if sum of purchase_value is >0-<=500

I think you want:
select customerID, purchase_prd, count(*), sum(purchase_value)
from table a
where purchase_prd between 201700 and 201712 /*data is quarterly, so 201700, 201703, 201706,201709, 201712*/
group by customerid, purchase_prd
having sum(purchase_value) > 0 and sum(purchase_value) <= 500

Related

POSTGRESQL - SUM of all orders with the same customer for a given month

I'm trying to produce a report with the total invoice amount for each customer in the month of December :
date
customer
invoice amount
01/12/2021
AB1
40
02/11/2021
AB2
60
12/12/2021
CE6
1000
31/12/2021
RF9
0.5
Could I get any pointers? I'm still fairly new to postgresql.
You should use GROUP BY for your purposes.
SELECT customer, SUM(invoice_amount) as total_invoice_amount
FROM your_table
WHERE EXTRACT(MONTH FROM date) = 12
GROUP BY customer

Oracle sql: Order by with GROUP BY ROLLUP

I'm looking everywhere for an answer but nothing seems to compare with my problem. So, using rollup with query:
select year, month, count (sale_id) from sales
group by rollup (year, month);
Will give the result like:
YEAR MONTH TOTAL
2015 1 200
2015 2 415
2015 null 615
2016 1 444
2016 2 423
2016 null 867
null null 1482
And I would like to sort by total desc, but I would like year with biggest total to be on top (important: with all records that compares to that year), and then other records for other years. So I would like it to look like:
YEAR MONTH TOTAL
null null 1482
2016 null 867
2016 1 444
2016 2 423
2015 null 615
2015 2 415
2015 1 200
Or something like that. Main purpose is to not "split" records comparing to one year while sorting it with total. Can somebody help me with that?
Try using window function max to get max of total for each year in the order by clause:
select year, month, count(sale_id) total
from sales
group by rollup(year, month)
order by max(total) over (partition by year) desc, total desc;
Hmmm. I think this does what you want:
select year, month, count(sale_id) as cnt
from sales
group by rollup (year, month)
order by sum(count(sale_id)) over (partition by year) desc, year;
Actually, I've never use window functions in an order by with a rollup query. I wouldn't be surprised if a subquery were necessary.
I think you need to used GROUPING SETS and GROUP_ID's. These will help you determine a NULL caused by a subtotal. Take a look at the doc: https://docs.oracle.com/cd/B19306_01/server.102/b14223/aggreg.htm

SQL Query hardCoded Update

If I execute this query I get the result below
SELECT Month_Name,
Count(SalesID) AS Toal
FROM Sales
Group by Month_Name
I have the following Result
Month_Name Toal
Apr-15 341
May-15 314
Jun-15 342
Jul-15 327
Aug-15 179
Sep-15 234
Oct-15 286
Nov-15 171
PROBLEM: In previous month I have data for Feb-15 and Mar-15 but they are not on the table. I have been told not to update the table with the figures
I was given for Feb-15 and Mar-15. The figures are as follows
Feb-15 = 349
Mar-15 = 312
Is there a way I can write a query to reflect both months by hardcoding it in the query so that the result will look like the table below
Month_Name Toal
Feb-15 349
Mar-15 312
Apr-15 341
May-15 314
Jun-15 342
Jul-15 327
Aug-15 179
Sep-15 234
Oct-15 286
Nov-15 171
(
SELECT
Month_Name,
Count(SalesID) AS Toal
FROM
Sales
Group by
Month_Name
)
UNION ALL
(
SELECT
'Feb-15' AS Month_Name,
349 AS Total
)
UNION ALL
(
SELECT
'Mar-15' AS Month_Name,
312 AS Total
)
You'd add the two records with UNION ALL. In order to produce such records, you simply select them without a table or from DUAL - which of the two depends on the DBMS you are using.
As you certainly want the records to be sorted by month, there must be a sort key in your table to make this possible. In the following query I assume it to be a positive numeric value named sortkey and the two months to come first. Adjust this according to your needs.
select month_name, total
from
(
select month_name, count(*) as total, sortkey from sales group by month_name
union all
select 'Feb-15', 349, -2 -- here you may have to add FROM DUAL
union all
select 'Mar-15', 312, -1 -- here too
) all_months
order by sortkey;

How to add summary rows to income statements in postgresql

Income statement table has structure:
sgroup char(30),
account char(10),
jan numeric(12,2),
feb numeric(12,2)
and has values:
SGroup Account Jan Feb
Sales 311 100 200
Sales 312 20 30
..
Other 410 3333 44
Other 411 333 344
...
How convert this table to have header and subtotals for each group:
Caption Jan Feb
Sales
311 100 200
312 20 30
Sales Total 120 230
Other
410 3333 44
411 333 344
Other total 3666 388
... ... ...
Grand Total ... ...
Caption column should contain group header, account numbers and group total for each group.
After total there should be empty row.
After that that there should be next group etc.
In the end there should be a "Grand Total" row containing the sum of all rows.
Using Postgres 9.1.2 in Debian.
Mono C# ASP.NET MVC application running in Debian. If it's more reasonable, this conversion can done in MVC controller also.
I would calculate sums per group in a CTE to use it three times in the main query:
WITH total AS (
SELECT sgroup, 'Sales Total'::text AS c, sum(jan) AS j, sum(feb) AS f
FROM income_statement
GROUP BY 1
)
( -- parens required
SELECT caption, jan, feb
FROM (
SELECT 1 AS rnk, sgroup, account::text AS caption, jan, feb
FROM income_statement
UNION ALL
SELECT 0 AS rnk, sgroup, sgroup::text, NULL, NULL FROM total
UNION ALL
SELECT 2 AS rnk, * FROM total
) sub
ORDER BY sgroup, rnk
)
UNION ALL
SELECT 'Grand Total', sum(j), sum(f) FROM total;
The extra set of parentheses is required to include ORDER BY.
You probably don't want to use the data type char(30):
Any downsides of using data type "text" for storing strings?

Sql for considering previous week same day sales if same day sales are 0

i have a store sales table which looks like below containing two years of data.
If the sales amount is '0' for a particular day for a particular store ,i have to take sales for same day last week.(current day-7)
if those sales are also '0' then sales for current_day-8
if those sales are also '0' then sales for current_day-9
if those sales are also '0' then sales for current_day-10
Sales_table1
day_id week_id sales Store
2/1/2014 201401 34566 1234
2/2/2014 201401 67777 567
2/3/2014 201401 3333 698
2/4/2014 201401 45644 345
2/5/2014 201401 2456 789
**2/6/2014 201401 3456 567**
2/7/2014 201401 5674 780
2/8/2014 201402 3333 1234
2/9/2014 201402 22222 567
2/10/2014 201402 111134 698
2/11/2014 201402 56789 345
2/12/2014 201402 4356 789
**2/13/2014 201402 0 567**
2/14/2014 201402 899 780
please give the query that i can use.
As you have not said which database you are using I'm giving a solution using SQL Server as that's what I'm most familiar with.
The logic of my query is to select all the rows where sales are greater than zero for each store, where the date of the sales info is either the MAX date in the table or the date is less than or equal to the MAX(date)-7 days (effectively exclude any sales in the previous 7 days). I'm doing this because you said you need to take the most recent sale or sales on day -7, -8, -9, etc.
From that set of rows the query then picks the most recent sale date.
;WITH Store_Dates AS (
SELECT store, MAX(day_id) max_date, DATEADD(day, -7, MAX(day_id)) old_date
FROM Sales_Table1
GROUP BY store
)
,Sale_Days AS (
SELECT MAX(day_id) day_id, Sales_Table1.store
FROM Sales_Table1 INNER JOIN Store_Dates
ON Sales_Table1.store = Store_Dates.store
WHERE sales > 0
AND (day_id = max_date
OR day_id <= old_date)
GROUP BY Sales_Table1.store
)
SELECT Sales_Table1.*
FROM Sales_Table1 INNER JOIN Sale_Days
ON Sales_Table1.store = Sale_Days.store
AND Sales_Table1.day_id = Sale_Days.day_id
SQL Fiddle
The only thing my query does not do is stop after going back X number of days. You could do that by limiting the number of rows returned from the Sale_Days query.