Im having trouble coming up with a way of counting distinct record for a particular table and needing a little help with this. For example say have i have the following
FIRSTNAME LASTNAME STATE
WILL SMITH PA
JOHN DOE PA
BOB THOMAS OH
TOM JONES OH
MARK TIMMS CA
What I am looking for is a count of the distinct states in this table. Im looking for a count of 3.
Here what I have so far, but getting syntax error
SELECT COUNT(DISTINCT CONFIG) AS total
FROM TABLE_NAME
This should give you the correct count of 3:
SELECT Count(*) as Total
FROM
(
SELECT DISTINCT State
FROM yourTable
)
In MS Access you typically have to get the Distinct records first, then then the count of those records. See the below article for some tips on Distinct Counts.
Microsoft Access Tips & Tricks: Distinct Counts
Related
To be blunt I don't know SQL however I don't want the answer, I want to work it out myself.
Here's the question:
Write a SQL query to calculate the number of goals for each team.
players
id name team_id goals
1 Joel 1 3
2 Ed 2 1
3 Simon 2 4
teams
id name
1 New Zealand
2 London
What I'm asking for is an arrow to information that will allow me to solve the question.
I've tried looking myself but I don't even know the correct terminology to ask the question, googling 'write sql to add fields for each row' just seems to return about adding columns or inserting.
You need to first try to JOIN your tables(id in Teams will be linked to TeamId in Players.) based on the foreign key columns.
Then you need to do the GROUP BY and use the aggregate function SUM to get the goals for each team.
So your query will be like:
select t.name, sum(p.goals) as cnt,
from players p inner join teams t on p.teamid = t.id
group by t.name
First you have to group players by teams : use t1.id=t2.id to join values in the tables, and then group theme by "BROUP BY" t.name.
Then : user "SUM(value)" function who sum values .
select teams.name,sum(players.goals) from players,team where player.team_id=teams.id group by teams.name;
I am new to SQL and was wondering if anyone could help me solve my problem.
I have a table that contains information as follows:
firstname lastname group orderinggroup date
tim s A Facebook 6/4/13
tim s A Facebook 6/4/13
tim s A Facebook 6/4/13
dan d B Google 4/5/12
dan d B Google 4/5/12
Something like that. I want it to look like this
firstname lastname group orderinggroup date
tim s A Facebook 6/4/13
dan d B Google 4/5/12
Where there aren't duplicates for tim and dan. I tried using DISTINCT but that only makes one column distinct, and I actually have many people named Tim, Dan, Groups that are A/B, etc. I was wondering if there is a method to take the distinct of multiple roles, e.g., Distinct of firstname, lastname, group, orderinggroup, and date. Last names matter. Thanks!
You could really use the max() function to aggregate some of the columns and do something like this:
select
firstname,
lastname,
[group],
max(orderinggroup) as orderinggroup,
max([date]) as [date]
from (VALUES
('tim','s','A','Facebook','6/4/13'),
('tim','s','A','Facebook','6/4/13'),
('tim','s','A','Facebook','6/4/13'),
('dan','d','B','Google','4/5/12'),
('dan','d','B','Google','4/5/12')) as A (firstname,lastname,[group],orderinggroup,[date])
--replace this with your tablename
GROUP BY firstname, lastname, [group]
ORDER BY [group]
This is bad data and DISTINCT will work. DISTINCT is actually great for this.
SELECT DISTINCT * from table
Why even list columns unless there is direct manipulation or subqueries going on? There's not even a JOIN in your statement. Please tell me with a comment why DISTINCT does not work in this situation as you mention you've used it before.
DISTINCT checks each column for matching values, if it hits a value that doesn't match on record A from record B it will spit out record B also because it's distinct from A. Even if everything before it matches.
In your example:
firstname lastname group orderinggroup date
tim s A Facebook 6/4/13
tim s A Facebook 6/4/13
tim s A Facebook 6/4/13
dan d B Google 4/5/12
dan d B Google 4/5/12
Let's start with Tim. Every field is exactly the same. Therefore Distinct would collapse all these records into one row. Same for Dan. Now if the lastname is different in your actual database (which should be reflected in your example) then DISTINCT will not work. However, the premise of your question would need to change. You would need to discern what in fact you want to reflect in your data set. Do you want to ignore the last name? Do you want to consider it? These questions are pertinent. Group By works also, but is unnecessary as you're not doing aggregates. I hope this helps.
You can use below query:
SELECT firstname, lastname, group, orderinggroup, date
FROM tablename
GROUP BY firstname, lastname, group, orderinggroup, date
HAVING count(*) = 1;
I am learning SQL now, and I have a question. I recently came across a query that hand a large number of column names in the group by clause. I've used group by clauses before, and I've only ever seen one column name included in it.
SELECT TransportType.Description, TransportType.CargoCapacity, TransportType.Range, Transport.SerialNumber, Transport.PurchaseDate, Transport.RetiredDate,
MAX(Repair.BeginWorkDate) AS LatestRepairDate
FROM Transport INNER JOIN
TransportType ON Transport.TransportTypeID = TransportType.TransportTypeID LEFT OUTER JOIN
Repair ON Transport.TransportNumber = Repair.TransportNumber
GROUP BY TransportType.Description, TransportType.CargoCapacity, TransportType.Range, Transport.SerialNumber, Transport.PurchaseDate,
Transport.RetiredDate
HAVING (Transport.RetiredDate IS NULL)
ORDER BY TransportType.Description, Transport.SerialNumber
Why are there so many columns in the group by clause?
Except in MySQL & SQLite (which are lenient about the GROUP BY with sometimes indeterminate results), most RDBMS require every non-aggregated column (MAX(),MIN(),SUM(),COUNT(), etc) that appears in the SELECT list to be in the GROUP BY.
The behavior of MySQL & SQLite when columns from SELECT aren't listed in GROUP BY is not well defined. If for example, you execute a query like:
SELECT firstname, lastname, COUNT(*) FROM names GROUP BY lastname
MySQL would give you a result without complaint.
However, if your table included two different values of firstname having the same lastname, your resultant COUNT(*) would count both of them while only returning the firstname of one of them. What's more, which firstname MySQL chooses to return isn't defined so you can't really rely on it returning the first of the pair, for example.
From a table like:
firstname, lastname
--------------------
Jane Smith
John Smith
Peter Jones
The not-fully-correct result might be:
firstname, lastname, COUNT(*)
-----------------------------
Jane Smith 2 <----wrong!
Peter Jones 1
Outside MySQL & SQLite, columns referenced anywhere in the SELECT list not also appearing in the GROUP BY will result in a query parse error.
Commonly here on Stack Overflow, we encounter users with questions about the GROUP BY, having just begun working with an RDBMS that is stricter about its usage. If you learn aggregates in MySQL first, chances are you'll need to relearn to do them properly when moving to a different RDBMS.
I'm working on this assignment in Oracle and the question states "Determine the number of different customers who have ordered a book by an author or co-written by John Doe?" Ultimately it's suppose to look like this when completed.
COUNT DISTINCTCUSTOMER
-----------------------
5
My SQL query looks like this which is getting me nowhere.
SELECT customers
FROM books
where author John Doe
Without knowing your data model - use COUNT DISTINCT, e.g:
SELECT COUNT(DISTINCT customer)
FROM books
WHERE author = 'John Doe'
I'm trying to extract info from a table in my database based on a persons job. In one table i have all the clients info, in another table linked by ID_no their job title and the branches theyre associated with. the problem I'm having is when i join both tables I'm returning some duplicates because a person can be associated with more than one branch.
I would like to know how to return the duplicated values only once, because all I care about for the moment is the persons id number and what their job title is.
SELECT *
FROM dbo.employeeinfo AS ll
LEFT OUTER JOIN employeeJob AS lly
ON ll.id_no = lly.id_no
WHERE lly.job_category = 'cle'
I know Select Distinct will not work in this situation since the duplicated values return different branches.
Any help would be appreciated. Thanks
I'm using sql server 2008 by the way
*edit to show result i would like
------ ll. ll. lly. lly.
rec_ID --employeeID---Name-----JobTitle---Branch------
1 JX100 John cle london
2 JX100 John cle manchester
3 JX690 Matt 89899 london
4 JX760 Steve 12345 london
I would like the second record to not display because i'm not interested in the branch. i just need to know the employee id and his job title, but because of how the tables are structured it's returning JX100 twice because he's recorded as working in 2 different branches
You must use SELECT DISTINCT and specify you ONLY want person id number and job title.
I don't know exactly your fields name, but I think something like this could work.
SELECT DISTINCT ll.id_no AS person_id_number,
lly.job AS person_job
FROM dbo.employeeinfo AS ll LEFT OUTER JOIN
employeeJob AS lly ON ll.id_no = lly.id_no
WHERE lly.job_category = 'cle'