mysql calculation on cross tables - mysql

I've three tables in mysql with data as below, and I would like to know how do I get the following output. I don't know what's wrong with my coding?
select
rev.memberid,
(sum(rev.earned)/rule.revperpoint) - sum(redeem.redeempoint) as bal
from rev
left join rule on rev.rulename = rule.rulename
inner join redeem on rev.memberid = redeem.memberid
group by rev.memberid;
table1 rule
rulename revperpoint
CNY 2
NY 1
table2 rev
memberid earned rulename
37638899 500.50 CNY
37638899 400.50 CNY
25264833 300.50 CNY
2526833 600.50 CNY
table3 redeem
memberid redeempoint
25264833 100.00
25264833 50.00
expected output
memberid bal
25264833 300.50
37638899 450.50

Based on your desired result, the 4th line in table rev should be
25264833 600.50 CNY
And here you should not use inner join, cause it will only retrive record which memberid is 25264833, try this:
select
rev.memberid,
(sum(rev.earned) / rule.revperpoint ) - coalesce(redeem.redeempoint, 0) as bal
from rev
left join rule on rev.rulename = rule.rulename
left join (select memberid, sum(redeem.redeempoint) as redeempoint from redeem group by memberid) redeem on rev.memberid = redeem.memberid
group by rev.memberid;
And demo here.

Related

JOIN without Duplicate

I have 2 tables
fct_collection
fct_collection_dr_details
my target is to combine them without duplicate like this.
edited
but my output is this
here is my query
SELECT a.appkey,
a.drnumber,
a.amt,
b.prnumber,
b.prdate
FROM fct_collection_dr_details AS a
INNER JOIN
(SELECT MAX(appkey) AS 'appkey',
MAX(prnumber) AS 'prnumber',
prdate
FROM fct_collection
GROUP BY prdate) AS b ON a.appkey = b.appkey
I used to join them by appkey
here are the tables
fct_collection
appkey prnumber prdate
10/04/2017 15:49:2032s1gXox 1234567 2017-10-04
10/05/2017 14:29:142qmvYRJM 2356489 2017-10-05
fct_collection_dr_details
appkey drnumber amt
10/04/2017 15:49:2032s1gXox 12345 2500
10/05/2017 14:29:142qmvYRJM 234569 5000
10/05/2017 14:29:142qmvYRJM 698745 10000
Try left join
SELECT a.appkey, a.drnumber, a.amt, b.prnumber, b.prdate
FROM fct_collection_dr_details AS a
left JOIN
(SELECT MAX(appkey) AS 'appkey', MAX(prnumber) AS 'prnumber', prdate
FROM fct_collection GROUP BY prdate) AS b ON a.appkey = b.appkey

Join multiple tables without duplicate row

I would like to select 3 tables with the code number is 777
Table 1
Employee code Company Name
001 a
002 b
Table 2
Employee code Voucher NO Date Amount
001 123 12-4-14 100
001 456 2-5-14 500
002 789 3 -7 14 300
Table 3
Voucher No Tax amt code
123 50 777
789 100 888
The output should be
Company Name Employee code Voucher No Date Amount Tax amt code
a 001 123 12-4-14 100 50 777
a 001 456 2-5-14 500 null null
but there are duplicate row when apply the query
SELECT DISTINCT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
INNER JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
WHERE table3.CODE = '777'
The output from the above query
Company Name Employee code Voucher No Date Amount Tax amt code
a 001 123 12-4-14 100 50 777
a 001 456 2-5-14 500 **50 777**
I got try to use DISTINCT but since no work well. Please kindly help what problem with my query.
Try this :
SELECT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table1
INNER JOIN table2 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
HAVING
(table3.CODE = '777' OR table3.CODE IS NULL)
Leave me a comment if you need something else, or if you have a specific issue.
Your example output wasn't produced by your SQL command since you use WHERE statement to filter.
If you need to output only 777 code you should use LEFT JOIN with condition instead of WHERE
.....
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
AND (table3.CODE = '777')
Your second row in your output maybe because it has the same EMPLOYEE_CODE in table2, so I think this may the query that you want:
SELECT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
WHERE table1.EMPLOYEE_CODE IN
(SELECT table2.EMPLOYEE_CODE
FROM table2 INNER JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO)
WHERE table3.code = '777')

SQL Query -- Student Weighted Grade Calculation

I am having trouble calculating students grades together to get their final grade.
I have the following tables
Students
----------------
stu_id
stu_fname
stu_lname
Grades
----------------
grade_id
grade_name
grade_type
grade_possible
StudentGrades
-----------------
stu_grade_id
grade_id
stu_id
grade_earned
GradeTypes
----------------
grade_type
grade_type_name
grade_weight
This is the query that I have been able to come up with
Select S.stu_fname, S.stu_lname, GT.grade_type_name,
(ROUND((SUM(SG.grade_earned)/SUM(G.grade_possible)), 2) * ROUND((GT.grade_weight/100.0)
, 2) ) as CalculatedGrade
FROM Student S
INNER JOIN StudentGrade SG on SG.stu_id = S.stu_id
INNER JOIN Grade G on SG.grade_id = G.grade_id
INNER JOIN GradeType GT WHERE G.grade_type = GT.grade_type
GROUP BY S.stu_fname, S.stu_lname, GT.grade_type_name;
I get the query report below
James | Fort | HW/QUIZ | 30.0
James | Fort | LogBook | 60.0
Robin | Hood | HW/QUIZ | 60.0
Robin | Hood | Logbook | 25.0
I want to be able to add both of James Forts grades together to get his final grade and the same for Robin Hood.
Any help is appreciated, I am stuck at this point. I am almost done. I have researched sub queries and need more help to narrow my search to get the answer.
Have you tried the following ?
SELECT results.stu_fname, results.stu_lname, sum(results.CalculatedGrade)
FROM(
SELECT S.stu_fname, S.stu_lname, GT.grade_type_name,
(ROUND((SUM(SG.grade_earned)/SUM(G.grade_possible)), 2) * ROUND((GT.grade_weight/100.0)
, 2) ) as CalculatedGrade
FROM Student S
INNER JOIN StudentGrade SG on SG.stu_id = S.stu_id
INNER JOIN Grade G on SG.grade_id = G.grade_id
INNER JOIN GradeType GT WHERE G.grade_type = GT.grade_type
GROUP BY S.stu_fname, S.stu_lname, GT.grade_type_name
)results
GROUP BY results.stu_fname, results.stu_lname;
Edit: added aliases thanks to AshReva's remark.
Well, just remove GT.grade_type_name from the select and group by. Does this do what you need?
Select S.stu_fname, S.stu_lname,
(ROUND((SUM(SG.grade_earned)/SUM(G.grade_possible)), 2) * ROUND((GT.grade_weight/100.0)
, 2) ) as CalculatedGrade
FROM Student S INNER JOIN
StudentGrade SG
on SG.stu_id = S.stu_id INNER JOIN
Grade G
on SG.grade_id = G.grade_id INNER JOIN
GradeType GT
on G.grade_type = GT.grade_type
GROUP BY S.stu_fname, S.stu_lname;

how to write the select statement in case statement

Let me know how to write select query in case statement.
select
ROW_NUMBER() OVER(Order by vendor.VendorName ) AS ID,
PH.PurchasingHeaderID as BILLNo,
PH.TotalPriceCompanyCurrency as Balance,
acc.AccountName as [Account_Name]
**Into #tempOpenVedorlist**
from PurchasingHeader PH
LEFT OUTER JOIN TransactionType Trans ON PH.TransactionTypeID =Trans.TransactionTypeID
LEFT OUTER JOIN Vendor vendor on PH.VendorID=vendor.VendorID
LEFT OUTER JOIN PaymentTerm PT on PT.PaymentTermID = vendor.PaymentTermID
LEFT OUTER JOIN PurchasingDetail PD on PD.PurchasingHeaderID = PH.PurchasingHeaderI
LEFT OUTER JOIN Account Acc on Acc.AccountID= PD.FinancialAccountID
where PH.TransactionTypeID=7
Group by vendor.VendorName,
PH.PurchasingHeaderID,PH.TotalPriceCompanyCurrency,acc.AccountName
I GOT THIS RESULT :
with this result: Here i have No : VB1003 two times but Account Name is different.
ID BILLNo Account_Name Balance
-------------------------------------------------------------
101 VB1000 Cash-Petty Cash 4000.00
102 VB1001 Accounts Receivable 5000.00
103 VB1003 Cash-PettyCash 6000.00
104 VB1003 Cash 6000.00
105 VB1004 UndepositedFunds 7000.00
Here i have to show ;
I need this result :
ID BILLNo Account_Name Balance
------------------------------------------------------
101 VB1000 Cash-PettyCash 4000.00
102 VB1001 AccountsReceivable 5000.00
103 VB1003 ---Multiple---- 6000.00
104 VB1004 UndepositedFunds 7000.00
For aboue result what i have did : i have taken all the data in temp table.
Am able show Multiple string for which has more than one No.
But unfortunatly am not able show Account Name for which has only one BILLNo.
select ROW_NUMBER() OVER(Order by BILLNo ) AS ID,
[BILLNo],
Balance,
CASE
WHEN count(BILLNo)>1 THEN 'Multipul'
WHEN count(BILLNo)<2 THEN **(Select Account_Name from #tempOpenVedorlist )**
End As [Financial_Account]
from #tempOpenVedorlist
Group By BILLNo,Balance
Let me know how can i get account name from temp table which is related to that BILLNo.
Remove the AccountName from the GROUP BY and put an aggregate on that.
SELECT ROW_NUMBER() OVER(Order by vendor.VendorName ) AS ID,
PH.PurchasingHeaderID as BILLNo,
PH.TotalPriceCompanyCurrency as Balance,
CASE WHEN MIN(acc.AccountName) IS NULL
THEN '----'
WHEN MIN(acc.AccountName) = MAX(acc.AccountName)
THEN MIN(acc.AccountName)
ELSE '--MULTIPLE--'
END as [Account_Name]
INTO #tempOpenVedorlist
FROM PurchasingHeader PH
LEFT OUTER JOIN TransactionType Trans
ON PH.TransactionTypeID =Trans.TransactionTypeID
LEFT OUTER JOIN Vendor vendor
ON PH.VendorID=vendor.VendorID
LEFT OUTER JOIN PaymentTerm PT
ON PT.PaymentTermID = vendor.PaymentTermID
LEFT OUTER JOIN PurchasingDetail PD
ON PD.PurchasingHeaderID = PH.PurchasingHeaderI
LEFT OUTER JOIN Account Acc
ON Acc.AccountID= PD.FinancialAccountID
WHERE PH.TransactionTypeID=7
GROUP BY vendor.VendorName,
PH.PurchasingHeaderID,
PH.TotalPriceCompanyCurrency

sql query implement two count in one

SELECT (select count(u.ag_code)
from table1 as u inner join table2 as tc
on u.industry_id=tc.tempcatid
where u.ag_code!=0) as agnt,
(select count(u.ag_code)
from table1 as u inner join table2 as tc
on u.industry_id=tc.tempcatid where u.ag_code=0),as dircus,
tc.catename from table1 as u inner join table2 as tc
where u.industry_id=tc.tempcatid
group by tc.tempcatid
this query have error
i need two count and category name in one query
this is the condition for count
ag_code!=0
ag_code=0
in table1 have column ag_code (this have 0 and nonzero value)
my result need like this
Full Texts
agent customer catename
11 3 Real Estate
15 1 Automobile
3 0 Medical
34 77 Business
1 45 Travel & Hotels
11 3 Construction & Engineering
SELECT tc.catename,
count(case when u.ag_code!=0 then 1 end) agnt,
count(case when u.ag_code =0 then 1 end) dircus
from table1 as u
inner join table2 as tc on u.industry_id=tc.tempcatid
group by tc.tempcatid, tc.catename
Hi There you might be able to use the below example to get back the result you require.
Select SUM(Inactive) Inactive ,SUM(Active) Active FROM
(
Select Count(t1.UserId) Inactive,0 Active
FROM
(select * from users where inactive=1) as t1
UNION
SELECT 0 Inactive,Count(t2.UserId) Active FROM
(select * from users where inactive=0) as t2
) as result