Cross tabulation. Count one column against another - mysql

I have a table with nine columns, I would like to do a order on one column, counting the frequency against another and listing the users that have performed that action and how many times.
The table is called table 1
So I would like to have in the end
tcodetcountuser
abc 9 user1
abc 4 user2
def 3 user1
So user one performed abc 9 times, user 2 4 times, user one also performed def 3 times

You didn't provide the schema of the table containing the data, but your query has to look something like this :
select tcode, count(*) as tcount, user
from table1
group by tcode, user

Related

Count Unique Values Throughout One Day For Nonconsecutive Student ID's

I am trying to figure out how to count all instances where a student is online without counting duplicate instances.
For example, in the screenshot below, I want to see a column counting only instances where a student is logged in. So, if Student A is logged in at 5 AM, count = 1. Student B logged in at 7, Count = 2. At some point student A logged off and logged back on at 8 am, the count should be 2, not 3.
Thank you!
Student
Time.
Desired Column (Count)
A
5 AM
1
B
7 AM
2
A
8 AM
2
C
9 AM
3
D
10 AM
4
E
11 AM
5
D
12 PM
5
I am mainly trying to track the activity and only count when someone is logged in. If those students appear multiple times, we can assume they logged off at some point and logged back in. It's basically a unique running count. Not sure how to write this in SQL. I hope this makes sense.
One option, use the exists operator with a correlated subquery to check if the student has logged in before:
SELECT Student, Time_,
SUM(flag) OVER (ORDER BY Time_) AS expected_count
FROM
(
SELECT *,
CASE
WHEN EXISTS(SELECT 1 FROM table_name D WHERE D.Student = T.Student AND D.Time_<T.Time_)
THEN 0 ELSE 1
END AS flag
FROM table_name T
) D
ORDER BY Time_
See demo.

How do I change the value of one field in SQL record to a value from another table?

First of all, I'm sorry if this is a bit of a dumb question. I checked what's written in similarly phrased questions like "How do I update fields from one table with values from another table" but the content doesn't seem to match what I'm trying to do.
Let's say I have a table called site_users:
user_id
login
password
user_id2
2
user
password
1
7
access
xyz
2
11
otherlogin
abc
3
15
somebody
defg
4
22
user
qwert
5
Then I have a lot of other tables in the same database, that have some columns of various names that are actually corespondent to the "user_id" of the "site_users" table. There are no relations set or anything like that. I want to change the values in the fields of those other tables to user_id2. So let's say I have a table: user_options:
admin_id
perms1
perms2
2
1
12139389
7
1
13232111
I want to change it to:
admin_id
perms1
perms2
1
1
12139389
2
1
13232111
How can I do that? This is the first time I'm doing anything other than just simple mass changes of text with some regex :/
If i am understanding your question correctly you should be able to do following where table1 is top table and table2 is table you are trying to update:
update table2 t set admin_id = (select user_id2 from table1 where user_id = t.admin_id)

Merge two colums result as single colum data using Mysql query

I want to get all record data as single column using SQL query.User Table
user_id username parent_id
10 user1 5
12 user2 3
14 user3 2
.. .. ..
get all users with parent id as single column
Need results as below (get data from user_id & parent_id)
users OR users
10 10
12 5
14 12
5 3
3 14
2 2
.. ..
Here just need list of user sequence is not important.Is this possible in sql query? is there any SQL function for that?
This will do the trick.
SELECT user_id AS users FROM Users
UNION
SELECT parent_id AS users FROM Users
Use UNION ALL if you need duplicate values (such as in the parent_id column) to return as individual rows. Make sure the alias is the same for both halves to get it to return in a single column. This will return unordered, so rerunning the query might not always give the same result (meaning same data in same order), but it will always give the same data.

how to select specific rows in some condition

I tried to write a query that selects rows with steps that both user 1 and user 2 did, with combined number of times they did the step (i.e., if user 1 did step 1 3 times and user 2 did 1 time then the count should show 4 times.)
when I put condition as user_id=1, user_id=2 there is no error but it return nothing, when it should return some rows with values.
there is table step, and step taken
and table step has column id, title
table step_taken has column id, user_id(who performs steps), step_id
i want to find step that both of two user whose id 1,2 did
and also want to have the value as count added up how many times they performed that step.
for example if user id 1 did step named meditation 2 times,
and user id 2 did step named meditation 3 times,
the result i want to find should be like below ;
------------------------------
title | number_of_times
------------------------------
meditation| 5
------------------------------
here is my sql query
select title, count(step_taken.step_id)as number_of_times
from step join step_taken
on step.id = step_taken.step_id
where user_id = 1 and user_id=2
group by title;
it returns nothing, but it should return some rows of step both user1 and user 2 did.
when i wrote same thing only with user_id=1 or user_id=2, it shows selected information
how can I fix my code so it can show the information I want to get?
thanks in advance :)
user_id cannot be 1 and 2 at the same time. You need a second user table. Then join those on your criteria and count:
select title, count(u1.id) + count(u2.id) as number_of_times
from step u1 join step u2
on u1.id = u2.id
where u1.user_id = 1 and u2.user_id=2
group by title;
note: cannot tell what table title is in, or the purpose of step_taken was as step.id is identical.

Join from two tables mysql

I have a doubt where trying to join two tables by a previous search. I've looked several solutions and read some chapters in a mysql book but I think I'm pretty close to the right answer but still not get it
I have this table "userprocess":
idProcess username state
------------------------------------------
1 blisssing 3
2 enriquecalvera 1
2 africabaluja 2
1 enriquecalvera 3
2 blisssing 1
The primery key for this table is the union of idProceso+username.
I have this other table "user":
index username pass active tipeUser .... so on
----------------------------------------------------------------- ----
1 blisssing 6OiZVVUPi3LDE 1 user
2 carmen 6OOtfrXB2Nu5. 1 user
3 consuelo 6OgdhVSkr1VDs 1 user
4 africabaluja 6OoPtGjWMQARE 1 user
5 enriquecalvera 6O6tvHg.122uQ 1 user
The thing is I want to show the join of the two tables but with a search within the first table. If I run this query
SELECT username FROM userprocess where idProcess='1' ORDER BY state
I get this:
username
---------
blisssing
enriquecalvera
which is what I am looking for, but I want to show all the fields in the "user" table for those usernames ordered by idProceso. So I run this other query:
SELECT *
FROM
user u,
userprocess p
WHERE
u.username=p.username
AND u.username IN (
SELECT username
FROM userprocess
where idProcess='1'
ORDER BY username
) ORDER BY p.state
I got this:
username pass active tipeUser idProcess state
----------------------------------------------------------------------
blisssing 6Od3nSkfOiwlg 1 user 2 1
enriquecalvera 6Oc9usiDEk51U 1 user 2 1
enriquecalvera 6Oc9usiDEk51U 1 user 1 3
blisssing 6Od3nSkfOiwlg 1 user 2 3
But this is not what I want I just want the same two results as in the previous query but with all the columns of the result of joining the two tables..
I know there is a lot of questions like this, but I have tried a lot of things and still not having the desire result..
What am I missing?
thank you, if you have any qestion or doubt just ask :)
The reason you're seeing multiple results is because you're joining on just the username, but of course the userprocess table has 2 rows where username = enriquecalvera. Your subquery is correctly only returning the 1 row you're interested in (where idprocess = 1) but as your join is seperate to this, and therefore doesn't include the idprocess = 1 condition, you're getting both rows back.
You should just do this in one step with a join like this:
SELECT *
FROM
user u
INNER JOIN userprocess p on u.username=p.username and p.idProcess='1'
ORDER BY p.state