Summarize the DeviceCode based on users in SQL Query - mysql

I have following data
ID User1 User2 DeviceCode
1 John D1
2 John D2
3 Brian D3
4 Brian D4
5 Davis D5
6 Davis D5
For the above kind of table Layout in MySQL I am trying to figure out count of how many different DeviceCode each user is mapped to
Looking forward to the guidance.
Thanks

Use coalesce():
select coalesce(user1, user2) as user, count(distinct devicecode)
from t
group by user;
It seems odd to me that only one user column is populated per row. Why bother having two columns in that case?

Related

Select and compare two columns from different tables to find matched records

I'm just learning PHP and MySQL and I have two tables in the same database : FirstYear , SecondYear that have a structure like this :
StudentId |Math | Physics StudentId1 | Math1 | physics1
Joe 10 14 Alan 12 17
Alan 13 17 Smith 11 13
Smith 9 9 Joe 10 15
Is it possible to write a query that select and compare the two columns StudentId , StudentId1 to find matched records and if for example Joe=Joe after that compare records of math with math1 and physics with physics1 that are in the same row as matched records of StudentId with StudentId1 ;the idea of this query is to study the improvement of same student from first year to the second one ,Thanks .
Yes, it is possible but you have to complete SQL fundamental course.
In this situation you have to know about JOIN. Such as, Inner Join, Left Join, Right Join, Full Join etc. Also, compare with unique id, not name. Because, name always duplicate. It is not good practice. So, Know about primary key and foreign key.
However,
Query-
SELECT * FROM FirstYear INNER JOIN SecondYear ON FirstYear.StudentId = SecondYear.StudentId1 WHERE FirstYear.id = 1
Something like that, alternatively, you can try to another logic.

Combine columns and get count SQL Server 2008

Forgive me, I'm still learning but am in need of some assistance. Some of what I’ve done is an amalgam of previous questions but I can’t find quite what I’m looking for.
I have a table with 30 columns of data, let’s call it table1. Every two columns are actually a set of the same type of data that have meaning together and singly. For example col1 with col2, is say a set of names.
Like this:
1 Jim Jeff
2 Mike Ben
3 Mike Mike
4 Peter Jeff
5 Jeff Jim
6 etc etc
The remaining 28 columns aren't important at this point. I want to return a single list of the unique names in col1 AND col2 along with their counts in total from both columns. Here’s what I have and it seems to work to a point but there is a problem with the return.
SELECT col1, COUNT(*)
FROM table1
GROUP BY col1
UNION
SELECT col2, COUNT(*)
FROM table2
GROUP BY col2
The problem is, when col1 has a name in it that is also in col2 it will return two counts. For example, if I had 6 different names, a total of 100 times, 50 in each column I might see something like this returned with the above query.
Jim 4
Jim 13
Jeff 8
Jeff 19
Mike 11
Mike 34
Ben 4
Brian 2
Peter 5
Obviously, Jim, Jeff and Mike appear in both columns and Ben, Brian and Peter appear in only one (It seems to me that it doesn’t matter which one).
What I need returned is:
Jim 17
Jeff 27
Mike 45
Ben 4
Brian 2
Peter 5
I tried putting a subquery in GROUP BY to force what is returned by a union without the count (forgive me, I don’t know much SQL, I'm just making assumptions by what little I understand of the language), meaning:
GROUP BY (SELECT col1 FROM table1 UNION SELECT col2 FROM table2)
but I guess I’m making silly assumptions. Any suggestions?
You can use a CTE to get the list of all names, then do a count based on that.
;WITH Names AS
(
SELECT col1 AS [Name]
FROM table1
UNION ALL
SELECT col2 AS [Name]
FROM table2
)
SELECT [Name], COUNT(*)
FROM Names
GROUP BY [Name]

Distinct is not working in crystal reports and in mysql

id_no doc_id item_no product customer
123 2 1 A Daisy
123 2 9 A Ben
123 4 3 A Daisy
123 4 4 A Ben
123 6 11 B Daisy
123 6 13 B Ben
when I put it in my report it results to
Daisy Daisy
Ben
And it is also the result in mysql
select distinct customer from receipt where id_no like '123'
result:
Daisy
Daisy
Ben
Another query that I tried:
select distinct id_no, customer, product from receipt where id_no like '123'
result:
123 Daisy A
123 Daisy B
123 Daisy A
123 Ben A
123 Ben B
desired result:
Daisy
Ben
Please help me please.
Thank you guys for the help I found out why the other one keeps on showing. It is because the other Daisy is spelled as Daissy that's why.
Most likely your Customer name contains additional characters between the two records. Depending on how the datatype is implemented, spaces could matter and have contributed to the difference.
Try concatenating a character before and after customer.
I am unfamiliar with the concepts in Crystal Reports, but from what I understand, you would have to create a formula like so:
"XXX" & {Receipt.Customer} & "XXX"
If you run it again, you might recognize there is additional space like so:
XXXDaisyXXX
XXXDaisy XXX
^____ Additional Space
There is no chance of error while you using distinct ..it should return distinct value ...any way you can try another way
SELECT customer FROM receipt WHERE id_no like '123' GROUP BY customer
I don't see why you are fetching three records. I tried implementing your database and ran your query. It returned the result as expected.
See the above pic. There may be some issue with the data type you used. You may try grouping via customer, but I don't think it should affect your result anyway.
Also Check if the data types match.
The selection you made from customer id and id_no is unique and with distinct it should return only two rows
plase try this code
i get solution
select distinct `customer` from receipt where `id_no`='123'
this is right
i tryied this is my past project
best of luck

MySQL table comparison between two tables

I have two tables, follow and followed. I want to get all the rows in the follow table such that follow.screen != followed.following_screen_name.
follow table
ID screen_name
-----------------
1 eddie
2 jason
3 omar
4 jonathan
5 jack
followed table
ID my_screen_name following_screen_name
-------------------------------------------
1 john eddie
2 kenny eddie
3 kenny omar
4 john jason
5 john omar
Query I tried which didn't work
SELECT follow.screen_name from follow, followed where followed.my_screen_name='john'
AND follow.screen_name != followed.following_screen_name
Expected results
ID screen_name
-----------------
1 jonathan
2 jack
you can get this by doing a LEFT JOIN
SELECT F.screen_name FROM follow F
LEFT JOIN followed FD
on F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
WHERE FD.my_screen_name IS NULL
and FD.following_screen_name IS NULL
Another way is to use NOT EXISTS, get all rows that exists in followed and do NOT EXISTS clause to get desired result.
SELECT F.screen_name FROM follow F
WHERE NOT EXISTS
(
SELECT 1 FROM followed FD
WHERE F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
)
There are plenty of ways to solve this, but common to all is that you need to compare the follow.screen_name to both followed.my_screen_name and followed.following_screen__name.
One way is to use NOT IN with a UNION:
select screen_name
from follow
where screen_name not in (
select following_screen_name
from followed
where following_screen_name is not null
union all
select my_screen_name
from followed
where my_screen_name is not null
)
While this approach is nice for clarity, it may not be as good for performance as using a left join or not exists.
A nice place to pick up mysql syntax and logic is here.
But try this code, it selects every row where the screen_name is not identical to anything produced in the next two queries:
SELECT * from follow WHERE screen_name
not in (select screen_name from followed)
AND not in (select followed_screen_name from followed);
The last two queries would look this and the WHERE filters all of the rows out with screen names identical to the fields below.
my_screen_name following_screen_name
-------------------------------------
john eddie
kenny eddie
kenny omar
john jason
john omar

Subquery in Access

I have 2 tables in Access with these fields
Student:
ID(PK) Name Family Tel
Lesson:
ID StudentRef(FK(Student)) Name Score
Imagine we have these records
Student :
1 Tom Allen 09370045230
2 Jim leman 09378031380
Lesson:
1 1 Math 18
2 1 Geography 20
3 2 Economic 15
4 2 Math 12
How can I write a query that result will be this (2 fields)?
Tom Math : 18 , Geography 20
Jim Economic :15 , Math :12
SELECT s.Name, l.Name, l.Score
INNER JOIN tbl_lessons as l ON s.student_id = l.student_id
FROM tbl_students as s
That won't give you your formatting, but it'll get you the data.
The most tricky part of your problem is how to aggregate strings in your sub-query. MS Access does not have any aggregation function that is applicable to strings (except for Count()) and there is no way to define your own function. This means you can't just get the desired "subject:score , subject:score" concanetation. As long as you can go without you can easily take the solution provided in the answer by Corith Malin.