Combine two SQL statements with sum - sql

Two statements need to combine.
select INVOICEAMOUNT, itemid
from MTS_NONPAYMENT
select SUM(AMOUNT) AS SUM, ITEMID
from CUS_GLACCOUNT
Common column itemid. Each time I try and join, it fails. What am I doing wrong?

Fundamentally, you appear to be missing group by. I suspect the following does what you want:
select itemid, sum(invoiceamount) as invoiceamount, sum(sum) as sum
from ((select itemid, sum(INVOICEAMOUNT) as invoiceamount, 0 as sum
from MTS_NONPAYMENT
group by itemid
) union all
(select itemid, 0, SUM(AMOUNT)
from CUS_GLACCOUNT
group by itemid
)
) x
group by itemid;
To get unequal values, use:
having sum(invoiceamount) <> sum(sum)
at the end of the query.

Related

Grouping in SQL using CASE Statements

Hello I am trying to group multiple customer orders into buckets in SQL, the output should look something like it does below. Do I have to use a case statement to group them?
Table1 looks like:
CustomerID
Order_date
1
somedate
2
somedate
3
somedate
2
somedate
Edit: # of customers meaning if CustomerID 2 had 2 orders he/she would be of the in the bucket of #of orders of 2.
Output should be something like this?
# of Customers
# of Orders
2
1
1
2
My code so far is:
select count(*) CustomerID
FROM Table1
GROUP BY CustomerID;
Use a double aggregation:
SELECT COUNT(*) AS num_customers, cnt AS num_orders
FROM
(
SELECT CustomerID, COUNT(*) AS cnt
FROM Table1
GROUP BY CustomerID
) t
GROUP BY cnt;
The inner subquery finds the number of orders for each customer. The outer query then aggregates by number of orders and finds out the number of customers having each number of orders.
If you want to sort your tables and your users depending on the number of orders they made, this query should work:
SELECT CustomerID, COUNT(CustomerID) as NbOrder
FROM Table1
GROUP BY(NbOrder)
I believe what you want to do is get the count of orders by customer, first, via aggregation. Then get the count of customers by order count from that query.
SELECT count(*) as count_of_customers, count_of_orders
FROM
(
SELECT customerid, count(*) as count_of_orders
FROM your_table
GROUP BY customerid
) sub
GROUP BY count_of_orders
ORDER BY count_of_orders

How do I use COUNT function on another aggregate function in SQL?

I would like to count the aggregate function, for eg:
SELECT customer_id, SUM(amount)
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>100;
So, how do I use COUNT() on SUM() to count the filtered SUM()?
You wrap it in an outer query.
select count(*) from (
SELECT customer_id, SUM(amount)
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>100
) big_spenders
If you want to count the number of customers for each amount that you get from your query, you need a 2nd level of aggregation:
SELECT amount, COUNT(*) counter
FROM (
SELECT customer_id, SUM(amount) amount
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>100
) t
GROUP BY amount;
Or, with COUNT() window function:
SELECT DISTINCT SUM(amount) amount,
COUNT(*) OVER (PARTITION BY SUM(amount)) counter
FROM payment
GROUP BY customer_id
HAVING SUM(amount)>100;
See a simplified demo.

Mysql query to PostgreSQL query

select storeID, itemID, custID, sum(price)
from Sales F
group by storeID, custID, itemID
with cube(storeID, custID);
I am going to use this query in postgreSQL but it doesn't work in postgreSQL directly
how can I convert this query into postgreSQL query?
Your code will work without the with keyword:
select storeID, itemID, custID, sum(price)
from Sales F
group by itemID, cube(storeID, custID);
I prefer grouping sets for expressing groupings:
select storeID, itemID, custID, sum(price)
from Sales F
group by grouping sets ( (storeID, custID, itemID),
(custID, itemID),
(storeID, itemID),
(itemID)
);
If I understand what you want to do, this should be exactly the same.
You could also use cube and then filter:
select storeID, itemID, custID, sum(price)
from Sales F
group by cube(storeID, custID, itemID)
having itemId is not null
Try this:
select storeID, itemID, custID, sum(price)
from Sales F
group by cube (storeID, itemID, custID)
Check this post for more details on CUBE,GROUPING SETS and ROLLUP.

Count Function after query in postgres sql

I have written some query and I wanted to know the number of rows or total rows count. Is there a "count" function that can be applied on top of the query?
My query is like:
select customer_id, sum(amount)
from payment
group by customer_id having sum(amount)>100;
You could use count() as a window function, like in
SELECT *,
count(*) OVER () AS total_count
FROM (SELECT customer_id,
sum(amount)
FROM payment
GROUP BY customer_id HAVING sum(amount)>100) AS q;
That will return the total count in an additional column.
You can use a subquery or CTE:
select count(*)
from (select customer_id, sum(amount)
from payment
group by customer_id
having sum(amount)>100
) c;

Add row to SQL query results with totals of certain columns

I have a sql query
select * from orderTable
that returns:
I want to have the query return an additional row that has orderId = 'Total' and the sum of GrossAmt, Payments, NetAmt with the remaining fields being empty, like this:
What I've tried:
select isnull(OrderId, 'Total') as OrderId, GrossAmt
from (
select OrderId, SUM(GrossAmt) as GrossAmt
from orderTable
group by OrderId with rollup
) as OT
Which will return the last row with Total and sum of GrossAmt, but when I try to add, say StoreId like:
select isnull(OrderId, 'Total') as OrderId, GrossAmt, StoreId
from (
select OrderId, SUM(GrossAmt) as GrossAmt, StoreId
from orderTable
group by OrderId, StoreId with rollup
) as OT
Then I get double the results with a duplicate for each row only with a null value for StoreId.
I just had the idea to switch the place of OrderId and StoreId in the above query and that has given me my closest results. I have exactly what I want, only there are 2 total rows at the bottom, one with a null StoreId.
You can use UNION to make it a single table:
select * from
(
select cast(OrderId as Varchar) as OrderId, Custname, GrossAmt, Payments, NetAmt, StoreId, StaffName
from orderTable
UNION
(select 'Total' as OrderId, Null as Custname,
Sum(GrossAmt) As GrossAmt,
Sum(Payments) As Payments,
Sum(NetAmt) As NetAmt, Null as StoreId, Null as StaffName
from orderTable)
) a
Notice the cast to have the word Total fit in the OrderId column
SELECT ISNULL(OrderId, 'Total') as OrderId, GrossAmt, NULL AS storeId
FROM (
SELECT OrderId, SUM(GrossAmt) as GrossAmt
FROM orderTable
GROUP BY
OrderId WITH ROLLUP
) AS OT