Distinct Count Multiple Columns MS Access [closed] - ms-access

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am wanting to find a way to use the following table and write a distinct count query:
Unique Number | Name | Number | ID
1 | James | 1 | 456
2 | John | 2 | 456
2 | John | 3 | 456
2 | John | 4 | 456
2 | John | 5 | 457
1 | James | 3 | 457
1 | James | 4 | 456
3 | Ted | 3 | 457
3 | Ted | 4 | 456
And would like the following query result when the ID is 456
Unique | Name | Number Count
1 | James | 2
2 | John | 3
3 | Ted | 1

A simple aggregate query will get the count of Number by name and ID.
SELECT [Unique Number], [Name], ID, Count(*) AS CountNum FROM table GROUP BY [Unique Number], [Name], ID;'
Counting how many unique ID per name requires two queries. First have to do a query that returns DISTINCT IDs for each Name.
SELECT DISTINCT [Unique Number], [Name], ID FROM tablename;
Then use that query to calculate aggregate count.
SELECT [Unique Number], [Name], Count(ID) AS CountID FROM Query1 GROUP BY [Unique Number], [Name];
Can nest the 2 queries.
SELECT [Unique Number], [Name], Count(ID) AS CountID FROM (SELECT DISTINCT [Unique Number], [Name], ID FROM tablename) AS Query1 GROUP BY [Unique Number], [Name];

Related

Write a query to switch the ids for students who are next to each other? Here is the sample input [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
1) Write a query to switch the ids for students who are next to each other? Here is the sample input.
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
For the sample input, the output is:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
Note that if the total number of seats is odd, The id for the last one remains the same.
If your ID's are ordered this will help you,
SELECT a.id,b.Name
from
(SELECT [id],[Name]
FROM [Table]
where mod([id],2)=1) a
inner join
(SELECT [id],[Name]
FROM [Table]
where mod([id],2)=0) b
on a.id+1=b.id
UNION
select b.id,a.Name
from
(SELECT [id],[Name]
FROM [Table]
where mod([id],2)=1) a
inner join
(SELECT [id],[Name]
FROM [Table]
where mod([id],2)=0) b
on a.id+1=b.id
You can try below
DEMO
select id,coalesce(bname,aname) as Name from
(
select A.id,A.name as aname,B.name as bname
from
(select id,name
from cte1 where id%2=1
)A left join
(select id,name
from cte1 where id%2=0
)B on A.id+1=B.id
union all
select A.id,A.name,B.name
from
(select id,name
from cte1 where id%2=0
)A left join
(select id,name
from cte1 where id%2=1
)B on A.id=B.id+1
)X order by id
OUTPUT:
id Name
1 Doris
2 Abbot
3 Green
4 Emerson
5 Jeames

Sum of Counted records that calculated using "group by" with condition and "group by"

I'm sorry for fuzzy title of this question.
I have 2 Tables in my database and want to count records of first_table using "group by" on a foreign key id that exists in a column of second_table (which stores ids like array "1,2,3,4,5").
id | name | fk_id
1 | john | 1
2 | mike | 1
3 | jane | 2
4 | tailor | 1
5 | jane | 3
6 | tailor | 5
7 | jane | 4
8 | tailor | 5
9 | jane | 5
10 | tailor | 5
id | name | fk_ids | s_fk_id
1 | xxx | 1,5,6 | 1
2 | yyy | 2,3 | 1
3 | zzz | 9 | 1
4 | www | 7,8 | 1
Now i wrote the following query but it not working properly and displays wrong numbers.
I WANT TO:
1-Count records in first_table group by "fk_id"
2-Sum the counted records which exists in "fk_ids"
3-Display the sum result (sum of related counts) grouped by id.
symbol ' ' means ``.
select sum(if(FIND_IN_SET('fk_id', 'fk_ids')>0,'count',0) 'sum', 'count', 'from'.'fk_id', 'second_table'.* FROM 'second_table'
LEFT JOIN
(
SELECT 'fk_id', count(*) 'count'
FROM 'first_table'
group BY 'fk_id'
) AS 'from'
ON FIND_IN_SET('fk_id', 'fk_ids')>0
WHERE 'second_table'.'s_fk_id'=1
GROUP BY 'id'
ORDER by 'count' DESC
This table has many data and we have no plan to change the structure.
Edit:
Desired output:
id | name | sum
1 | xxx | 7 (3+4+0)
2 | yyy | 2 (1+1)
3 | zzz | 0 (0)
4 | www | 0 (0+0)
After two holidays i came back to work and found out that the "FIND_IN_SET" function is not working properly with space contained string.
And the problem is that i was ignored the spaces too, (same as this question)
Finnaly this query worked:
select sum(`count`) `sum`, `count`, `from`.`fk_id`, `second_table`.* FROM `second_table`
LEFT JOIN
(
SELECT `fk_id`, count(*) `count`
FROM `first_table`
group BY `fk_id`
) AS `from`
ON FIND_IN_SET(`fk_id`, replace(`fk_ids`,' ',''))>0
WHERE `second_table`.`s_fk_id`=1
GROUP BY `id`
ORDER by `count` DESC
And the magic is replace(fk_ids,' ','')

How to get result only for spesified ID's in MySQL [duplicate]

This question already has answers here:
How to return rows that have the same column values in MySql
(3 answers)
Closed 6 years ago.
i am working on college ERP software where i couldn't be able to find solution on following problem. here i am shortly explain the problem
Table Student.
+----+--------+--------+
| id | enroll | sub_id |
+----+--------+--------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 2 | 2 |
| 5 | 2 | 3 |
| 6 | 3 | 1 |
| 7 | 3 | 2 |
+----+--------+--------+
Requirement - In student table, fetch only those enroll no. which have sub_id 1 & 2 if any enroll no. have assign 1,2 & 3 then it should not be come into the result.
I try to us IN function but i won't give right results.
Please suggest me solution for this problem.
Thanx...
Using conditional aggregation:
SELECT t1.id, t1.enroll, t1.sub_id
FROM Student t1
INNER JOIN
(
SELECT enroll,
SUM(CASE WHEN sub_id IN (1, 2) THEN 1 ELSE 0 END) AS subCount
FROM Student
GROUP BY enroll
HAVING subCount = 2 AND COUNT(*) = 2
) t2
ON t1.enroll = t2.enroll
The trick in this query is the HAVING clause, which restricts to enrollments which have (1, 2) sub enrolled, and only (1, 2) sub enrolled.
Follow the link below for a running demo:
SQLFiddle
I have a hard time figuring out exactly what you want from your description but this returns 1, 3 for your example
SELECT DISTINCT enroll FROM Student
WHERE enroll NOT IN
(SELECT enroll FROM Student WHERE sub_id NOT IN (1,2))

get sum for multiple blocks

I have a table like this:
name | day | score
------------------
John | 1 | 4
John | 2 | 5
John | 3 | 6
Marc | 1 | 7
Marc | 2 | 4
Marc | 3 | 5
Paul | 1 | 8
Paul | 2 | 2
Paul | 3 | 3
I want to get the sum of the score for each person, but only for certain days, sorted by this sum. let's say I want to get the score-sum of the 1. and 2. day, this is what I expect:
name | sum(score)
-----------------
Marc | 11
Paul | 10
John | 9
this is what failed:
SELECT name, sum(score) FROM mytable WHERE day<=2
I think I have to surround the sum(score)-part with some IF-statement, but I have no idea how.
Just add group by
SELECT name, sum(score) FROM mytable WHERE day<=2 group by name
Use sum function and group by clause for grouping the result.
query
select name,sum(score) as score
from myTable
where day in (1,2)
group by name
order by sum(score) desc;
fiddle demo

MYSQL Ignore Repeat Values and Get Unique Count

Basically, as the question states, I am looking for the easiest and most straight forward way of getting counts based on unique values.
Here is my data set:
id | item_id | user_id
1 | 10 | 123
2 | 10 | 123
3 | 10 | 123
4 | 11 | 123
5 | 12 | 123
6 | 10 | 456
7 | 10 | 789
8 | 12 | 456
Ideally, when I run the query I should get the following:
count | user_id
3 | 123
2 | 456
1 | 789
Where even though user 123 has 5 items to their name, the really only purchased 3 unique items. Is this really straight forward and I'm just missing it completely? Here is what I have currently:
SELECT count(user_id) AS count, item_id, user_id
FROM table
GROUP BY item_id, user_id
HAVING count > 1
ORDER BY count DESC
This is producing the opposite of what I want:
count | user_id
5 | 123
2 | 456
1 | 789
Thanks in advance! And if this has been answered already, please point me in that direction.
You can count distinct item ids and then group by the user id:
SELECT
COUNT(DISTINCT item_id) AS count,
user_id
FROM
event_assigned
GROUP BY
user_id
ORDER BY
count DESC