How do I combine these two queries? - sql

I have 2 queries from MS Access:
Query 1:
Select Date, Username
From Table1
where Date>='9/1/2013'
group by Date, Username
After run the Query 1, I got the unique users list and run Query 2 to get the total counts:
Select Query1.Date, Count(Query1.Date)
from Query1
Where Date>='9/1/2013'
How do I translate this into SQL studio script to get unique users then count the total numbers from Date? Please advise, thank you!

Try it like this:
Select q.Date, Count(q.Date)
FROM (
Select Date, Username
From Table1
where Date>='9/1/2013'
group by Date, Username
) q
GROUP BY q.Date

There's no need for a sub query, try this:
SELECT Date, Count (date)
FROM Database.User.Table1
Where Date > '9/1/2013'
Group By date, username
Per the comments:
SELECT Date, username, Count (date)
FROM Database.User.Table1
Where Date >= '9/1/2013'
Group By date, username

Related

SQL - Select Query to get group by records whose sum(data) > 24

I need to select a UserID from the table whose sum of Data greater than 24.
I can able to select group and sum the records using
SELECT SUM(DATA),UserID FROM TableName GROUP BY UserID
But how can I select only the records for which SUM(DATA)>24
I have tried
SELECT SUM(DATA),UserID FROM #tempTimesheetValue where SUM(DATA)>24 GROUP BY UserID
But its not working.
Thanks in advance for suggestion..,
you can do this by below query:
select UserID, DATA from (
SELECT SUM(DATA) as DATA, UserID FROM #tempTimesheetValue GROUP BY UserID
) A where DATA > 24
The question might as well have the correct answer, which is;
SELECT SUM(DATA), UserID
FROM #tempTimesheetValue
GROUP BY UserID
HAVING SUM(DATA) > 24;
A subquery could be used, but it is unnecessary complication.

How to check if a person has duplicate date records?

I am looking to query my Access database from Excel (DAO) to determine if any name in the table has more than one record per date. E.g. If Bob has two records on 05/05/17 then I want to return both records as part of a recordset.
Seems like you are looking for something like:
SELECT *
FROM yourtable
INNER JOIN
(
SELECT count(*), name, date
FROM yourtable
GROUP BY name, date
HAVING COUNT(*) > 1
) multi
ON multi.name = yourtable.name
AND multi.date = yourtable.date
The inner select returns rows with more than 1 entry for the same name and date.
In Access you can do
select name, date
from your_table
group by name, date
having count(*) > 1

Select statement with count

I want to count the number of ABC group using id.
SELECT group, count(id) as total FROM `user` WHERE group=`ABC`;
What's wrong?
Many thanks.
Include the columns in the select list in group by clause when using aggregate functions.
SELECT group, count(id) as total FROM user
WHERE group=`ABC`
GROUP BY group
Else simply get the count with out using other columns in the select statement.
SELECT count(id) as total FROM user
WHERE group=`ABC`
Try this:
SELECT group, count(id) as total FROM `user`
group by group having group like 'ABC';
If you want to get COUNT of users, who has the "group" field = "ABC"
SELECT count(id) as total FROM user WHERE group='ABC';
Also, it's better to avoid using SQL keywords in column names (GROUP is an SQL keyword)

Specific sql query with count of records

I have simple table.
I need to build a SQL query and in result get the count of record where user re-played same game. So in this case we will have 3 in result..
You want a count group by
select user_id, count(*)
from your_table
group by user_id ;
You'll want to group by user_id and ensure the game count is greater than 1.
Assuming you're using SQL Server, please try something like:
SELECT user_id, count(*) as GamesPlayed
FROM table_name
GROUP BY user_id
HAVING count(*) > 1;

SQL sum output based on multiple values

How can I get an output that shows the sum(hours) for multiple names in this table with one sql command?
Ideal output would for example (20, 70) based on the below table which would be the sum hours for each person on that date.
Current SQL.
SELECT sum(hours) from project_time where date = '04/07/2013' AND name = 'Rhys Parker'
The above sql give the result I want but its only for one users, I would like to get the output for all users on that date.
DB table:
DB Structure: SQLITE
CREATE TABLE PROJECT_TIME ( name VARCHAR(16), date DATE, project VARCHAR(16), hours VARCHAR(16), PRIMARY KEY (name, project, date))
You will add a GROUP BY. Using the aggregate function along with the GROUP BY will give you a result for each DISTINCT name you have the in the table:
SELECT name, sum(hours) TotalHours
from project_time
where date = '04/07/2013'
GROUP BY name
See SQL Fiddle with Demo
Sounds like you should be using GROUP BY:
SELECT name, sum(hours)
FROM project_time
WHERE date = '04/07/2013'
GROUP BY name
This will get you the sum of hours for each name:
Rhys Parker 70
Bob Smith 20
SQL Fiddle Demo
You want a group by:
SELECT name, sum(hours)
from project_time
where date = '04/07/2013'
group by name
Just to give an alternative, if you want it for all the dates, do
SELECT name,date, sum(hours) as TotalHours
from project_time
GROUP BY name,date;