I have a table called Customer which contains two columns called opening_amt and receive_amt. I wish to display all customer details where the sum of opening_amt and receive_amt is greater than 15000.
select *
from Customer
where opening_amt > 15000;
works for just the opening amt, however this function does not work.
select *
from Customer
where opening_amt and receive_amt > 15000 ;
Thanks in advance for any help.
You need to specify the amount for both columns!
select * from Customer where opening_amt > 15000 and receive_amt > 15000
or
"sum of opening_amt and receive_amt is greater than 15000."
select * from Customer where opening_amt + receive_amt > 15000
Both examples above are doing quite different things. One is ensuring that only customers with both amounts are greater than 15000 will be returned. The second will only return customers with the sum of both over 15000.
Related
I have a need to create a stock report, where I have article number in one stock table and Invoice number under which they were sold in another table.
Stock Table:
Select * from StockTable
result:
artno opening_Stock stock_received
30271472 1 50
Invoice Table:
Select * from InvoiceTable
result:
itemno invoicenumber QTYSold invoicedate
30271472 Inv_123 10 2018-10-06T00:00:00
30271472 Inv_234 20 2018-10-06T00:00:00
30271472 Inv_345 10 2018-10-06T00:00:00
30271472 Inv_567 10 2018-10-06T00:00:00
The Problem is that in Stock Table StockReceived is 50. Now this 50 Quantity can be sold to difference customers in multiple invoices.
My Objective is to show data in most presentation way and then query for that: a) Some option I can think of is to show all invoice numbers in comma saperated, Using XML Path or COALESCE..
b) Second option is to join two table and for each invoice number generate a new row, but in this case Opening_Stoc and Stock Received value will also repeat for each row.
c) Third is to generate dynamic columns for each invoice. Not even sure how to achive this..
Really confused, can somebody help me suggested the best possible way to present this to business and query to achive the same
Regards
Vipin
In the tab above you can see several records in some cases the Material column of the record n is equal to the record n + 1 and in the Material column Desc the record n is equal to the record n + 1.
But it does not make the columns List Price USD and RVS-ZSEG what happens is that I need to make a group by or combine the pairs in some registers but that they become complementary.
For example that in register
1 and 2 would be converted by combining the values of List Price USD and RVS ZSEG should be shown as image below
Try this:
SELECT Material, MaterialDesc
,MAX([D-Chain-Spec(Status)])[D-Chain-Spec(Status)]
,MAX([List Price USD])[List Price USD]
,MAX([RVS - ZSEG])[RVS - ZSEG]
FROM YourTable
GROUP BY Material, MaterialDesc
From what I see you just need a sum on List Price USD and RVS-ZSEG right?
SELECT Material, MaterialDesc,[D-Chain-Spec(Status)]
,SUM([List Price USD]) as [List Price USD]
,SUM([RVS - ZSEG]) as [RVS - ZSEG]
FROM Table
GROUP BY Material, MaterialDesc,[D-Chain-Spec(Status)]
I would like to query my database to know which fraction/percentage of the elements of a table are larger/smaller than a given value.
For instance, let's say I have a table shopping_list with the following schema:
id integer
name text
price double precision
with contents:
id name price
1 banana 1
2 book 20
3 chicken 5
4 chocolate 3
I am now going to buy a new item with price 4, and I would like to know where this new item will be ranked in the shopping list. In this case the element will be greater than 50% of the elements.
I know I can run two queries and count the number of elements, e.g.:
-- returns = 4
SELECT COUNT(*)
FROM shopping_list;
-- returns = 2
SELECT COUNT(*)
FROM shopping_list
WHERE price > 4;
But I would like to do it with a single query to avoid post-processing the results.
if you just want them in single query use UNION
SELECT COUNT(*), 'total'
FROM shopping_list
UNION
SELECT COUNT(*),'greater'
FROM shopping_list
WHERE price > 4;
The simplest way is to use avg():
SELECT AVG( (price > 4)::float)
FROM shopping_list;
One way to get both results is as follows:
select count(*) as total,
(select count(*) from shopping_list where price > 4) as greater
from shopping_list
It will get both results in a single row, with the names you specified. It does, however, involve a query within a query.
I found the aggregate function PERCENT_RANK which does exactly what I wanted:
SELECT PERCENT_RANK(4) WITHIN GROUP (ORDER BY price)
FROM shopping_list;
-- returns 0.5
Trying to write a SQL query that would sum values based on another columns data. The part that have me stomped is the fact that the other column's data is partially alike as seen below.
AccountID AcctName Amount
---------------------------------------
1-1-100-2 Vehicles 10000
1-1-100-3 Vehicles 20000
1-1-100-4 Vehicles 20000
1-2-500-1 Books 10000
1-2-600-1 Pencils 500
I'd like to have this data display as:
AccountID AcctName Amount
--------------------------------------
1-1-100 Vehicles 50000
1-2-500 Books 10000
1-2-600 Pencils 500
Assuming the significant part of AccountId is always the first 7 characters, you can try with:
SELECT
SUBSTRING(AccountID,0,7),
AcctName,
SUM(Amount)
FROM YourTable
GROUP BY AcctName, SUBSTRING(AccountID,0,7)
Group by AcctName, left(AccountID, 7)
Quite difficult to explain in the title so here is my scenario:
Let's say I have a value of £10,000, this is going to be paid in random instalments (up to 100%). The cumulative total is stored in a field called PercentageComplete.
Payment 1 is £5,000 (PercentageComplete = 50% of total amount owed)
Payment 2 is £2,500 (PercentageComplete = 75% of total amount owed)
So far the way I'm trying to work this out is by adding (0.5 + 0.75) which gives me a total of 1.25 ... and using the COUNT function I'm dividing this by 2 (which gives the incorrect answer of 0.625).
I'm probably over complicating this issue so thanks for any help.
The code I tried:
select FinanceID, CustomerID sum(PercentageComplete), COUNT(PercentageComplete)
from Finances group by FinanceID, CustomerID
I think you just want max():
select FinanceID, CustomerID max(PercentageComplete)
from Finances
group by FinanceID, CustomerID ;