how to write the sql - mysql

i have a table like
ID USER_ID type
1 1 2
2 1 1
3 3 3
4 3 1
5 6 2
6 6 3
and i want to get the sum of all user_id = 1 and user_id =3
and every type sum in user_id = 1 and user_id =3 ,it can be two sql
the result is
sum
4
type sum
1 2
2 1
3 1

The first function you needed to achieve the result is Count() not SUM() function
For first result
select count(*) as sum from user1 where user_id in(1,3)
For second result you need to select type also and it should be grouped
select type, count(*) as sum from user1 where user_id in(1,3)
group by type
Fiddle for second you can check the first one also there by taking the query
Next time post your effort. Guess you are naive to sql.

Related

Query to get distinct count of column with MYSQL

I have a table name POEMS with columns poemID and userID. I want to make the poemID distinct and show only the count in the userID column. There are 3 poemID's with 1. I want to make it show poemID 1 and userID would be 2. poemID 2 and userID 3, for the 3 rows and poemID 3 with userID 1 for the 1 row.
|poemID | userID|
1 1
1 5
2 2
2 5
2 4
3 2
I want the above table to look like the table below.
|poemID | userID |
1 2
2 3
3 1
My SQL query im trying is below but its not working. Please help.
SELECT DISTINCT(poemID), COUNT(userID) FROM POEMS GROUP BY poemID;
This looks like a straight aggregation query:
SELECT poemID, COUNT(*) no_users
FROM POEMS
GROUP BY poemID;
Or, if the same user may appear multiple times for a given poem and you want to count it only once:
SELECT poemID, COUNT(DISTINCT userID) no_distinct_users
FROM POEMS
GROUP BY poemID;

SQL: How do I add specific rows together?

How I would add 2 specific rows in an SQL table together?
For example:
ID Quantity
1 1
1 3
1 2
2 3
2 4
I want to be able to add together ID 1 and 2 so the table becomes
ID Quantity
1 6
2 7
You need to use the SUM aggregate function and GROUP BY the ID.
SELECT Id, SUM(Quantity)
FROM yourtable
GROUP BY ID
Output:
ID Quantity
1 6
2 7
SQL Fiddle:
Further Reading.

MySQL Count and Group By two distinct columns

I have a table recommendation with fields recommender, attractionid
I want to count the how many attractionid existed in the table group by the attractionid but if there are same pairs of recommender and attractionid they are counted as 1.
For example,
attractionid recommender
1 1
1 2
1 1
2 3
2 1
2 2
2 2
2 2
expected result :
attractionid count
1 2
2 3
the rows below should be counted as 1
attractionid recommender
1 1
1 1
2 2
2 2
2 2
Use distinct attractionid,recommender inside count function.
Query
select attractionid,
count(distinct attractionid,recommender) as `count`
from recommendation
group by attractionid;
Try:
select attractionid, count(recommender) cnt
from (
select distinct attractionid, recommender
from recommendation
) x
group by attractionid

Access Totals Query Not Necessarily Returning First Record

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.

Update row according to result number in result set

I am looking to do the following:
tblOne:
-page_id
-split
tblMany
-view_id
-page_id
I want to order tblOne by the number of related page_id in tblMany.
Then divide the number of rows in tblOne by 5 and update tblOne.split to a number between 1 - 5 into which split it falls... e.g if there are 50 rows... row 0 - 9 are split 1, 10 - 19 split 2...etc
I am sure I can do the 'count' part... but havn't a clue how I would update the 'split' row
This query will give you the distinct page id and their count ordered by page_id count in descendant order (so max count first) :
SELECT page_id, count(1)
FROM tblMany
GROUP BY 1
ORDER BY 2 DESC
So for a tblMany like this:
view_id | page_id
--------+---------
1 1
2 1
3 2
4 2
5 2
6 3
You will get
page_id | count(1)
--------+---------
2 3
1 2
3 1