I know how to subtract in sql but in my case its kinda difficult.
I have following DB
Table1
id........artnumber.......price.......flag.......cid
1 12345001 1200 L 9999
2 12345002 2000 L 9999
3 12345003 500 L 7777
4 12345004 1950 L 6666
5 54321001 500 R 9999
6 54321002 1000 R 6666
7 54321003 500 R 9999
Table2
id........artnumber.......comment
1 54321001 12345001
2 54321002 12345004
3 54321003 12345001
what im trying is:
i want to select all entrys from table1 having L flag
on the result of this i want to subtract the prices with R flag from the one with L flag. but only on the one who has his artnumber in the comment of table2
Result i want is
artnumber.......price.......flag.......cid
12345001 200 L 9999 //her was 2 R flagged items substracted
12345002 2000 L 9999 //her was nothing substracted
12345003 500 L 7777 //her was nothing substracted
12345004 950 L 6666 //her was 1 R flagged item substracted
does anybody knows how to set up a sqlstatement for this?
i found some on a other thread but it doesn't really helps me :)
This seems a little funky, but here goes. I think this will get you what you're looking for:
SELECT a.artnumber, a.price - SUM(IFNULL(b.price, 0)) price, a.flag, a.cid
FROM Table1 a
LEFT JOIN (Table1 b INNER JOIN Table2 ON Table2.artnumber = b.artnumber AND b.flag = 'R')
ON Table2.comment = a.artnumber
WHERE a.flag = 'L'
GROUP BY a.artnumber, a.price, a.flag, a.cid
Let me know if you need me to explain any of this, but the joins should be pretty straightforward.
Also, depending on your types, I'm not sure if you'll have to CAST that comment field to do the match. There may be some considerations there. I'm also making assumptions about the uniqueness of your records. In other words, I assume that artnumber and flag together constitute a unique record.
This is your data for the L prices:
select l.artnumber, l.price, l.flag
from table1 l
where l.flag = 'L';
This is your data for the R prices:
select x.comment as artnumber, sum(r.price)
from table1 r
inner join table2 x on x.artnumber = r.artnumber
where r.flag = 'R';
Together:
select
l_data.artnumber,
l_data.price - (case when r_data.sum_price is null then 0 else r_data.sum_price end) as calc_price,
l_data.flag,
l_data.cid
from
(
select l.artnumber, l.price, l.flag, l.cid
from table1 l
where l.flag = 'L'
) l_data
left join
(
select x.comment as artnumber, sum(r.price)
from table1 r
inner join table2 x on x.artnumber = r.artnumber
where r.flag = 'R'
) r_data on r_data.artnumber = l_data.artnumber;
Related
I have four table and structure as below :
1)Budget
id Budget_name
1 test1
2 test2
3 test3
2)Yearly Budget
id amount_yearly budgetid
1 1000 1
2 2000 2
3 5000 3
ri_spent
id Spent_amount budgetid
1 100 2
2 100 2
3 200 3
4)FI_spent
id Spent_amount budgetid
1 100 2
2 100 3
3 200 3
i want to fetch data accourding to or based on first budget table id
below is the query i was trying:
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d
inner join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
inner join RI_DETAILS as l on l.COST_CENTER = d.id
inner join F_RI_DETAILS as f on f.COST_CENTER = d.id
ORDER BY d.id DESC
I want output of the following way:
Id Name ri_id FI_ID RI_Spent_Amount FI_Spent_Amount Annual_Buget
1 test1 1000
2 test2 1 1 100 200 1000
2 test2 2 100 2000
3 test3 3 2 300 100 2000
3 test3 3 200 2000
Any way if possible then please help me.
I want to minus annual budget with spent_amount later.
If possible then help me .
Please Try This Query ,
select b.id ,
b.Budget_name,
y.id,
y.amount_yearly,
r.id,
r.ri_spent,
f.id,
f.Spent_amount
from Budget b
INNER JOIN Yearly_Budget y on y.budgetid =b.id
INNER JOIN ri_spent r on r.budgetid =b.id
INNER JOIN FI_spent f on f.budgetid =b.id
ORDER BY b.id DESC
Could you please use left join instead of inner join. Requirement and sql arent matching so i may be little off. But you can get the idea.
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d -- I assume this is the budget table
left outer join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
left outer join RI_DETAILS as l on l.COST_CENTER = d.id
left outer join
(select sum(FINANCEADD_ID) FINANCEADD_ID,sum(AMOUNT) AMOUNT,id from
F_RI_DETAILS group by id) as f
on f.COST_CENTER = l.id
ORDER BY d.id DESC
I need help about generating query for multiple column.
part of my tbl_advert_specific_fields_values table look like:
id advert_id field_name field_value
1 654 t1_sqft 50
2 655 t1_yearbuilt 1999
3 1521 t2_doorcount 5
4 656 t1_yearbuilt 2001
5 656 t1_sqft 29
6 654 t1_yearbuilt 2004
SELECT p.*, p.id AS id, p.title AS title, usr.id as advert_user_id,
p.street_num, p.street,c.icon AS cat_icon,c.title AS cat_title,c.title AS cat_title,
p.description as description,
countries.title as country_name,
states.title as state_name,
date_FORMAT(p.created, '%Y-%m-%d') as fcreated
FROM tbl AS p
LEFT JOIN tbl_advertmid AS pm ON pm.advert_id = p.id
INNER JOIN tbl_usermid AS am ON am.advert_id = p.id
LEFT JOIN tbl_users AS usr ON usr.id = am.user_id
INNER JOIN tbl_categories AS c ON c.id = pm.cat_id
INNER JOIN tbl_advert_specific_fields_values AS asfv ON asfv.advert_id = p.id
LEFT JOIN tbl_countries AS countries ON countries.id = p.country
LEFT JOIN tbl_states AS states ON states.id = p.locstate
WHERE p.published = 1 AND p.approved = 1 AND c.published = 1
AND (asfv.field_name = 't1_yearbuilt'
AND CONVERT(asfv.field_value,SIGNED) <= 2004 )
AND (asfv.field_name = 't1_sqft'
AND CONVERT(asfv.field_value,SIGNED) <= 50)
AND p.price <= 10174945 AND (p.advert_type_id = 1)
AND (c.id = 43 OR c.parent = 43)
GROUP BY p.id
ORDER BY p.price DESC
ok, the problem is in this asfv query part that are generated dynamically. It belong to objects which represent adverts by its specific fields. asfv is actually advert_specific_fields_values table (table name say all about it).
Without part:
AND (asfv.field_name = 't1_yearbuilt'
AND CONVERT(asfv.field_value,SIGNED) <= 2004 )
AND (asfv.field_name = 't1_sqft'
AND CONVERT(asfv.field_value,SIGNED) <= 50)
query return all adverts that belong on advert_type_id and price of them are less than 10.174.945,00 €.
All what I need is query update that return only adverts, for example t1_yearbuilt less than 2005 and t1_sqft less than 51 (advert_id => 654,656).
I also need query for values between for example t1_sqft >=30 AND t1_sqft <=50 (advert_id => 654).
Can anybody know how, update this query?
TNX
I've got this query and I want to SUM all the results of the query grouped by the column omschrijving.
The query
SELECT b.BoekRegelBedrag as total, c.omschrijving, ctl.vd1, b.BoekRegelId
FROM condensations as c
LEFT JOIN condensations_to_ledgers as ctl
ON ctl.vd1 = c.code
LEFT JOIN BoekstukRegels as b
ON b.BoekRegelGrootboekNr = ctl.GrootboekNummer
LEFT JOIN GrootboekRekeningen as g
ON g.GrootboekNummer = ctl.GrootboekNummer
WHERE c.bedrijf_id = 118
AND b.BoekregelUserId = 118
AND ctl.bedrijf_id = 118
AND g.GrootboekUserId = 118
AND c.code < 10
AND g.BaSoort = 2
AND b.BoekRegelPeriode BETWEEN 201000 AND 201013
GROUP BY b.BoekRegelId
Is there a simple way to do this?
EDIT
I tried to SUM BoekRegelBedrag but then each record sums up a part in one way or the other and i got 4 results instead of one result with the total of the summed column
Since you've not clearly stipulated which column(s) should be summed, we have to guess. Assuming that the BoekRegelId column should not be summed (it seldom makes sense to do arithmetic on ID numbers) — and then not summing ctl.vd1 per comment — then:
SELECT omschrijving, SUM(total) AS sum_total
FROM (SELECT b.BoekRegelBedrag as total, c.omschrijving, ctl.vd1, b.BoekRegelId
FROM condensations AS c
LEFT JOIN condensations_to_ledgers AS ctl
ON ctl.vd1 = c.code
LEFT JOIN BoekstukRegels AS b
ON b.BoekRegelGrootboekNr = ctl.GrootboekNummer
LEFT JOIN GrootboekRekeningen AS g
ON g.GrootboekNummer = ctl.GrootboekNummer
WHERE c.bedrijf_id = 118
AND b.BoekregelUserId = 118
AND ctl.bedrijf_id = 118
AND g.GrootboekUserId = 118
AND c.code < 10
AND g.BaSoort = 2
AND b.BoekRegelPeriode BETWEEN 201000 AND 201013
GROUP BY b.BoekRegelId
) AS I
GROUP BY omschrijving;
Basically, I'm using your original query result as a 'table' in the FROM clause, and then aggregating on its columns in a way which might be what you're after.
An alternative, simpler approach may also be feasible if the core query is close to what you wanted:
SELECT c.omschrijving, SUM(b.BoekRegelBedrag) as total
FROM condensations AS c
LEFT JOIN condensations_to_ledgers AS ctl ON ctl.vd1 = c.code
LEFT JOIN BoekstukRegels AS b ON b.BoekRegelGrootboekNr = ctl.GrootboekNummer
LEFT JOIN GrootboekRekeningen AS g ON g.GrootboekNummer = ctl.GrootboekNummer
WHERE c.bedrijf_id = 118
AND b.BoekregelUserId = 118
AND ctl.bedrijf_id = 118
AND g.GrootboekUserId = 118
AND c.code < 10
AND g.BaSoort = 2
AND b.BoekRegelPeriode BETWEEN 201000 AND 201013
GROUP BY c.omschrijving;
I am having a bit of a brain block with this problem and I am finding it hard to search for a solution because I cant phrase the question correctly to bring up the relevant information.
I am trying to get back "fProduct" record from the table below where it has a "fAttribute"
of 2 and 20.
id fAttribute fProduct
19 2 2967
48 2 2923
50 2 3008
51 20 3008
52 2 2295
53 20 2295
My statment below produces 0 results when I would expect to return fProduct's 2295 and 3008.
SELECT fProduct
FROM tableName
WHERE fAttribute = 2 AND fAttribute = 20
GROUP BY fProduct
Can anyone help please?
You can either use INNER JOINS or use EXISTS conditions:
INNER JOIN:
SELECT DISTINCT a.fProduct
FROM MyTable a
INNER JOIN MyTable b ON a.fProduct = b.fProduct AND b.fAttribute = 2
INNER JOIN MyTable c ON a.fProduct = c.fProduct AND c.fAttribute = 20
EXISTS:
SELECT afproduct
FROM MyTable a
WHERE EXISTS (SELECT b.id FROM MyTable b WHERE a.fProduct = b.fProduct AND b.fAttribute = 2)
AND EXISTS (SELECT c.id FROM MyTable c WHERE a.fProduct = c.fProduct AND c.fAttribute = 20)
A join should help:
SELECT distinct a.fProduct
FROM tableName as a
join tableName as b on b.product = a.product
WHERE a.fAttribute = 2 and b.fAttribute = 20
Since your are already doing a GROUP BY just change your WHERE clause to an OR or an IN and add the HAVING COUNT(fattribute) = 2 which makes sure it has both.
SELECT fproduct
FROM tablename
WHERE fattribute IN (2 , 20)
GROUP BY fproduct
HAVING COUNT(fattribute) = 2
I am having a problem with MySQL joins.
Table_A:
A_id Cost1 A1_id Cost2
1 500 0 200
1 100 1 100
1 50 2 60
1 10 3 50
2 5 0 10
Table_B (Refers B_id: from Table_A A1_id):
B_id FName LName
1 X A
2 Y B
3 Z C
Table_C (Refers C_id: from Table_A A_id):
C_id Towns
1 Atlanta
2 NewYork
I need to combine all three tables, like the following output:
I extract the Towns that match (Table_A.A_id=Table_C.C_id).
I extract the Fname,Lname that match (table_A.A1_id=Table_b.b_id).
I need to skip the Towns if A1_id != 0.
I need to skip the Fname,Lname if A1_id == 0.
The remaining data may either be a value or null, which I specify as '#'.
What would be an efficient MySQL query for the given scenario?
Output:
A_id Cost1 A1_id cost2 Fname Lname Towns
1 500 0 200 # # Atlanta
1 100 1 100 X A #
1 50 2 60 Y B #
1 10 3 50 Z C #
I think it should be something like this.
select A_id, Cost1, A1_id, cost2, Fname, Lname, Towns
from Table_A
left join Table_B on table_A.A1_id = Table_b.b_id
left join Table_C on Table_A.A_id = Table_C.C_id
This looks like a UNION of two distinct queries to me. I'm going to assume that the ID columns never contain negative values.
SELECT A.A_id, A.Cost1, A.A1_id, A.Cost2, B.Fname, B.Lname, C.Town
FROM Table_A AS A
INNER JOIN Table_B AS B ON A.A1_id = B.B_id
LEFT OUTER JOIN Table_C AS C ON A.A_id = C.C_id
WHERE A.A1_id != 0
AND C.C_id < 0
UNION
SELECT A.A_id, A.Cost1, A.A1_id, A.Cost2, B.Fname, B.Lname, C.Town
FROM Table_A AS A
LEFT OUTER JOIN Table_B AS B ON A.A1_id = B.B_id
INNER JOIN Table_C AS C ON A.A_id = C.C_id
WHERE A.A1_id = 0
AND B.B_id < 0;
I'm not completely confident about that formulation. There's a chance that the conditions on B.B_id < 0 and C.C_id < 0 need to be associated with the corresponding ON clauses.
There's also a decent chance that using two left outer joins in a single SELECT with appropriate OR'd filters would achieve the correct result.
If I understand correctly, it should be:
select fname, lname, towns
from table_a, table_b table_c
where table_a.a_id = table_c.c_id
and table_a.a1_id = table_b.b_id
and table_a.a1_id <> 0;