Get max value per id [duplicate]











up vote
2
down vote

favorite
1













This question already has an answer here:




  • SQL counting all rows instead of counting individual rows

    1 answer




I have this query :



BEGIN 
DROP TABLE IF EXISTS `tblquota`;
CREATE TABLE IF NOT EXISTS tblquota (id INT AUTO_INCREMENT PRIMARY KEY) AS
SELECT c.id as user_id
, c.`email`
, h.`packageid` as pack_id
, p.`name` as pack_name
, max(CASE
WHEN `name` = "Basic" THEN '100'
WHEN `name` = "Silver" THEN '100'
WHEN `name` = "Gold" THEN '100'
ELSE '10'
END) as quota
FROM `tblclients` c
LEFT JOIN `tblhosting` h ON c.id = h.userid
INNER JOIN `tblproducts` p ON h.packageid = p.id
ORDER BY c.id;
END


In this case, the query will only get the max value for only one customer. I would like to get the value per customer.



table clients:



id | email          | status
----------------------------
1 | user1@mail.com | Active
2 | user2@mail.com | Inactive
3 | user3@mail.com | Active


table tblhosting



id | userid | packageid | domainstatus 
------------------------------------------------
1 | 1 | 3 | Active
2 | 1 | 2 | Active
3 | 2 | 1 | Active
4 | 2 | 2 | Active
5 | 2 | 3 | Inactive
6 | 3 | 1 | Active


table tblproducts



id | name 
-----------
1 | Basic
2 | Silver
3 | Gold


I expect result like :



id | user_id | email          | pack_id | pack_name | quota
-----------------------------------------------------------
1 | 1 | user1@mail.com | 3 | Gold | 1000
2 | 2 | user2@mail.com | 2 | Silver | 100
3 | 3 | user3@mail.com | 1 | Basic | 10









share|improve this question















marked as duplicate by Barmar mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    You need GROUP BY c.id.
    – Barmar
    yesterday















up vote
2
down vote

favorite
1













This question already has an answer here:




  • SQL counting all rows instead of counting individual rows

    1 answer




I have this query :



BEGIN 
DROP TABLE IF EXISTS `tblquota`;
CREATE TABLE IF NOT EXISTS tblquota (id INT AUTO_INCREMENT PRIMARY KEY) AS
SELECT c.id as user_id
, c.`email`
, h.`packageid` as pack_id
, p.`name` as pack_name
, max(CASE
WHEN `name` = "Basic" THEN '100'
WHEN `name` = "Silver" THEN '100'
WHEN `name` = "Gold" THEN '100'
ELSE '10'
END) as quota
FROM `tblclients` c
LEFT JOIN `tblhosting` h ON c.id = h.userid
INNER JOIN `tblproducts` p ON h.packageid = p.id
ORDER BY c.id;
END


In this case, the query will only get the max value for only one customer. I would like to get the value per customer.



table clients:



id | email          | status
----------------------------
1 | user1@mail.com | Active
2 | user2@mail.com | Inactive
3 | user3@mail.com | Active


table tblhosting



id | userid | packageid | domainstatus 
------------------------------------------------
1 | 1 | 3 | Active
2 | 1 | 2 | Active
3 | 2 | 1 | Active
4 | 2 | 2 | Active
5 | 2 | 3 | Inactive
6 | 3 | 1 | Active


table tblproducts



id | name 
-----------
1 | Basic
2 | Silver
3 | Gold


I expect result like :



id | user_id | email          | pack_id | pack_name | quota
-----------------------------------------------------------
1 | 1 | user1@mail.com | 3 | Gold | 1000
2 | 2 | user2@mail.com | 2 | Silver | 100
3 | 3 | user3@mail.com | 1 | Basic | 10









share|improve this question















marked as duplicate by Barmar mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    You need GROUP BY c.id.
    – Barmar
    yesterday













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1






This question already has an answer here:




  • SQL counting all rows instead of counting individual rows

    1 answer




I have this query :



BEGIN 
DROP TABLE IF EXISTS `tblquota`;
CREATE TABLE IF NOT EXISTS tblquota (id INT AUTO_INCREMENT PRIMARY KEY) AS
SELECT c.id as user_id
, c.`email`
, h.`packageid` as pack_id
, p.`name` as pack_name
, max(CASE
WHEN `name` = "Basic" THEN '100'
WHEN `name` = "Silver" THEN '100'
WHEN `name` = "Gold" THEN '100'
ELSE '10'
END) as quota
FROM `tblclients` c
LEFT JOIN `tblhosting` h ON c.id = h.userid
INNER JOIN `tblproducts` p ON h.packageid = p.id
ORDER BY c.id;
END


In this case, the query will only get the max value for only one customer. I would like to get the value per customer.



table clients:



id | email          | status
----------------------------
1 | user1@mail.com | Active
2 | user2@mail.com | Inactive
3 | user3@mail.com | Active


table tblhosting



id | userid | packageid | domainstatus 
------------------------------------------------
1 | 1 | 3 | Active
2 | 1 | 2 | Active
3 | 2 | 1 | Active
4 | 2 | 2 | Active
5 | 2 | 3 | Inactive
6 | 3 | 1 | Active


table tblproducts



id | name 
-----------
1 | Basic
2 | Silver
3 | Gold


I expect result like :



id | user_id | email          | pack_id | pack_name | quota
-----------------------------------------------------------
1 | 1 | user1@mail.com | 3 | Gold | 1000
2 | 2 | user2@mail.com | 2 | Silver | 100
3 | 3 | user3@mail.com | 1 | Basic | 10









share|improve this question
















This question already has an answer here:




  • SQL counting all rows instead of counting individual rows

    1 answer




I have this query :



BEGIN 
DROP TABLE IF EXISTS `tblquota`;
CREATE TABLE IF NOT EXISTS tblquota (id INT AUTO_INCREMENT PRIMARY KEY) AS
SELECT c.id as user_id
, c.`email`
, h.`packageid` as pack_id
, p.`name` as pack_name
, max(CASE
WHEN `name` = "Basic" THEN '100'
WHEN `name` = "Silver" THEN '100'
WHEN `name` = "Gold" THEN '100'
ELSE '10'
END) as quota
FROM `tblclients` c
LEFT JOIN `tblhosting` h ON c.id = h.userid
INNER JOIN `tblproducts` p ON h.packageid = p.id
ORDER BY c.id;
END


In this case, the query will only get the max value for only one customer. I would like to get the value per customer.



table clients:



id | email          | status
----------------------------
1 | user1@mail.com | Active
2 | user2@mail.com | Inactive
3 | user3@mail.com | Active


table tblhosting



id | userid | packageid | domainstatus 
------------------------------------------------
1 | 1 | 3 | Active
2 | 1 | 2 | Active
3 | 2 | 1 | Active
4 | 2 | 2 | Active
5 | 2 | 3 | Inactive
6 | 3 | 1 | Active


table tblproducts



id | name 
-----------
1 | Basic
2 | Silver
3 | Gold


I expect result like :



id | user_id | email          | pack_id | pack_name | quota
-----------------------------------------------------------
1 | 1 | user1@mail.com | 3 | Gold | 1000
2 | 2 | user2@mail.com | 2 | Silver | 100
3 | 3 | user3@mail.com | 1 | Basic | 10




This question already has an answer here:




  • SQL counting all rows instead of counting individual rows

    1 answer








mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









executable

899221




899221




marked as duplicate by Barmar mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Barmar mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    You need GROUP BY c.id.
    – Barmar
    yesterday














  • 1




    You need GROUP BY c.id.
    – Barmar
    yesterday








1




1




You need GROUP BY c.id.
– Barmar
yesterday




You need GROUP BY c.id.
– Barmar
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote













What you are looking for is Max OVER (PARTITION BY user_id)






share|improve this answer





















  • Can you show how to use it ?
    – executable
    yesterday










  • Max(case when..…..) over (partition by user_id) as quota
    – Kristjan Kica
    yesterday


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













What you are looking for is Max OVER (PARTITION BY user_id)






share|improve this answer





















  • Can you show how to use it ?
    – executable
    yesterday










  • Max(case when..…..) over (partition by user_id) as quota
    – Kristjan Kica
    yesterday















up vote
0
down vote













What you are looking for is Max OVER (PARTITION BY user_id)






share|improve this answer





















  • Can you show how to use it ?
    – executable
    yesterday










  • Max(case when..…..) over (partition by user_id) as quota
    – Kristjan Kica
    yesterday













up vote
0
down vote










up vote
0
down vote









What you are looking for is Max OVER (PARTITION BY user_id)






share|improve this answer












What you are looking for is Max OVER (PARTITION BY user_id)







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Kristjan Kica

2,0311826




2,0311826












  • Can you show how to use it ?
    – executable
    yesterday










  • Max(case when..…..) over (partition by user_id) as quota
    – Kristjan Kica
    yesterday


















  • Can you show how to use it ?
    – executable
    yesterday










  • Max(case when..…..) over (partition by user_id) as quota
    – Kristjan Kica
    yesterday
















Can you show how to use it ?
– executable
yesterday




Can you show how to use it ?
– executable
yesterday












Max(case when..…..) over (partition by user_id) as quota
– Kristjan Kica
yesterday




Max(case when..…..) over (partition by user_id) as quota
– Kristjan Kica
yesterday



Popular posts from this blog

Write to the output between two pipeline

How to check Newtonsoft.Json.Linq.JArray If null Or get length

How to test boost logger output in unit testing?