Group by Account Code - sql-server

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

Related

Query help, not getting right rows

enter image description here> Write a select statement that returns InvoiceNumber, VendorName and InvoiceDate
from the Vendors and Invoices table. Use the following correlations for each table:
Vendors v
Invoices i
Filter the results to return only rows where a balance is due
I should be getting back 11 rows but I am getting 114 rows.
Select InvoiceNumber, VendorName, InvoiceDate
From Vendors as v
join Invoices as i on v.VendorId = i.VendorID
where InvoiceTotal > 0
Query above looks good.
If you got unexpected number of rows in result it means (like #Dale-K mentioned in comment) wrong condition. InvoiceTotal does not look like amount left to pay.
Look for column like InvoiceDue or InvoiceBalance.
Or there is also column with due date and this should be added to condition (to find overdued invoices).
Or there is another table keeping payments which should be joined too.
Without structure of tables and sample data we can only guess.
However this looks like homework, so look for the column with current balance of invoice and use it.

How can I retrieve "exception" data from a table without knowing the data in advance?

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

how to get data from two tables of sqlite and sort data

I've two tables
income
expense
the problem is I want to query all the data from both tables
SELECT income.date AS IN_DATE, expense.date AS EX_DATE FROM income, expense
I get weird result data is double times from db as you can see
you can try this out HERE
how can I get distinct results not double and at last wanna ask don't have idea of getting data from both tables and sort by date descending.
My guess is that you want union all:
select 'income' as which, id, title, date
from income
union all
select 'expense' as which, id, title, date
from expense;
This will give you a result set containing the rows from the two tables, with an identifier of which table each row comes from.
You can order by date and do other manipulations if you use a subquery:
select ie.*
from (select 'income' as which, id, title, date
from income
union all
select 'expense' as which, id, title, date
from expense
) ie
order by date desc;
Your simple SELECT does a cross product with the two columns (IN_DATE, EX_DATE). Hence, you get every possible combination of the values from both columns. INNER JOIN income ON expense.id=income.id or WHERE income.id == expense.id should do the trick.
You need to match the same ids, else SQL will just output any possible combination.
SELECT income.date AS IN_DATE, expense.date AS EX_DATE FROM income, expense WHERE income.id LIKE expense.id

How to get sum of column from specific representatives orders?

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.

t sql sort by calculated column

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.

Resources