thanks in advance for your help on this !
I have 2 tables that I need to join based on 2 different SELECTs
First Select:
SELECT
Agent,
count( Online_playerdatabase_v2.First_Deposit_Date) as NumbFirstDeposits
FROM Online_playerdatabase_v2
WHERE Agent <>'Agent' AND Online_playerdatabase_v2.First_Deposit_Date BETWEEN '2015-12-01' AND '2015-12-31'
group by Agent
Output:
Agent NumbFirstDeposits
john 49
No Agent 1
mike 9
Then another Select:
SELECT
Agent,
COUNT(DISTINCT Online_customer_activity_v2.Customers) as ActivePlayers,
Truncate(sum(Online_customer_activity_v2.Real_Money),0) as RM,
Truncate(sum(Online_customer_activity_v2._Bonuses),0) as BO,
Truncate(sum(Online_customer_activity_v2.Total_Win_Loss),0) as GGR,
Truncate(sum(`Online_customer_activity_v2`.`Total_Bets`),0) as BETS
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Date` BETWEEN '2015-12-01' AND '2015-12-31'
AND Online_customer_activity_v2.Total_Bets>0
GROUP BY Agent
Output:
Agent ActivePlayers RM BO GGR BETS
john 73 63118 28538 64 1395799
No Agent 1 80 0 - 21 876
mike 24 209780 28464 20 7955633
I would like to have a Join of the 2 outputs as below:
Agent ActivePlayers RM BO GGR BETS NumbFirstDeposits
john 73 63118 28538 64 1395799 49
No Agent 1 80 0 - 21 876 1
mike 24 209780 28464 20 7955633 9
One way to do this without having to rewrite the queries is to use them both at derived tables like this:
select t1.*, t2.NumbFirstDeposits
from (<<your first query here>>) t1
join (<<your second query here>> t2 on t1.agent = t2.agent
Try to apply join like this way:
SELECT ***your_column_names_here***
FROM Online_playerdatabase_v2 as P
join Online_customer_activity_v2 as C on P.Agent=C.agent
WHERE
***conditions***
Group BY p.Agent
You can try this,
SELECT Online_customer_activity_v2.Agent,
COUNT(DISTINCT Online_customer_activity_v2.Customers) as ActivePlayers,
Truncate(sum(Online_customer_activity_v2.Real_Money),0) as RM,
Truncate(sum(Online_customer_activity_v2._Bonuses),0) as BO,
Truncate(sum(Online_customer_activity_v2.Total_Win_Loss),0) as GGR,
Truncate(sum(`Online_customer_activity_v2`.`Total_Bets`),0) as BETS,
count( Online_playerdatabase_v2.First_Deposit_Date) as NumbFirstDeposits FROM Online_customer_activity_v2 join Online_playerdatabase_v2 on Online_playerdatabase_v2.agent=Online_customer_activity_v2.agent WHERE `Online_customer_activity_v2`.`Date` BETWEEN '2015-12-01' AND '2015-12-31' AND Online_customer_activity_v2.Total_Bets>0 GROUP BY Online_customer_activity_v2.Agent
Related
I am new with mysql and working to change a store application to make it have two stock. I created a table to store stock quantity:
Then I plan to create a view with stock quantity, per store, per SKU. I using the following query:
SELECT
`stockList`.`sku`,
SUM(A.`stockQty`) AS 'store1',
SUM(B.`stockQty`) AS 'store2',
SUM(`stockList`.`stockQty`) AS 'total'
FROM `stockList`
LEFT JOIN (
SELECT * FROM `stockList` WHERE `idStock`=1
) AS A
ON `stockList`.`sku`=A.`sku`
LEFT JOIN (
SELECT * FROM `stockList` WHERE `idStock`=2
) AS B
ON `stockList`.`sku`=B.`sku`
GROUP BY `stockList`.`sku`
Per resulting table, calculation is not proper and I could not identify the logic:
SKU 43 should show for store1 = 9 and for store2 = 10, total = 19. This is what they show if I execute the select queries alone. Please, let me know if I misunderstood how this sum logic works.
You might to use SUM on subquery to calculate Totle price by sku
LEFT JOIN may make some fields not match causing NULL so use IFNULL to preset value 0
You can try this.
SELECT
T.sku,
SUM(T.stockQty) as totle,
IFNULL(A.`store1`,0) AS `store1`,
IFNULL(B.`store2`,0) AS `store2`
FROM `stockList` AS T
LEFT JOIN
(
SELECT sku,SUM(`stockQty`) as `store1`
FROM `stockList`
WHERE `idStock`=1
GROUP BY sku
) as A ON A.sku = T.sku
LEFT JOIN
(
SELECT sku,SUM(`stockQty`) as `store2`
FROM `stockList`
WHERE `idStock`=2
GROUP BY sku
) AS B ON T.sku =B.sku
GROUP BY T.sku
sqlfiddle
Your query is much more complicated than it needs to be. You can just do this:
SELECT
sku,
SUM(stockQty) as total,
SUM(IF(idStock=1,stockQty,0)) AS `store1`,
SUM(IF(idStock=2,stockQty,0)) AS `store2`
FROM `stockList`
GROUP BY sku
Output:
sku total store1 store2
36 10 10 0
37 3 3 0
38 4 4 0
39 3 3 0
40 10 10 0
41 12 12 0
42 12 12 0
43 19 9 10
I have some problem from using the inner join with group by using max function.
my detail :
Table inventory
site_ID tank_number volume times-tramp ID
1 1 5000 06/08/2017 15:00 1
1 1 4900 06/08/2017 15:01 2
1 2 6000 06/08/2017 15:05 3
1 3 4000 06/08/2017 15:05 4
2 1 3000 06/08/2017 15:33 5
2 2 2000 06/08/2017 15:34 6
1 1 4800 06/08/2017 15:36 7
1 2 5800 06/08/2017 16:00 8
Table wp_users (wordpress)
ID Name
1 aaa
2 bbbb
Now, I using with wpdatatable plugin in wordpress so i want use the inner join and group with tank_number and using data update by times-strap
The result should be is :
When user "aaa" login then ID will is 1
the result that want to show.
site_ID tank_number volume times-tramp
1 1 4800 06/08/2017 15:36
1 2 5800 06/08/2017 16:00
1 3 4000 06/08/2017 15:05
But
I got the this result :
site_ID tank_number volume times-tramp
1 1 5000 06/08/2017 15:00
1 2 6000 06/08/2017 15:05
1 3 4000 06/08/2017 15:05
So can every one help to advise me so I have show my code the showing the error due to i can't join with 2 table by correct syntax sql.
this code can work without inner join
select site_id, tank_product, volume, timestramp from inventory as t1
inner join (
select tank_product as tank, max(timestramp) as time from inventory
where site_id=1 group by tank_product) as t2
on t1.tank_product = t2.tank and t1.timestramp = t2.time and t1.site_id=1
and Now I trying to using with inner join but not work now.
SELECT inventory.site_id,
inventory.tank_product,
inventory.volume,
inventory.timestramp,
wp_users.ID
FROM inventory as t1
INNER JOIN wp_users
(SELECT inventory.tank_product, max(inventory.timestramp) as time from inventory
WHERE inventory.site_id=wp_users.ID GROUP BY inventory.tank_product) as t2
ON t1.inventory.tank_product=t2.tank AND t1.inventory.timestramp=t2.time AND t1.inventory.site_id=wp_users.ID
Can advise me for correct way to use the inner join by group by max function.
So, the schema you present and the queries you provided don't line up (naming is all over the place). It would be very helpful if you provided a sql fiddle to help present your problem (at least in the future).
In any event, I think you were close:
select t2.site_id, t2.tank_product, t1.volume, t2.time
from (
select site_id, tank_product, max(timestramp) as time
from inventory
group by site_id,tank_product) t2
inner join inventory t1
on t2.site_id=t1.site_id and t1.tank_product=t2.tank_product and t2.time=t1.timestramp
inner join wp_users u
on u.id=t2.site_id
where u.id=1
order by t2.site_id,t2.tank_product asc
(Assuming you pass the wp_users.id into the query; I've hard-coded that with u.id=1.)
fiddle
Try to run this:
SELECT i1.site_ID, i1.tank_product, i1.volume, i.timestramp, iu.User_ID
FROM Inventory AS i1
INNER JOIN (
SELECT u.ID AS UserID, i.tank_product, MAX (i.timestramp) AS time
FROM Inventory AS i2
INNER JOIN wp_users AS u
ON i2.site_id = u.ID
GROUP BY u.ID, i.tank_product
) as iu
ON i1.tank_product = iu.tank_product
AND i1.timestramp = iu.time
AND i1.site_ID = iu.UserID;
I have a data set with name and their transaction ,how to get average and count of transactions grater than that average and less than that average..
Name Transaction
John 12
John 34
John 45
John 66
John 32
chris 26
chris 54
chris 56
chris 99
chris 13
chris 4
kim 22
kim 34
kim 7
kim 11
kim 34
O/P will be
Name Avg Count_greater_than_Avg Count_Less_than_Avg
John 37.8 2 3
chris 42 3 3
kim 21.6 3 2
Thanks in advance..
Try this:
SELECT t1.Name, t2.Aver,
COUNT(CASE WHEN Transaction < Aver THEN 1 END) Count_Less_than_Avg,
COUNT(CASE WHEN Transaction > Aver THEN 1 END) Count_greater_than_Avg
FROM mytable AS t1
JOIN (
SELECT Name, AVG(Transaction * 1.0) AS Aver
FROM mytable
GROUP BY Name
) AS t2 ON t1.Name = t2.Name
GROUP By Name
You need a derived table in order to calculate the average value per Name. You can then JOIN the original table to this derived table and use conditional aggregation in order to calculate less than, greater than number of transactions.
Demo here
This basically first add a column Your_Avg using a correlated query, and then wrap it with another select to select the count of the occurrences of times smaller then avg and times larger.
SELECT tt.name,tt.Your_Avg,
count(CASE WHEN tt.Your_Avg > tt.Transaction then 1 end) as Greater_Then_Avg,
count(CASE WHEN tt.Your_Avg > tt.Transaction then 1 end) as Smaller_Then_Avg
FROM(
SELECT t.name,
(SELECT avg(s.transaction) FROM YourTable s WHERE s.name = t.name) as Your_Avg,
t.transaction
FROM YourTable) tt
GROUP BY tt.name
i have a scenario where there are 2 tables
tblMember with 2 columns MemberID & MemberName
100 Aakash
101 Seema
tblLoanHistory with columns LoanID, MemberID, FineCharged
1 100 30
2 100 60
3 101 30
I want to find the member whose total fine charged is highest.
output should be
100 Aakash 90 (60+30=90)
can anybody please help
thanks in advance.
SELECT TOP 1
M.MemberID, M.MemberName,
SUM(LH.FineCharged)
FROM
tblMember M
JOIN
tblLoanHistory LH ON LH.MemberID=M.MemberID
GROUP BY
M.MemberID, M.MemberName
ORDER BY
SUM(LH.FineCharged) DESC
Update... Oops! Added JOIN condition
SELECT M.MemberID ,M.MemberName,SUM(LH.FineCharged) FineCharged
FROM tblMember M
INNER JOIN tblLoanHistory LH ON LH.MemberID=M.MemberID
GROUP BY M.MemberID, M.MemberName
SELECT
C.CompanyId,
CompanyName,
Server,
ServerUsers,
ServerUptime,
ServerHostName,
ServerType
FROM
CUSTOMERS AS C
INNER JOIN
USERS ON C.CompanyId = USERS.CompanyId
WHERE
USERS.UserEmail='matt' AND
USERS.UserPin='5153' AND
(SELECT Status FROM 4321_BlackBerryServices LIMIT 0,1)
LIMIT 0, 8
Currently my table is below
4321 T1 Solutions EXCH-01 392 47 days, 17 min exch01.myCorp.com ExchangeServices
4321 T1 Solutions EXCH-02 685 47 days, 17 min exch02.myCorp.com ExchangeServices
4321 T1 Solutions Lync-01 368 47 days, 17 min lync01.myCorp.com LyncServices
4321 T1 Solutions Lync-02 890 458 days, 58 min lync02.myCorp.com LyncServices
What i would like to do is add the last result from Status FROM 4321_BlackBerryServices so i would want to add the latest status for each server in my table
how is this possible ?
If I understand what you need, try this:
SELECT
C.CompanyId, CompanyName, Server, ServerUsers,
ServerUptime, ServerHostName, ServerType,
(SELECT Status FROM 4321_BlackBerryServices
ORDER BY field_you_know DESC LIMIT 0,1) AS BBS_Status
FROM CUSTOMERS AS C
INNER JOIN USERS
ON C.CompanyId = USERS.CompanyId
AND USERS.UserEmail = 'matt'
AND USERS.UserPin = '5153'
LIMIT 0, 8
In my query field_you_know field is the one you know you can sort the table to take the last one.