Let's say I've a mysql table properties with these records:
---------------------------------
id value
---------------------------------
1 1
1 1
2 1
2 1
1 1
---------------------------------
Now, I want to query to output:
----------------------------
id value
----------------------------
1 3
2 2
----------------------------
Try this
select id,sum(value) as value FROM table group by id
And this is what you want:
select id, sum(value) as value from properties group by id
You need to use the GROUP BY parameter.
SELECT id, SUM(value) as value FROM tablename GROUP BY id
Use This Code
SELECT id, sum(value) AS value FROM `tablename ` Group by id;
Related
Let's say I have this table:
name| value
----|------
A | 0
B | 0
A | 1
C | 1
A | 1
The select I want is to give the result like this:
A | 2
B | 0
C | 1
In first phase I tried with:
SELECT name, count(0)
FROM table
WHERE value > 0
GROUP BY name;
Which result
A | 2
C | 1
I also want to include B with count(0) = 0. How I can do this?
You want to aggregate your rows and get one result row per name. This translates to GROUP BY name in SQL. For counting use COUNT and inside use CASE WHEN to decide what to count.
select name, count(case when value > 0 then 1 end)
from mytable
group by name
order by name;
This works because COUNT only counts non-null occurences. We could just as well use SUM for counting: sum(case when value > 0 then 1 else 0 end).
In your example there is only 0 and 1. If these are the only possible values, you can just add them up:
select name, sum(value)
from mytable
group by name
order by name;
Do you just want aggregation? The following query would actually produce the correct results for your sample data:
select name, sum(value) sum_value
from mytable
group by name
Try this:
SELECT a.name, count(*)*(case when sum(a.value) >= 1 then 1 else 0 end)
FROM table a
GROUP BY name;
For a better solution, give more details about the nature of the value column
I really don't know how to title this problem properly.
Heres the table structure:
ID | CLIENT_ID | …
ID is primary and auto increment. CLIENT_ID on the other hand can occur multiple times.
What i want is to fetch the rows by CLIENT_ID with highest ID ... Heres a example
ID | CLIENT_ID
1 | 1
2 | 1
3 | 2
4 | 3
5 | 2
So here CLIENT_ID 1 and 2 occurs multiple times (because there is a newer version).
After the query i want the following IDs in the results: 2,4,5 (Because the highest ID in rows with CLIENT_ID 1 is the row with ID 2 and so on)
If you need all the columns you can use a select in
select * from my_table
where (id, client_id) in ( select max(id), client_id
from my_table
group by client_id);
but if you need only the id
select id from my_table
where (id, client_id) in ( select max(id), client_id
from my_table
group by client_id);
or more simple
select max(id)
from my_table
group by client_id;
SELECT * FROM table GROUP BY client_id HAVING max(id)
this should be more efficient than a sub select
I need to list all the duplicate IDs with the number of occurrence of each ID in a single MYSQL query.
ID
____
1
1
2
3
4
4
4
5
5
6
7
Output must be:
ID | Occurrence
_______________
1 | 2
4 | 3
5 | 2
Just use a simple GROUP BY query:
SELECT ID, COUNT(*) AS Occcurrence
FROM yourTable
GROUP BY ID
HAVING COUNT(*) > 1
This can be done simply by using Group By clause and Count() function
select
Id, count(id) as Occurance
from
tableName
group by id
having Occurance > 1;
use mysql GROUP BY
select ID,count(*) from table_name group by ID having count(*) > 1
Sample data:
ProductID PackingID
------- ---------
1 2
1 2
3 2
3 2
1 1
2 1
3 2
I have the above sample data. What i want is to select the unique (not distinct) rows of the combination productID and packingID. In the above example the only matching results are
ProductID PackingID
------- ---------
1 1
2 1
These rows are the only unique combinations of ProductID and PackingID together. I do not want Distinct results because it will give me one of all the other combinations.
SELECT PRODUCTID,PACKINGID FROM DTEMP
GROUP BY PRODUCTID,PACKINGID
HAVING COUNT(PRODUCTID)=1
ORDER BY 1;
You can try this one this is how i do in oracle ... to get the unique rows without using distinct.
SELECT ProductID, PackingID
FROM yourtable
GROUP BY ProductID, PackingID
HAVING COUNT(*) = 1
your table should be like:
uniqueID ProductID PackingID
1 x y
2 x y
3 z x
Query:
SELECT uniqueID,ProductID,PackingID
FROM yourtable
WHERE uniqueID IN
(
SELECT MIN(uniqueID)
FROM yourtable
GROUP BY ProductID,PackingID
)
OK, Here is what my table looks like
------------------------------------------------
Textid type
-----------------------------------------------
1 a
2 b
1 a
1 c
2 c
1 a
3 a
------------------------------------------------
Now, I need a query that can give me this output...
-------------------------------------
Distinct(textid) | rand(type) |
--------------------------------------
1 a
2 c
3 a
--------------------------------------
rand(type) gives me number.... Do I need to pass a different records inside rand() like random(SELECT type FROM mytable)
UPDATE
I am trying to get a distinct id from the table and random field(type) associated with that distinct id
SELECT textid,
(
SELECT type
FROM mytable mi
WHERE mi.textid = md.textid
ORDER BY
RAND()
LIMIT 1
)
FROM (
SELECT DISTINCT textid
FROM mytable
) md
Create a composite index on mytable (textid, type) for this to work fast:
CREATE INDEX ix_mytable_textid_type ON mytable (textid, type)