Hi i have following table on mysql DB.
╔═══════════╦═════════╦════════╦════════════════╗
║ REVIEW_ID ║ USER_ID ║ STATUS ║ DATE_ADDED ║
╠═══════════╬═════════╬════════╬════════════════╣
║ 218 ║ 2 ║ cool ║ 20130121134811 ║
║ 218 ║ 2 ║ cool ║ 20130121134812 ║
║ 218 ║ 2 ║ lame ║ 20130121134813 ║
║ 218 ║ 2 ║ funny ║ 20130121134814 ║
║ 218 ║ 2 ║ funny ║ 20130121134815 ║
║ 218 ║ 2 ║ funny ║ 20130121134816 ║
║ 218 ║ 2 ║ lame ║ 20130121134817 ║
╚═══════════╩═════════╩════════╩════════════════╝
how can i get a result where when i do query based on user_id i need to get result of total status for each type:
╔════════╦════════════╗
║ STATUS ║ TOTALCOUNT ║
╠════════╬════════════╣
║ cool ║ 2 ║
║ funny ║ 3 ║
║ lame ║ 2 ║
╚════════╩════════════╝
Thanks
Use COUNT() which is an aggregate function, and group them according to their status
SELECT status, COUNT(*) totalCount
FROM tableName
GROUP BY status
SQLFiddle Demo
OTHER(s)
MySQL GROUP BY with some Aggregate Functions List
SELECT status, count(*)
FROM your_table
GROUP BY status
SELECT status, count(*)
FROM yourtable
GROUP BY status
;
SELECT STATUS, COUNT(*) AS TOTALCOUNT
FROM tableName
GROUP BY STATUS
HAVING USER_ID = user_id you need
Related
I want to find all ids of which group_concat only contains 'a'.
Here is simplified table from mine.
╔════╦══════════════╦
║ id ║ group_concat ║
╠════╬══════════════╬
║ 1 ║ a,b,b ║
║ 2 ║ a ║
║ 3 ║ a,a ║
║ 4 ║ a,a,a ║
║ 5 ║ a,b,a ║
╚════╩══════════════╩
And the table below is what I want to achieve.
╔════╦══════════════╦
║ id ║ group_concat ║
╠════╬══════════════╬
║ 2 ║ a ║
║ 3 ║ a,a ║
║ 4 ║ a,a,a ║
╚════╩══════════════╩
And this is the query statement I am trying to use.
select id, group_concat(val)
from user
group by id
having group_concat(val) = 'a'
Thanks in advance
Try this:
select id, group_concat(val)
from user
group by id
having count(distinct val) = 1 and max(val) = 'a'
I am learning sql query.
Here is my table simplified
╔════╦══════════════╦══════════════╦════════╦
║ id ║ user_id ║ department_id║ salary ║
╠════╬══════════════╬══════════════╬════════╬
║ 1 ║ 1 ║ 3 ║ 100 ║
║ 2 ║ 2 ║ 3 ║ 50 ║
║ 3 ║ 1 ║ 3 ║ 30 ║
║ 4 ║ 2 ║ 3 ║ 20 ║
║ 5 ║ 2 ║ 3 ║ 20 ║
╚════╩══════════════╩══════════════╩════════╩
Here is what I want to have below
╦══════════════╦══════════════╦════════╦
║ user_id ║ department_id║ salary ║
╠══════════════╬══════════════╬════════╬
║ 1 ║ 3 ║ 130 ║
║ 2 ║ 3 ║ 90 ║
╚══════════════╩══════════════╩════════╩
I want to add user salary if they are in the same department.
I have nooo ideas how to start.
Does anyone have good feedback that I can start with?
Thank you in advance
Try this query:
Select user_id, department_id, sum(salary) from table
Group by user_id, department_id
In the select clause, you have the columns you wish to select and your group by clause contains the grouping.
I have one table of records like
╔═════╦══════╦══════════╦══════════╗
║ PID ║ NAME ║ DATE1 ║ DATE2 ║
╠═════╬══════╬══════════╬══════════╣
║ 123 ║ john ║ 20110401 ║ 19700101 ║
║ 123 ║ john ║ 20110401 ║ 19700101 ║
║ 123 ║ john ║ 20110401 ║ 19700101 ║
║ 323 ║ mike ║ 20110401 ║ 19900101 ║
║ 323 ║ mike ║ 20110401 ║ 19900101 ║
╚═════╩══════╩══════════╩══════════╝
query1 is count on pid
query2 is if all the four fields are same the record is to be taken as one record else individual records and records to be counted on pid order.
The above result is:
╔═════╦══════════════╦═════════════╗
║ PID ║ TOTALRECORDS ║ TOTALUNIQUE ║
╠═════╬══════════════╬═════════════╣
║ 123 ║ 3 ║ 1 ║
║ 323 ║ 2 ║ 1 ║
╚═════╩══════════════╩═════════════╝
I did select pid,count(pid),(select count(pid) group by pid,name,date1,date2) from <table> group by pid;
But I get an error... kindly correct my code.
SELECT pid,
COUNT(*) totalRecords,
COUNT(DISTINCT name, date1, date2) totalUnique
FROM TableName
GROUP BY pid
SQLFiddle Demo
SQLFiddle Demo (with not unique record)
Result
╔═════╦══════════════╦═════════════╗
║ PID ║ TOTALRECORDS ║ TOTALUNIQUE ║
╠═════╬══════════════╬═════════════╣
║ 123 ║ 3 ║ 1 ║
║ 323 ║ 2 ║ 1 ║
╚═════╩══════════════╩═════════════╝
I was sitting several hours for trying to solve on specific problem but I couldn't get it. Finally, I decided to post it here. Here are some records,
╔════════╦════════════╦═════════╦══════════════════╗
║ AUTOID ║ PERSONNAME ║ FLOWER ║ Other columns... ║
╠════════╬════════════╬═════════╬══════════════════╣
║ 1 ║ Alex ║ Rose ║ ║
║ 2 ║ Rene ║ Rose ║ ║
║ 3 ║ Rene ║ Jasmin ║ ║
║ 4 ║ Darwin ║ Rose ║ ║
║ 5 ║ Alex ║ Rose ║ ║
║ 6 ║ Darwin ║ Jasmin ║ ║
║ 7 ║ Alex ║ Rose ║ ║
║ 8 ║ Rene ║ Jasmin ║ ║
║ 9 ║ Hello ║ World ║ ║
║ 10 ║ Hello ║ World ║ ║
╚════════╩════════════╩═════════╩══════════════════╝
How can I get this result, the person who has only one type of flower on all his/her records.
╔════════════╦════════════╗
║ PERSONNAME ║ FLOWERNAME ║
╠════════════╬════════════╣
║ Alex ║ Rose ║
║ Hello ║ World ║
╚════════════╩════════════╝
the best one I tried was this query below, and still not working.
SELECT DISTINCT t1.PersonName, t1.Flower
FROM TableName t1
INNER JOIN
(
SELECT personname, COUNT(DISTINCT flower) totals
FROM tableName
GROUP BY personname, Flower
) t2 ON t1.personname = t2.personname and
t2.totals = 1
Any Idea?
you can use GROUP BY clause, HAVING clause and COUNT() on this problem, no need to join on a subquery
SELECT PersonName, MAX(Flower) flowerName
FROM TableName
GROUP BY PersonName
HAVING COUNT(DISTINCT Flower) = 1
SQLFiddle Demo
or, because there's always more than one way to skin a cat...
SELECT x.*
FROM tablename x
LEFT
JOIN tablename y
ON y.personname = x.personname
AND ((y.flower <> x.flower)
OR (y.flower = x.flower AND y.autoid < x.autoid))
WHERE y.autoid IS NULL;
Since my problem is somewhat particular, I haven't yet found and answer, after very long searches, so here it goes: I have two tables: In_Stock and Out_Stock. I use the following selects:
IN_Stock:
select
INs.CatID as CategoryID,
INs.SubCatID as SubcategoryID, Sum(INs.Quantity) as QuantityIN
from IN_Stock INs
group by INs.CatID, INs.SubCatID
╔════════════╦═══════════════╦════════════╗
║ CategoryID ║ SubcategoryID ║ QuantityIN ║
╠════════════╬═══════════════╬════════════╣
║ 2 ║ 9 ║ 0 ║
║ 1 ║ 16 ║ 8 ║
║ 1 ║ 27 ║ 5 ║
║ 1 ║ 30 ║ 160 ║
║ 1 ║ 31 ║ 6 ║
║ 1 ║ 39 ║ 35 ║
║ 1 ║ 40 ║ 7 ║
║ 2 ║ 44 ║ 13 ║
║ 2 ║ 54 ║ 6 ║
║ 2 ║ 70 ║ 5 ║
║ 3 ║ 87 ║ 3,5 ║
╚════════════╩═══════════════╩════════════╝
OUT_Stock:
select
OUTs.CatID as CategoryID,
OUTs.SubCatID as SubcategoryID,
Sum(OUTs.Quantity) as QuantityOUT
from OUT_Stock OUTs
group by OUTs.CatID, OUTs.SubCatID
╔════════════╦═══════════════╦═════════════╗
║ CategoryID ║ SubcategoryID ║ QuantityOUT ║
╠════════════╬═══════════════╬═════════════╣
║ 1 ║ 30 ║ 30 ║
║ 1 ║ 39 ║ 15 ║
╚════════════╩═══════════════╩═════════════╝
What I get is this table bellow (and it's obviously not right).
select
INs.CatID as CategoryID,
INs.SubCatID as SubcategoryID,
Sum(INs.Quantity) as QuantityIN,
SUM(OUTs.Quantity) as QuantityOUT,
SUM(INs.Quantity)- SUM(OUTs.Quantity) as RemainingQuantity
from IN_Stock INs
left join OUT_Stock OUTs on INs.CatID=OUTs.CatID and INs.SubCatid=OUTs.SubCatid
group by INs.catid, INs.subcatid
╔════════════╦═══════════════╦═════════════╦════════════╦═══════════════════╗
║ CategoryID ║ SubcategoryID ║ QuantityIN ║ QuantityOUT║ RemainingQuantity ║
╠════════════╬═══════════════╬═════════════╬════════════╬═══════════════════╣
║ 2 ║ 9 ║ 0 ║ ║ ║
║ 1 ║ 16 ║ 8 ║ ║ ║
║ 1 ║ 27 ║ 5 ║ ║ ║
║ 1 ║ 30 ║ 320 ║ 150 ║ 170 ║
║ 1 ║ 31 ║ 6 ║ ║ ║
║ 1 ║ 39 ║ 35 ║ 30 ║ 5 ║
║ 1 ║ 40 ║ 7 ║ ║ ║
║ 2 ║ 44 ║ 13 ║ ║ ║
║ 2 ║ 54 ║ 6 ║ ║ ║
║ 2 ║ 70 ║ 5 ║ ║ ║
║ 3 ║ 87 ║ 3,5 ║ ║ ║
╚════════════╩═══════════════╩═════════════╩════════════╩═══════════════════╝
What i want is to make a select in SQL that returns something like the table bellow... and I would like to know if and how can I see in RemaningStock collumn: 130 where SubcategoryID=30 and 20 where SubCategoryID=39.
╔════════════╦═══════════════╦════════════╦════════════╦═══════════════════╗
║ CategoryID ║ SubcategoryID ║ QuantityIN ║ QuantityIN ║ RemainingQuantity ║
╠════════════╬═══════════════╬════════════╬════════════╬═══════════════════╣
║ 2 ║ 9 ║ 0 ║ ║ ║
║ 1 ║ 16 ║ 8 ║ ║ ║
║ 1 ║ 27 ║ 5 ║ ║ ║
║ 1 ║ 30 ║ 160 ║ 30 ║ 130 ║
║ 1 ║ 31 ║ 6 ║ ║ ║
║ 1 ║ 39 ║ 35 ║ 15 ║ 20 ║
║ 1 ║ 40 ║ 7 ║ ║ ║
║ 2 ║ 44 ║ 13 ║ ║ ║
║ 2 ║ 54 ║ 6 ║ ║ ║
║ 2 ║ 70 ║ 5 ║ ║ ║
║ 3 ║ 87 ║ 3,5 ║ ║ ║
╚════════════╩═══════════════╩════════════╩════════════╩═══════════════════╝
Both tables have one or multiple records for a certain category or subcategory
Any help is much appreciated. Many thanks!
Either SQL or Access VBA code is good for me.
PS: As this is my first post please be "gentle".
The main problem is that your final query makes the joins on individual rows in the initial dataset and only then performs the aggregation, whereas you would want to perform the joins on the intermediate sums.
Assuming this test data, for example:
CREATE TABLE table_in (
id INTEGER,
value INTEGER
);
CREATE TABLE table_out (
id INTEGER,
value INTEGER
);
INSERT INTO table_in(id, value) VALUES
(1, 120),
(1, 10);
INSERT INTO table_out(id, value) VALUES
(1, 30);
The way you're writing the LEFT JOIN in your last query:
SELECT t1.value AS val1, t2.value AS val2
FROM table_in t1 LEFT JOIN table_out t2 ON t1.id=t2.id;
would produce these rows, before aggregation:
ID VAL1 VAL2
1 120 30
1 10 30
Here, the sum would then give this:
ID SUM(VAL1) SUM(VAL2)
1 130 60
This will happen any time there's more than one row for the conditions used for the join.
You need to perform the join after the aggregate operations, since you want to compare the sum of all inputs, to the sum of all outputs.
This can be done using subselect statements, or CTEs.
For example:
WITH sum_in AS (
SELECT id, SUM(value) AS all_in
FROM table_in
GROUP BY id
), sum_out AS (
SELECT id, SUM(value) AS all_out
FROM table_out
GROUP BY id
)
SELECT t1.id, all_in, all_out, all_in - all_out
FROM sum_in t1 LEFT JOIN sum_out t2 ON t1.id=t2.id
You can use subqueries to get what you want, for example:
SELECT *
FROM (SELECT INs.catid AS CategoryID,
INs.subcatid AS SubcategoryID,
SUM(INs.quantity) AS QuantityIN
FROM in_stock INs
GROUP BY INs.catid,
INs.subcatid) AS a
LEFT JOIN (SELECT OUTs.catid AS CategoryID,
OUTs.subcatid AS SubcategoryID,
SUM(OUTs.quantity) AS QuantityOUT
FROM out_stock OUTs
GROUP BY OUTs.catid,
OUTs.subcatid) AS b
ON ( a.subcategoryid = b.subcategoryid )
AND ( a.categoryid = b.categoryid );
From this, it is very easy indeed to edit and modify the query using the Query Design Window in MS Access
SELECT a.categoryid,
a.subcategoryid,
a.quantityin,
b.quantityout,
[quantityin] - [quantityout] AS RemainingQuantity
FROM (SELECT INs.catid AS CategoryID,
INs.subcatid AS SubcategoryID,
SUM(INs.quantity) AS QuantityIN
FROM in_stock INs
GROUP BY INs.catid,
INs.subcatid) AS a
LEFT JOIN (SELECT OUTs.catid AS CategoryID,
OUTs.subcatid AS SubcategoryID,
SUM(OUTs.quantity) AS QuantityOUT
FROM out_stock OUTs
GROUP BY OUTs.catid,
OUTs.subcatid) AS b
ON ( a.subcategoryid = b.subcategoryid )
AND ( a.categoryid = b.categoryid );