How to sum multiple columns? - sql

my question is: is it possible to sum multiple columns and get the total of every column? I'm using this query:
SELECT borrower, SUM(collateral_amount) AS collateral_amount, SUM(withdraw_amount) AS withdraw_amount, SUM(total_amount_to_treasure) as total_amount_to_treasure, SUM(id) as id
FROM loans
GROUP BY borrower
But that result is not what I want 🤔. What I would like to get is for example collateral_amount: 30000 (second image)
And I would like to get this as a result:
{
"collateral_amount": "600000", //The total sum of the collateral_amount column
"id": "10", //The total sum of the id column
"withdraw_collateral": "30000", //The total sum of tthe withdraw_collateral column
"total_amount": "90000" //The total sum of the total_amount
}

If the task to sum all columns then you shouldn't use group by:
SELECT SUM(collateral_amount) AS collateral_amount,
SUM(withdraw_amount) AS withdraw_amount,
SUM(total_amount_to_treasure) AS total_amount_to_treasure,
SUM(id) AS id
FROM loans;

Related

How to get Max from Count in SQL (Access)?

There is a table called "Athletes" which has columns for "Group" and "Award". It is necessary to calculate using Count the number of awards in groups separately and display the group with the maximum number of awards and the number of awards for this group. I tried to make a request of this type:
SELECT Max (Reward1) AS Reward
FROM (
SELECT Count (Athletes.Reward) AS Reward1
FROM Athletes
GROUP BY Athletes.Group
) AS [% $ ## # _ Alias];
This works, but the column corresponding to the maximum number of awards is not displayed for the group that received these awards. Can you please tell me what is worth adding so that this column is also displayed?
You're not selecting the GROUP column. You don't need to nest the query in a subquery, you can just pull the largest count of athlete rewards by selecting the largest row from your query.
SELECT TOP 1 Athletes.Group as Group
, Count (Athletes.Reward) AS Reward1
FROM Athletes
GROUP BY Athletes.Group
ORDER BY Reward1 DESC;

Need SQL query in Access to Show Total Sales by Catalog#

I have a table named Sales_Line_Items. In it I have the following fields; order#, Catalog#, Whole_Sale_Price, Qty_Ordered. I have created a query using this table that sums up the Qty_Ordered field and returns a Revenue field with a total per order line. What I need is a query that shows me each the quantity of each item sold and the total sales by item. This is the query I wrote for the total revenue:
SELECT
Sales_Line_Items.[Order#],
Sales_Line_Items.[Catalog#],
Sales_Line_Items.Wholesale_Price,
Sum(Sales_Line_Items.Qty_Ordered) AS SumOfQty_Ordered,
Sum(Sales_Line_Items.[Qty_Ordered]*Sales_Line_Items.[Wholesale_Price]) AS Revenue
FROM Sales_Line_Items
GROUP BY
Sales_Line_Items.[Order#],
Sales_Line_Items.[Catalog#],
Sales_Line_Items.Wholesale_Price;
The result was a new field with header ' Revenue'. The other fieds listed are Order#, Catalog#, Wholesale_Price, and SumofQty_Ordered fields. This is fine, if I want to know what every sales line totals up to within each order. But I want to show the total sold by Catalog# so that I can view each product sales total.
I was able to figure this out by adding the Totals line in Design View, and grouping by Expression in the query field and leaving the Catalog# field alone. The query I used looks like this;
Total: Sum([Sales_Line_Items]![Wholesale_Price]*[Sales_Line_Items]![Qty_Ordered])

Question about divide result from the same column in SQL Server

I am trying to write statement in SQL Server. What I am trying to do is to get the result of count records in columns end with "R" divide the count of all the records. So it is basically the statement of a column with a statement " count (invoice) where Invoice like "%R" / count( Invoice)"
Here is my code without the divide calculation. I only come up with statement without the divide calculation.
SELECT
Invoice,
COUNT(ART_CURRENT__TRANSACTION.Invoice) AS Number_Revisions,
MAX(ART_CURRENT__TRANSACTION.[Customer]) AS "Customer",
MAX(ARM_MASTER__CUSTOMER.Name) AS "Name",
MAX(ART_CURRENT__TRANSACTION.[Job]) AS Job
FROM
ART_CURRENT__TRANSACTION
LEFT OUTER JOIN
ARM_MASTER__CUSTOMER ON ARM_MASTER__CUSTOMER.Customer = ART_CURRENT__TRANSACTION.Customer
WHERE
Invoice LIKE '%R'
GROUP BY
Invoice;
What I am trying to ask is how can I add a column that calculate the number of invoice end with "R"/ NUMBER OF INVOICE.
Thank you guys!
What I am trying to do is to get the result of count records in Columns end with "R" divide the count of all the records.
You seem to want this calculation:
SELECT AVG(CASE WHEN t.Invoice LIKE '%R' THEN 1.0 ELSE 0 END)
FROM ART_CURRENT__TRANSACTION t;
This assumes that invoice is in the transaction table. I don't think a join is necessary for what you want to do.

Sum fields by invoicenumber

I need to do an update of fields in a table - but only if the sum of a field differs from the sum of fields in another table.
Something along these lines:
if sum (KundFaktura.FakturaBeloppUtl) <> SUM (KundFakturaHandelser.BeloppUtl) **then update**
The summation need these conditions:
KundFakturaHandelser.FakturaNr
KundFaktura.FakturaNr

After executing a SQL statement, i get a column of data. How can I get a total of that data?

I am running a query to retrieve a list of invoices that have not been approved yet. This query is generating daily alerts via email. The email contains an HTML table of the client name, inv #, sub-total, adjustments, final total. I want to be able to list a summary before the table of the total sub-total, total adjustments, and total final total of the entire table.
ex:
Total Sub-total: 10
Total Adj: -8
Total Billed: 2
Sub-Total Adj Total
7 -6 1
3 -2 1
I tried SUM(sub-total), but it only retrieves the first sub total ("7" in this example) instead of adding them all up. Help?
The query I am using is:
SELECT dbo.CurInv.subtotal AS "subtotal", dbo.CurInv.CIAdj AS "Adj",
dbo.CurInv.CIAdj+dbo.CurInv.subtotal+dbo.CurInv.CISTax AS "TotalInvoice",
SUM(dbo.CurInv.CIAdj) AS "Total Write Up(Down)"
FROM dbo.Clients, dbo.CurInv
I ended up just creating a new table that calculated the totals for each row and then added that table to the query.
You can used the UNION operator. You need to define a column to designate the individual vs total data.
SELECT
dbo.CurInv.subtotal AS "subtotal",
dbo.CurInv.CIAdj AS "Adj",
dbo.CurInv.CIAdj+dbo.CurInv.subtotal+dbo.CurInv.CISTax AS "TotalInvoice",
SUM(dbo.CurInv.CIAdj) AS "Total Write Up(Down)"
FROM
dbo.Clients, dbo.CurInv
UNION
SELECT
SUM(dbo.CurInv.subtotal) AS "subtotal",
SUM(dbo.CurInv.CIAdj)AS "Adj",
SUM(dbo.CurInv.CIAdj+dbo.CurInv.subtotal+dbo.CurInv.CISTax) AS "TotalInvoice",
SUM(dbo.CurInv.CIAdj) AS "Total Write Up(Down)"
FROM
dbo.Clients, dbo.CurInv