I am using SELECT DISTINCT name FROM table to get a table with distinct names that appear in the column. How can i get an extra column returning the number of times that this specific name have appeared on the column?
select name, count(1) from table
group by name;
Related
QUESTION: I need to develop a query that looks for certain records that starts with a search term and records that contains the search term. Furthermore, both subsets should be sorted by a certain column, whereas the resultant set shouldn't be sorted. Records starting with term should come above the contains term records.
Example: I have students table and I want all students whose names start with "Jhon". Students having the first name "Jhon" should come first and after then all those students whose last name is "Jhon".
What I have are as following:
Got all records starting with the search term and save it into a temptable_A , than got all records all records containing the search term and excluding results that are already in temptable_A and save into temptable_B. Now both temptable should have respected results, so I dump tempTable_B into tempTable_A, believing that the new records are append at last of the table. But they are not, they are inserted in and are sorted, where are I haven't applied sorting.
I have done the same with a merge statement and it does the same thing, but no fruitful result.
I have tried Union between both sub queries (Start with and contains) but the resultant dataset always doesn't show the start with records on top.
Scenario:
Students Table with column
Id | Student
select * FROM students where name like 'jhon%'
UNION
select * FROM students where name like '%jhon%'
Use an order by. For instance:
select s.*
from students
where name like '%jhon%'
order by charindex('jhon', name);
This orders by how far down 'jhon' is in the string. If you just want the ones that start 'jhon' first, you can use a case expression:
order by (case when name like 'jhon%' then 1 else 2 end)
You can do this with a case expression in the ORDER BY clause:
select * from students where Name like '%jhon%' -- select all students containing john
order by
case -- order by place of match first
when Name like 'john%' then 0
when Name like '%john%' then 1
end,
Name -- then order by Name
I am working with my lab database and close to complete it. But i am stuck in a query and a few similar queries which all give back the similar results.
Here is the Query in design mode
and this is what it gives out
This query is counting the number of ID values in table PatientTestIDs whereas I want to count the number of unique PatientID values grouped by each department
I have even tried Unique Values and Unique Records properties but all the times it gives the same result.
What you want requires two queries.
Query1:
SELECT DISTINCT PatientID, DepartmentID FROM PatientTestIDs;
Query2:
SELECT Count(*) AS PatientsPerDept, DepartmentID FROM Query1 GROUP BY DepartmentID;
Nested all in one:
SELECT Count(*) AS PatientsPerDept, DepartmentID FROM (SELECT DISTINCT PatientID, DepartmentID FROM PatientTestIDs) AS Query1 GROUP BY DepartmentID;
You can include the Departments table in query 2 (or the nested version) to pull in descriptive fields but will have to include those additional fields in the GROUP BY.
We seem to be getting two different, mutually incompatible results from legacy SQL and standard SQL in Google Big Query.
Here is our standard SQL Query...which gives an answer with 218,529 rows.
SELECT DISTINCT(EID)
FROM test.ourBQtable
Here is our legacy SQL Query...
SELECT COUNT(EID) AS Total, EID
FROM [ourBQproject:test.ourBQtable]
GROUP BY EID
ORDER BY Total DESC
This shows results that look like the table below but yet also shows 218,529 rows of results:
Total EID
376 jb+qLvHMm5JrMkNybAi6uC75FzgsGcNQhJ19IeWFDcQ=
352 JGqNBgicm+mpcYBS4K7AI2WXI3xaSgMkktb+7oOjjnQ=
How is it possible to have what appears to be duplicate EIDs (376 of them as shown in one case in the table) - but when using the DISTINCT(EID) command - the number of rows doesn't decrease? Shouldn't DISTINCT be filtering out all the duplicate rows? Do we really have duplicate rows?
What are we missing in our understanding?
Your code appears to be working exactly correctly.
DISTINCT EID is saying that there are 218,529 different values of EID. This should be returning one row for each of the 218,529 different EIDs.
When you use GROUP BY, you are getting one row for each of the EIDs. In this case, you get the same number.
Try running this query:
SELECT COUNT(*) as num_rows, COUNT(DISTINCT EID) as num_eids
FROM test.ourBQtable;
This will show the number of rows in the table and the number of distinct values of EID (ignoring NULL values)`.
Below two query are equivalent and return same number or rows - one per each unique EID
SELECT DISTINCT EID
FROM test.ourBQtable
and
SELECT EID
FROM test.ourBQtable
GROUP BY EID
That explains why number of output rows are the same
Now, in second query you added COUNT(EID)
SELECT COUNT(EID) AS Total, EID
FROM test.ourBQtable
GROUP BY EID
this does not change the number of output rows, but rather adds count of rows in test.ourBQtable with respective EID (if you sum all these counts - you will get total rows in the original table)
I have the results in the top table. I would like the results in the bottom table.
Using an SQL query on the table above, I would like to find groups of duplicates (where the values in all columns except Id and Category are identical) and from that create a result that has for each entry the lowest Id from its group of duplicates and the (unmodified) Category from the original table.
Window function min can be used here:
select min(id) over (partition by first_name, last_name, company) id,
category
from t;
I need the second column of the table retrieved from a query to have a count of the number of rows, so row one would have a 1, row 2 would have a 2 and so on. I am not very proficient with sql so I am sorry if this is a simple task.
A basic example of what I am doing would be is:
SELECT [Name], [I_NEED_ROW_COUNT_HERE],[Age],[Gender]
FROM [customer]
The row count must be the second column and will act as an ID for each row. It must be the second row as the text file it is generating will be sent to the state and they require a specific format.
Thanks for any help.
With your edit, I see that you want a row ID (normally called row number rather than "count") which is best gathered from a unique ID in the database (person_id or some other unique field). If that isn't possible, you can make one for this report with ROW_NUMBER() OVER (ORDER BY EMPLOYEE_ID DESC) AS ID, in your select statement.
select Name, ROW_NUMBER() OVER (ORDER BY Name DESC) AS ID,
Age, Gender
from customer
This function adds a field to the output called ID (see my tips at the bottom to describe aliases). Since this isn't in the database, it needs a method to determine how it will increment. After the over keyword it orders by Name in descending order.
Information on Counting follows (won't be unique by row):
If each customer has multiple entries but the selected fields are the same for that user and you are counting that user's records (summed in one result record for the user) then you would write:
select Name, count(*), Age, Gender
from customer
group by name, age, gender
This will count (see MSDN) all the user's records as grouped by the name, age and gender (if they match, it's a single record).
However, if you are counting all records so that your whole report has the grand total on every line, then you want:
select Name, (select count(*) from customer) as "count", Age, Gender
from customer
TIP: If you're using something like SSMS to write a query, dragging in columns will put brackets around the columns. This is only necessary if you have spaces in column names, but a DBA will tend to avoid that like the plague. Also, if you need a column header to be something specific, you can use the as keyword like in my first example.
W3Schools has a good tutorial on count()
The COUNT(column_name) function returns
the number of values (NULL values will not be counted) of the
specified column:
SELECT COUNT(column_name) FROM table_name;
The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name;
The COUNT(DISTINCT column_name) function returns the number of
distinct values of the specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name;
COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but
not with Microsoft Access.
It's odd to repeat the same number in every row but it sounds like this is what you're asking for. And note that this might not work in your flavor of SQL. MS Access?
SELECT [Name], (select count(*) from [customer]), [Age], [Gender]
FROM [customer]