I have a table named All users which holds all users and the sum of their access to a system. And two more tables from which I made the one with all the users, which are basically scattered over the two tables.
The problem with my All users table is that it doesn't have the Name column that corresponds with the User.
tbl_sbc
Users Name
Foo John
Users JDF
Users Name
Bar Andrew
All Users
Users
Foo
Bar
and I want the query to crossmatch the users on the all users table and retrieve their name which can be in one of the two other tables.
I tried doing
SELECT [Users], [Name] ,Sum([Access]) AS Total
FROM (Select [Users], [Name] , [Access] from tbl_sbc
Union
Select [Users], [Name] , [Access] from [Users JDF])
GROUP BY [Users]
ORDER BY [users] DESC;
But I get Your query does not include the specified expression [Name] as part of an aggregate function
but without the [Name] it works fine
Take this
SELECT [Users], [Name] ,Sum([Access]) AS Total
FROM (Select [Users], [Name] , [Access] from tbl_sbc
Union
Select [Users], [Name] , [Access] from [Users JDF])
GROUP BY [Users]
ORDER BY [users] DESC;
AND Turn into this
SELECT [Users], [Name] ,Sum([Access]) AS Total
FROM (Select [Users], [Name] , [Access] from tbl_sbc
GROUP BY [Users], [Name]
Union
Select [Users], [Name] , [Access] from [Users JDF])
GROUP BY [Users]
ORDER BY [users] DESC;
Related
i have a following table users which has following definition
id integer
Name varchar
Email varchar
Location varchar
Phone varchar
Picture varchar
Status varchar
and i have another table userskills which has following definition
id integer
Userid integer
Skill integer
no i want to combine the two tables and select single row from first table while multiple rows from another table as user can have multiple skills but i'm getting multiple rows of user data but i want a single row?
can anybody tell me how to get user and its corresponding skills in an single row?
You can do it with a PIVOT.
You would select the skills from the skills table and then pivot them into a single row.
Then JOIN with the user.
Here is an example with a fixed number of skills.
WITH TEMP AS
(
SELECT 'John' AS U, 'Driver' AS S
UNION ALL
SELECT 'John' AS U, 'Plumber' AS S
UNION ALL
SELECT 'Jim' AS U, 'Driver' AS S
UNION ALL
SELECT 'Tom' AS U, 'Driver' AS S
)
SELECT * FROM
( SELECT *, U AS DRIVER FROM TEMP ) AS P
PIVOT
(
COUNT([U])
FOR S IN ([Driver],[Plumber])
) AS PIV
If you have a variable number of skills you are going to have to query that first, then transform that into a string and run EXEC on the string.
I have three mysql table named users,'clients' and 'products'. I want to count the 'id' field of every table in same query. Is it possible? please help me.
Thanks in advance.
Please this statement
SELECT u.total_users, c.total_clients, p.total_products (
SELECT COUNT(id) as total_users FROM users
) as u,
(
SELECT COUNT(id) as total_clients FROM clients
) as c,
(
SELECT COUNT(id) as total_products FROM products
) as p
I have a single table.
This table has 2 fields, product IDs and Store IDs. The same product ID can exist with many different Store IDs.
I need to find the products (if any) that are common across all the stores.
I'm having difficulty constructing the correct query, any advice?
You can check distinct store ids count with product id. If distinct Store ids count equal to total stores that will be the product ids you want.
SELECT productID, count(DISTINCT StoreID) as stroes FROM [Table name] GROUP BY productID
HAVING COUNT(DISTINCT StoreID) = (SELECT COUNT(DISTINCT StoreID) FROM [Table name] );
I'm sure you'll get many better answers, but it sounds like you are wanting the reverse of the distinct clause, not sure if this will work though:
SELECT NOT DISTINCT [Product_ID]
FROM TABLENAMEHERE
You could sue count(distinct productID)
select productID
from my_table
group by productID
having count(distinct productID) = (
select count(distinct store)
from my_table )
To describe my problem, heres a hypothetical scenario:
(
select user_id, stat
from stats1
where user_id in (select id from users where selected = 1)
)
union
(
select user_id, stat
from stats2
where user_id in (select id from users where selected = 1)
)
union
(
select user_id, stat
from stats3
where user_id in (select id from users where selected = 1)
)
# and so on ...
see how the query in 'where in' is used multiple times. I want to save the result of this query adn then use it. in all the other queries. Obviously this is easily possible if by running that query in PHP and then passing in the values, but is this possible purely in MySQL?
You could create a permanent view for your selected users
CREATE VIEW selectedUsers AS SELECT id FROM users WHERE selected = 1;
Then when you need it, your syntax will be easier and lighter
(
SELECT user_id, stat
FROM stats1
WHERE user_id IN (SELECT * FROM selectedUsers)
)
UNION
(
SELECT user_id, stat
from stats2
WHERE user_id IN (SELECT * FROM selectedUsers)
)
UNION
(
SELECT user_id, stat
FROM stats3
WHERE user_id IN (SELECT * FROM selectedUsers)
)
# and so on ...
If your data in the table users doesn't change during the request, the database engine should use the cache each time your view is needed
Here is the MySQL Views documentation
I have a select query with a subquery in it on table a:
select UserName, Name, anniversaryDate, RegionCode
from a where UserName in
(
select Username from
(
select Username, max(timeUsed)
from log_table where role='Cou'
group by Username order by max(timeUsed) desc limit 10
) a
)
which works fine, now I have another table called b,
on that table I only have 3 columns:
UserName, PName and feedback
which have each UserName multiple times and I want to join to my first query the count of each UserName I got from the first query.
for eaxmple:
SELECT COUNT(UserName) FROM b where UserName='Admin' group by UserName
How do I join the results from the second query to the first one?
Tables:
log_table :
id - Int, AI, PK
Username - varchar
Action - Int
timeUsed - Datetime
role - varchat
a:
UserName - varchar, PK
Name - varchar
anniversaryDate - Datetime
RegionCode - Int
b:
UserName - varchar
PName - varchar
feedback - varchar
Based on your table structure the query below might add the necessary column you are looking for:
select users.UserName, Name, anniversaryDate, RegionCode,
Coalesce(count(b.UserName),0) as cnt
from users
left outer join (select Username, max(timeUsed)
from log_table where role='Cou'
group by Username order by max(timeUsed) desc limit 10
) a using (UserName)
left outer join fedbacks b on (a.UserName = b.userName)
group by users.UserName;