Help building an Query - sql

I need some help building an Query.
I have a table "Orders" with 3 fields (IDorder, IDcostumer and amount) and i'm trying to create an List where i add one row for each costumer with the total of amount.
Can someone help me building this query?

Try the following:
SELECT IDCustomer, SUM(amount)
FROM Orders
GROUP BY IDCustomer

SELECT sum(amount), IDcostumer FROM Orders GROUP BY IDcostumer

Thanks for the answer. With your query i've tried to joined another table and converted it to LINQ with linqer. The final code was:
from c in contexto.Costumers
join s in contexto.Sales on c.IDcostumer equals s.IDCostumer
group new {c, s} by new {
c.IDcostumer,
c.name
} into g
select new {
IDcostumer = (Int32?)g.Key.IDcostumer,
g.Key.name,
total = (Decimal?)g.Sum(p => p.s.total)
}
Unfortunately i haven't understand yet the meaning of group and how it works.
I'll read a few articles to try to understand it.
Thanks ;)

Related

Use group by with sum in query

These 3 tables that you see in the image are related
Course table and coaching table and sales table
I want to make a report from this table on how much each coach has sold by each course period.
The query I created is as follows, but unfortunately it has a problem and I do not know where the problem is.
Please help me fix the problem
Thank you
SELECT
dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid, dbo.tblPost.postTitle,
dbo.tblArticleAuthor.authorName, SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM
dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblCustomersOrders.pid
For this use, SUM() is an Aggregate Function, so you need to refer all the
fields that you want to get in your result set.
Example:
SELECT
dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid, dbo.tblPost.postTitle,
dbo.tblArticleAuthor.authorName, SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid,
dbo.tblPost.postTitle, dbo.tblArticleAuthor.authorName
But this query does not solve the need for your report.
If you just need to get "how much each coach has sold by each course" , you can try the query bellow.
SELECT
dbo.tblArticleAuthor.authorName, dbo.tblPost.postTitle,
SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblArticleAuthor.authorName, dbo.tblPost.postTitle
If you need, send more details regarding the desired result.
Here you can find more information about SQL SERVER Aggregate Functions:
https://learn.microsoft.com/en-us/sql/t-sql/functions/aggregate-functions-transact-sql?view=sql-server-ver15
And here a quick example regarding SQL Aliases to build queries with a simple
and effective way:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_alias_table
Per your description of the task, the problem is that you only GROUPed BY dbo.tblCustomersOrders.pid, which is the period's id I guess, but you also need to GROUP BY the coach, which is dbo.tblArticleAuthor.authorName, I guess again. Plus in the SELECT field list you can not use more columns only that are aggregated + GROUPed.

How to use SUM in this situation?

I have the following tables below and their schema:
INV
id, product code, name, ucost, tcost, desc, type, qoh
1,123,CPASS 700,1.00,5.00,CPASS 700 Lorem, COM,5
2,456,Shelf 5,2.00,6.00,Shelf 5 KJ, BR,3
GRP
id,type,desc
1,COM,COMPASS
2,BR,SHELF
Currently I have a query like this:
SELECT INV.*,GRP.DESCR AS CATEGORY
FROM INV LEFT JOIN GRP ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0
There is no problems with that query.
Right now,I want to know the SUM of the TCOST of every INV record where their QOH is 0.
In this situation, does that I mean all I have to do is to write a separate query like the one below:
SELECT SUM(TCOST)
FROM INV
WHERE QOH = 0
Does it make any sense for me to try and combine those two queries as one ?
First understand that SUM is the aggregate function hence either you can run the Query like
(SELECT SUM(TCOST) FROM INV WHERE QOH=0) as total
This will return Sum of TCOST in INV Table for mentioned condition.
Another approach is finding the Sum based on the some column (e.g. Type)
you could write query like
SELECT Type , SUM(TCOST) FROM INV WHERE QOH=0 GROUP BY type ;
Its not clear on what criteria you want to sum . But I think above two approaches would provide you fare idea .
Mmm, you could maybe use a correlated query, though i'm not sure it's the best approach since I'm not sure I understand what your attempting to do:
SELECT INV.*,
GRP.DESCR AS CATEGORY ,
(SELECT SUM(TCOST) FROM INV WHERE QOH=0) as your_sum
FROM INV LEFT JOIN GRP ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0
If you want only one value for the sum(), then your query is fine. If you want a new column with the sum, then use window functions:
SELECT INV.*, GRP.DESCR AS CATEGORY,
SUM(INV.TCOST) OVER () as sum_at_zero
FROM INV LEFT JOIN
GRP
ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0;
It does not make sense to combine the queries by adding a row to the first one, because the columns are very different. A SQL result set requires that all rows have the same columns.

How to fetch specified data based on two columns in PostgreSQL

I am new to writing queries in SQL, and struggling to get the datas from table.
I have a table called "address" in DB. Here, I have more columns and specifically from two column I need to get the datas filtered out. Both are integers
frequency_type_id
date_of_invoice_id
I need to get rows only where frequency_type_id = 1 and date_of_invoice_id=2, and frequency_type_id = 2 and date_of_invoice_id=3, frequency_type_id = 3 and date_of_invoice_id=1.
I am trying with the following query, but its not filtering properly,
SELECT address_id, company_name,
invoice_batch_billing, frequency_type_id, date_of_invoice_id
FROM pls.address where invoice_style='Consolidated'
and frequency_type_id in
(SELECT frequency_type_id from pls.address where frequency_type_id=1 and date_of_invoice_id=1);
Hope someone assist.
Try below query:
SELECT address_id, company_name,
invoice_batch_billing, frequency_type_id, date_of_invoice_id
FROM pls.address where invoice_style='Consolidated'
and ((frequency_type_id=1 and date_of_invoice_id=2) or (frequency_type_id=2 and date_of_invoice_id=2) or (frequency_type_id=3 and date_of_invoice_id=2))
Try this:
SELECT
address_id,
company_name,
invoice_batch_billing,
frequency_type_id,
date_of_invoice_id
FROM
pls.address
WHERE
frequency_type_id IN (1,2,3)
AND
date_of_invoice_id=2
If I have understand your question correctly, this will suffice your requirement.

Building a sql query from two tables

I created a query which selects sum of columns from a table grouped by a field in Crystal reports, now I want the result to be filtered by a date range from another table which I am unable to do. Please Help...
Here is the query
SELECT
BDETAIL.HSN,
SUM(BDETAIL.TAXABLE),
SUM(BDETAIL.SGST_V),
SUM(BDETAIL.CGST_V),
SUM(BDETAIL.TOTAL),
BDETAIL.SGST_P
FROM
BDETAIL
JOIN
BILL ON BDETAIL.BILL_ID = BILL.BILL_ID
WHERE
BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY
BDETAIL.HSN, BDETAIL.SGST_P
I finally figured it out ..here is the working query
SELECT BDETAIL.HSN,
sum(BDETAIL.TAXABLE),
Sum(BDETAIL.SGST_V),
Sum(BDETAIL.CGST_V),
Sum(BDETAIL.TOTAL),
BDETAIL.SGST_P
FROM BDETAIL INNER JOIN BILL ON
( BILL.BILL_ID = BDETAIL.BILL_ID AND
BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY BDETAIL.HSN, BDETAIL.SGST_P;
thank you all for showing interest in my issue.

Oracle SQL Return Count of Primary key from 2 different columns in the same table

Tables
ARMASTER = Customer Information
ORDERHEAD = Sales order information (Ship to, bill to)
Hey everyone! I'm still quite new to SQL and I do belive I'm picking it up fairly quick. I have been racking my brain on this for a few hours now and have asked around the office and nobody seems to have a solution.
I'm trying to identify how many times a customer account has been used as a SHIP TO location & a BILL TO location.
SELECT ARMASTER.CUSTOMER,
ARMASTER.DIVISION,
ARMASTER.STATUS,
ARMASTER.CUHEAD AS "MASTER",
COUNT (ORDERHEAD.BILLTO) AS "COUNT_BILL",
COUNT (ORDERHEAD.SHIPTO) AS "COUNT_SHIP"
FROM ARMASTER
LEFT OUTER JOIN ORDERHEAD
ON ARMASTER.CUSTOMER = ORDERHEAD.BILLTO
LEFT OUTER JOIN ORDERHEAD
ON ARMASTER.CUSTOMER = ORDERHEAD.SHIPTO
GROUP BY ARMASTER.CUSTOMER,
ARMASTER.DIVISION,
ARMASTER.STATUS,
ARMASTER.CUHEAD
And I'm not even remotley getting what I should be getting. However, when I remove one of my joins the count is exactly what it should be for either 1 or the other of them.
Any guidance would be muchly appreciated! Thank you!
I think it can also work
SELECT CUSTOMER,
DIVISION,
STATUS,
CUHEAD AS "MASTER",
(SELECT COUNT(1) FROM ORDERHEAD WHERE SHIPTO = ARMASTER.CUSTOMER ) AS "COUNT_BILL",
(SELECT COUNT(1) FROM ORDERHEAD WHERE BILLTO = ARMASTER.CUSTOMER ) AS "COUNT_SHIP"
FROM ARMASTER