I have a column called balance and another column called repnum. Repnum represents a representative who's taken care of customers. I've managed to grab the customers name, the specific representative i'm looking for (15), and I know I can grab the balance by taking out (sum(balance)) but what I want to do is get the sum of the balance column with only representative 15's orders. This is what I currently have thought up:
select customername, repnum, sum(balance) balance from customer where repnum = '15'
Add an over clause to the sum function to prevent the need for group by:
Select customername,
Repnum,
Sum(balance) over(partition by repnum) balance
From customer
Where repnum = 15
I think you can just add group by customername, repnum to the end of your query.
Related
I have the two tables tbl_Customers and tbl_Invoices.
In tbl_Invoice table there the following columns:
• InvoiceId
• InvoiceDate
• DueDate
• CustomerId
• InvoiceAmount
• PaidAmount
• RemainingAmout
• PaidOrNot
When I generate the New Invoice, then how do I find the sum of previous not paid invoices?
My Question is that, when I print number of invoices with where clause with Invoice create Date, then how do I find the RemainingAmount of customers in newly printed invoice of each customer?
In order to get the not paid amounts, then you should get all the remaiming amount of the invoices not fully paid.
SELECT SUM(InvoiceAmount - PaidAmount)
FROM tbl_Invoices
WHERE PaidAmount < InvoiceAmount
AND CustomerId = #custID
You can add a condition on the CreateDate to be smaller than Today,
or perhaps adding a condition on the DueDay (whether it was due or not).
This part was not clear in your question.
Why can't you do this with a simple select like this:
SELECT SUM(InvoiceAmount)
FROM tbl_Invoices
WHERE PaidAmount < InvoiceAmount AND CustomerId = #custID
I have a table that updates all the time.
The table maintains a list that links stores to clubs, and manages, among other things, "discount percentages" per store + club.
Table name: Policy_supplier
Column: POLXSUP_DISCOUNT
Suppose all the "vendors" in the table are marked with a 10% discount.
And someone accidentally signs one vendor with 8% or 15% (or even NULL)
How do I generate a query to retrieve the "abnormal" vendor?
You can find the mode of your discounts and then just pick out the records that aren't equal to that mode:
WITH mode_discount AS (SELECT TOP 1 POLXSUP_DISCOUNT FROM table GROUP BY POLXSUP_DISCOUNT ORDER BY count(*) DESC)
SELECT * FROM table WHERE POLXSUP_DISCOUNT <> (SELECT POLSXUP_DISCOUNT FROM mode_discount);
You can use the OVER clause with aggregates to calculate an aggregate over a data range and include it in the results. For example,
SELECT avg(POLXSUP_DISCOUNT)
from Policy_supplier
Would return a single average value while
SELECT POLXSUP_DISCOUNT, avg(POLXSUP_DISCOUNT) OVER()
from Policy_supplier
Would return the overall average in each row. Typically OVER is used with a PARTITION BY clause. If you wanted the average per supplier you could have written AVG() OVER(PARTITION BY supplierID).
To find anomalies, you should use one of the PERCENTILE functions, eg PERCENTILE_CONT. For example
select PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY POLXSUP_DISCOUNT) over()
from Policy_Supplier
Will return a discount value below which you'll find 95% of the records. The other 5% of discounts that are above this are probably anomalies.
Similarly, PERCENTILE_CONT(0.05) will return a discount below which you'll find 5% of the records
You can combine both to find potentially exceptional records, eg:
with percentiles as (
select ID,
POLXSUP_DISCOUNT,
PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY POLXSUP_DISCOUNT) over() as pct95,
PERCENTILE_CONT(0.05) WITHIN GROUP (ORDER BY POLXSUP_DISCOUNT) over() as pct05,
from Policy_Supplier)
select ID,POLXSUP_DISCOUNT
from percentiles
where POLXSUP_DISCOUNT>pct95 or POLXSUP_DISCOUNT<pct05
I want result sum of credits , debits and balances Code wise but i am worried how to use group by Account code of all Company divisions
Select 'All Companies' as DivisionNameEn,
a.LF_CompanyDivisionID,
a.AccountCode as code,
a.AccountNameEn as name,
sum(ISNULL(Credit,0) - ISNULL(Debit,0)) as balance,
sum(ISNULL(Credit,0)) as credit,
sum(ISNULL(debit,0)) as debit
from View_ChartOfAccount a
group by a.AccountCode
it is giving me error
Column 'View_ChartOfAccount.LF_CompanyDivisionID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Do you want something like that;
select Code, sum(balance), sum(credit), sum(debit) from
table
where DivisionNameEn = 'All Companies'
group by Code
sum calculations per Code and apply where clause for All Companies
I'm on T-SQL 2014 and try to order products by their price. Now here is the problem: the price is a calculated field. Eg. I have created a function which evaluates a number of pricing rules (maybe about 4 tables with each about 4,000,000 records combined with JOINs to fit to the current login) and returns the users price for the product. While this is OK if I just want to return the price for a limited number of products it is way to slow if I want to sort by this.
I was thinking about having an additional table like UserProductPrice which will get calculated in the background but this will obviously not always have the correct price in it as the rules etc. could change in between the calculation.
Any suggestion on how I could sort by the price would be most appreciated.
You could use the ROW_NUMBER() function and place this into a temp table:
SELECT
Product,
dbo.ufnPrice(Price) as Price,
ROW_NUMBER() OVER (PARTITION BY Product ORDER BY dbo.ufnPrice(Price) DESC) AS Ranking
INTO #Products
FROM dbo.Products
SELECT
Product,
Price,
FROM #Products
WHERE Ranking = 1
DROP TABLE #Products
Should give you what you need.
I have the following table to work with, which I can not change. I have to work with what I have.
Id (int auto int)
CustomerName (varchar)
CustomerNumber (int)
Date (date)
WeeklyAmount (int)
What I would like to do is grab all the data per customer and add all the weekly amounts for a specific year. Eventually I will want to compare two years together, but right now I am working on the data to sum up the weekly totals per CustomerNumber.
I am using:
Select
CustomerNumber, SUM (WeeklyAmount) as Total from
Customers.RECORDS GROUP BY CustomerNumber;
This works fine, however, I want to return the CustomerName as well. Eventually I will have to place in the SQL for getting specific years and compare them. However, I have to tackle this part first.
Assuming there is a 1-to-1 relationship between CustomerName and CustomerNumber:
Select
CustomerNumber, CustomerName, SUM (WeeklyAmount) as Total from
Customers.RECORDS GROUP BY CustomerNumber, CustomerName;
If the relationship is not 1-to-1, then I suppose you'd need to define what exactly represents a customer in the phrase grab all the data per customer.