I have a log of points that I want to import somewhere else as (email - total points).
I'm trying to figure out a query that does the following:
Calculate points for EACH user_id, and merging those user_ids so they won't be duplicated.
After that, I need to replace the USER_IDs with corresponding emails in another table:
Source Table
USER_ID Points
------- ------
1 10
2 30
3 50
1 -5
2 5
3 -40
Desired result
USER_ID Points
------- ------
1 5
2 35
3 10
STEP 2
Another Source Table
USER_ID Email
------- ------
1 one#one.com
2 two#two.com
3 three#three.com
Final Desired result:
USER_ID Total Points
------- -----------
one#one.com 5
two#two.com 35
three#three.com 10
To pretify your query you can write like that :
select oc_customer.email,
sum(oc_customer_reward.points) as Total_points
from oc_customer_reward
inner join oc_customer using(customer_id)
group by customer_id
Related
I have 2 tables which have not relation both of them.
Table of income
Id Category Nominal Description Date
---- -------- -------- -------- --------
1 ADD 10000 Q1 2020-03-05
2 DD 15000 Q2 2020-05-11
3 PAD 5000 Q3 2020-08-10
Table of outcome
Id Category Nominal Description Date
---- -------- -------- -------- --------
1 ADD 7000 Q1 2020-03-20
2 DD 10000 Q2 2020-06-02
3 PAD 2000 Q3 2020-08-28
So, I want to do subtraction of nominal from income with nominal from outcome group by quarter.
Here is my query :
CREATE view Total AS
SELECT QUARTER(outcome.date) AS Qperiod, income.nominal-outcome.nominal AS remain
FROM income, outcome
GROUP BY YEAR(outcome.date), QUARTER(outcome.date)
this result shown below, it describe that first row in income table subtraction by all outcome nominal.
Qperiod remain
---- --------
1 3000
2 0
3 8000
Can anyone help me to solve this?
You have no JOIN condition in your query, so each row of income gets matched to every row of outcome. Since you have no aggregation function, this effectively means that a random row from outcome is subtracted from each row of income. You should use modern, explicit JOIN syntax and put in the appropriate JOIN condition, which is that the year and quarter are the same in both tables:
CREATE view Total AS
SELECT QUARTER(outcome.date) AS Qperiod, income.nominal-outcome.nominal AS remain
FROM income
JOIN outcome ON YEAR(outcome.date) = YEAR(income.date)
AND QUARTER(outcome.date) = QUARTER(income.date)
GROUP BY YEAR(outcome.date), QUARTER(outcome.date)
Output:
Qperiod remain
1 3000
2 5000
3 3000
Demo on SQLFiddle
I'm having a Two Tables one is Sales and another one is BrandMaster
My Requirement : How to Sum a single Column based on grouping in MySQL
The Structure and Data of Sales Table:
SNo BID Amount
-----------------------------------
101 1 200
102 2 500
103 5 800
104 8 250
105 1 200
106 2 500
107 5 800
108 8 250
The Structure and Data of BrandMaster Table:
BID BrandName
-------------------------
1 Prod#1
2 Prod#2
3 Prod#3
4 Prod#4
5 Prod#5
6 Prod#6
7 Prod#7
8 Prod#8
My Expected Output:
BID SumAmount
-------------------------
1 400
2 500
3 0
4 0
5 0
6 1600
7 0
8 500
The Sales Table contains the BrandID 'BID' and the Sales Amount with Sales ID 'SNo'. I need the Sum of Sales Amount for each Product. Kindly assist me.
You can try this
SqlFiddle Demo
SELECT
BrandMaster.BID,
IFNULL(SUM(Sales.Amount),0) AS SumAmount
FROM BrandMaster
LEFT JOIN Sales ON ( BrandMaster.BID=Sales.BID )
GROUP BY
BrandMaster.BID
ORDER BY
BrandMaster.BID,
Sales.SNo
Here you go!
SELECT db.sales.bid AS "BID",
<AGGREGATE>(db.sales.amount) AS "SumAmount"
FROM db.sales
GROUP BY db.sales.bid
ORDER BY db.sales.bid;
Edit: db is your database, and sales is your, "Sales," table.
Second Edit: replace <AGGREGATE> with something from here.
I want to display records that have more than one occurence, basing them off of a column.
For Example:
id user_id
------ --------
1 FA068
2 FA068
3 FA068
4 FA145
5 FA111
6 FA200
7 FA200
8 FA155
9 FA145
10 FA145
And I want this to be the output:
id user_id
------ --------
1 FA068
2 FA068
3 FA068
6 FA200
7 FA200
9 FA145
10 FA145
I want to display all the records of those that have more than one occurrence basing from the user_id column.
Please help. Thanks!
something like:
select * from my_table where user_id in
(select user_id from my_table group by user_id having count(user_id) > 1)
You can easily count records and use HAVING clause
SELECT `user_id`, COUNT(*) AS num
FROM `table`
GROUP BY user_id
HAVING num > 1
Considering the following query:
SELECT t.recording_id, m.release_id
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
i get a result set similar to this one
recording id release id
----------------------------------
1 25
1 25
1 37
1 76
1 300
1 336
2 37
... ...
i need to output the following
recording id count
---------------------------------------------------
1 5
2 1
In other words, i need to group by the recording_id but not count the release_id duplicates for that recording_id
After researching this board i've tried the following, with no success :
SELECT t.recording_id, count(t.recording_id)
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
group by t.recording_id, m.release_id
but, im getting
recording id release id
--------------------------
1 2
1 1
1 1
1 1
1 1
2 1
What's wrong?
Try this, you can use distinct in your count function to return distinct release ids for a recording_id
SELECT t.recording_id, count(distinct m.release_id) cnt
FROM track t
JOIN medium m ON t.medium_id = m.medium_id
group by t.recording_id
I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.