I have 2 tables naming..InvoiceGarmentService and Payment..
InvoiceGarmentService Table Data
----------------------------------
IGSID InvoiceID Price
0 1001 50
1 1001 100
2 1002 500
3 1002 600
------------------------------------
Payment Data
------------------------------------
PaymentID InvoiceID Amount
0 1001 20
1 1002 300
2 1003 900
------------------------------------
I want to get the due amount ie. (Sum of Price From IGS)-(Sum of Amount from Payment)
I have used a query like this
SELECT sum(Price) FROM InvoiceGarmentService - sum(Amount)FROM Payment
WHERE InvoiceGarmentService.InvoiceID='1001'
But I am Unable to find result..saying here is a syntax error but I dn knw about that..
Anyone can help me
SELECT igs.InvoiceID, (val1 - val2)
FROM (SELECT InvoiceID, sum(Price) AS val1
FROM InvoiceGarmentService
GROUP BY InvoiceID) igs
JOIN (SELECT InvoiceID, sum(Amount) AS val2
FROM Payment
GROUP BY InvoiceID) p
ON (igs.InvoiceID = p.InvoiceID)
WHERE igs.InvoiceID = '1001'
Related
I have 2 tables :
payments:
id amount type code
1 1200 0 111
2 100 1 111
3 200 0 111
4 50 0 112
5 500 2 112
6 300 3 113
bills:
id details code
-----------------------
1 bill-1 111
2 bill-2 112
3 bill-3 113
4 bill-4 114
I wanted to sum the amounts in payments table and join it with bills like below
result:
bills.code type0Sum type1Sum type2Sum type3Sum
-------------------------------------------------------------------------
111 1400 100 0 0
112 50 0 500 0
113 0 0 0 300
114 0 0 0 0
Sorry if this is a newbie question
[Edit]
I have used a similar query as below :
SELECT *
FROM bills,
(SELECT SUM(amount) AS type0Sum, code
FROM payments
WHERE type = 0
GROUP BY code)
AS sub1,
(SELECT SUM(amount) AS type1Sum, code
FROM payments
WHERE type = 1
GROUP BY ref_code)
AS sub2
WHERE bills.code = sub1.code
AND bills.code = sub2.code
But I am getting only the rows those having the type like :
bills.code type0Sum type1Sum type2Sum type3Sum
-------------------------------------------------------
111 1400 100
I've modified that final query to do proper joins, not the old joins that you were doing (read up on cartesian joins). Give this one a go for you, see if it works;
SELECT b.code
,sub1.type0Sum
,sub2.type1Sum
FROM bills b
LEFT JOIN (
SELECT SUM(amount) AS type0Sum
,code
FROM payments
WHERE type = 0
GROUP BY code
) AS sub1 ON b.code = sub1.code
LEFT JOIN (
SELECT SUM(amount) AS type1Sum
,code
FROM payments
WHERE type = 1
GROUP BY ref_code
) AS sub2 ON b.code = sub2.code
There are other ways of doing this that are more efficient but I've kept to your query in order to help you learn.
So there are 2 tables, Transactions with created_at column and Transaction_associations with amount and remaining_balance columns, among others. I need to calculate a running sum(total) on the amount column, sorted by the created_at column, obviously. The only problem is that I need to get the SUM of all transactions that are created before the current transaction that is being calculated. I would've needed a select inside the update query in order to SELECT a current_transactions table in order to get hold of the current created_at date. However I can't. Am I missing something? Are there alternatives to this method?
UPDATE Transaction_associations SET remaining_balance =
(
SELECT SUM (Transaction_associations.amount)
FROM Transactions
JOIN Transaction_associations ON Transactions.id = transaction_id
WHERE created_at <= current_transactions.created_at // here
)
WHERE id IN
(
SELECT id
FROM Transaction_associations
JOIN Transactions ON Transactions.id = transaction_id
WHERE created_at >= '2014-11-24'
)
Edit: Added example.
Transactions Transaction_associations
created_at amount remaining_balance
2014-02-01 100 100
2014-03-01 50 150
2014-04-01 205 355
Later Edit: Added complete code for use on SQLFiddle. I've replaced Transaction_associations with TA2 on SUM, as it complains of misuse of aggregate: SUM()
DROP TABLE IF EXISTS Transactions;
DROP TABLE IF EXISTS Transaction_associations;
CREATE TABLE Transactions ( id integer, created_at text);
CREATE TABLE Transaction_associations ( id integer, amount integer, remaining_balance integer, transaction_id integer);
INSERT INTO Transactions VALUES (1,'2015');
INSERT INTO Transactions VALUES (2,'2014');
INSERT INTO Transactions VALUES (3,'2013');
INSERT INTO Transactions VALUES (4,'2012');
INSERT INTO Transactions VALUES (5,'2010');
INSERT INTO Transaction_associations VALUES (6, 100, 0, 1);
INSERT INTO Transaction_associations VALUES (7, 20, 0, 2);
INSERT INTO Transaction_associations VALUES (8, 3, 0, 3);
INSERT INTO Transaction_associations VALUES (9, 40, 0, 4);
INSERT INTO Transaction_associations VALUES (10, 500, 0, 5);
UPDATE Transaction_associations
SET remaining_balance =
(
SELECT SUM(TA2.amount)
FROM Transactions
JOIN Transaction_associations AS TA2 ON Transactions.id = TA2.transaction_id
WHERE created_at <= (SELECT created_at
FROM Transactions
WHERE id = TA2.transaction_id)
)
WHERE transaction_id IN
(
SELECT id
FROM Transactions
WHERE created_at >= '2013'
);
SELECT * from Transactions join Transaction_associations on Transactions.id = Transaction_associations.transaction_id;
This results in, which is wrong:
1 2015 6 100 663 1
2 2014 7 20 663 2
3 2013 8 3 663 3
4 2012 9 40 0 4
5 2010 10 500 0 5
Result should be:
1 2015 6 100 663 1
2 2014 7 20 563 2
3 2013 8 3 543 3
4 2012 9 40 0 4
5 2010 10 500 0 5
To use the same table name multiple times, rename one of them. This is not possible with UPDATE, so you have to do this in the SELECT.
To look up the corresponding timestamp, use another subquery.
Together with some simplifications, this becomes:
UPDATE Transaction_associations
SET remaining_balance =
(
SELECT SUM(TA2.amount)
FROM Transactions
JOIN Transaction_associations AS TA2 ON Transactions.id = TA2.transaction_id
WHERE created_at <= (SELECT created_at
FROM Transactions
WHERE id = Transaction_associations.transaction_id)
)
WHERE transaction_id IN
(
SELECT id
FROM Transactions
WHERE created_at >= '2014-11-24'
);
In google BigQuery I have done a simple query to get how many music someone has listened.
What I need is to make a sum for all rows returned from the query below (some type of subquery)?
select count(1) cnt
from OF7.PETERV_TEST
where gender='F'
group by userId
Row f0_
1 14
2 1
3 7
4 18
5 1
6 4
7 2
8 2
expected result:
49
you can use:
SELECT sum(cnt)
FROM
(SELECT count(1) cnt
FROM OF7.PETERV_TEST
WHERE gender='F'
GROUP BY userId )
I have some records.
ID Salary WillGroupBy Amount
----------------------------------------
6320 100 1 15
6320 150 1 20
6694 200 0 25
6694 300 0 30
7620 400 1 45
7620 500 1 50
How can I group by only which "WillGroupBy = 1" records?
(I will SUM Salary and Amount columns)
I want to get this result:
ID Salary WillGroupBy Amount
----------------------------------------
6320 250 1 35
6694 200 0 25
6694 300 0 30
7620 900 1 95
Can you help me please :( ?
Solution:
SELECT ID, SUM(Salary) Salary, WillGroupBy, SUM(Amount) Amount
FROM YourTable
where WILLGROUPBY = 0
union all
SELECT ID, SUM(Salary) Salary, WillGroupBy, SUM(Amount) Amount
FROM YourTable
where WILLGROUPBY = 1
group by ID, WillGroupBy
I used this solution via Erhan.
I would to know that how it could be in another way.
With MySQL you can do:
SELECT ID, SUM(Salary) Salary, WillGroupBy, SUM(Amount) Amount, #row := #row + 1
FROM YourTable
JOIN (SELECT #row := 0) v
GROUP BY ID, IF(WillGroupBy = 1, -1, #row)
DEMO
I have an example table:
ID | ArticleID | Price | SupplierID | dateAdded
1 1 100 1 2014-08-01
2 1 110 2 2014-08-01
3 2 105 1 2014-08-01
4 2 106 1 2014-08-01
5 2 101 2 2014-08-01
6 3 100 1 2014-08-01
7 1 107 2 2014-09-01
8 3 343 2 2014-09-01
9 3 232 2 2014-09-01
10 1 45 1 2014-09-01
I want to use .query on this table and select LAST value entered for each DISTINCT ArticleID for each SupplierID, resulting in:
ID | ArticleID | Price | SupplierID
10 1 45 1
9 3 232 2
6 3 100 1
7 1 107 2
4 2 106 1
5 2 101 2
I want to get price for last ArticleID entered for each SupplierID.
What should I enter into
public Cursor query (boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
I came up with this so far:
String[] columns = new String[]{DatabaseOpenHelper.KEY_ID, DatabaseOpenHelper.KEY_CENA, DatabaseOpenHelper.KEY_IZDELEK_ID};
Cursor crs = database.query(true,"prices", columns, selection, selectionArgs, null, null, null, null);
but now I'm stuck:S
Any hint how to do this?
You can also suggest raw query if possible..
Raw query would be like this:
SELECT ID, ArticleID, Price, SupplierID FROM your_table WHERE ID IN (SELECT max(ID) from your_table GROUP BY ArticleID, SupplierID);
I assumed the IDs are autoincremented and the more recent entries have higher ids. If that's not the case change the HAVING clause to operate on DATE column.
After fidling around a bit and help of a friend I have came with SQL query that does what I want, not sure about optimization:
select tab.* from cene tab inner join (
select izdelek_id, trgovina_id, Max(enter_date) as maxDate
from cene group by izdelek_id, trgovina_id) art
on (art.izdelek_id = tab.izdelek_id) and (art.trgovina_id = tab.trgovina_id) and (art.maxDate = tab.enter_date)
izdelek_id = ArticleID
trgovina_id = SupplierID
cene is the name of a table.
Hope it helps to somebody..