I have a table with columns
ID, DateStamp
and the ID need not be unique.
How do I write a query that will give me a list of IDs with the minimum DateStamp and the maximum DateStamp?
So, for example, for a given ID, the output may look like:
938423, 1/1/2000, 12/13/2003
[I am running SQL Server 2000.]
The following should do it:
SELECT ID, MIN(DateStamp), MAX(DateStamp)
FROM TableName
GROUP BY ID
EDIT Added from clause for clarity, be sure to change TableName to the actual table name
Related
I am working with a table in SQLite3 on Python, and the table has four attributes -
ID, added(date when course added to subsection), course_id, course_subsection_title
Of these, the date_added attribute contains the date the course was added to the course_subsection, the course_id contains the ID of the course added, and the ID contains the ID of the course_subsection. The query I have to write groups the courses by the course_subsections, and then calculates the number of days passed between the oldest course added and the most recent course added to the particular course_subsection. I have a query for it as follows, but it appears to be working incorrectly -
query = '''
SELECT
course_subsections.ID as id,
CAST((JulianDay(max(course_subsections.added)) - JulianDay(min(course_subsections.added))) as INTEGER) as num_days_passed
FROM course_subsections
WHERE course_subsections.ID = (
SELECT course_subsections.ID
FROM course_subsections
GROUP BY course_subsections.ID
)
ORDER BY num_days_passed DESC
'''
This isn't working how it should, however. I am very new to SQLite, and still have some confusion about how nested subqueries work in general. Can you help me out with where I am going wrong here?
Remove the WHERE clause.
It filters out all the IDs but one of the table:
SELECT ID,
CAST(JulianDay(MAX(added)) - JulianDay(MIN(added)) AS INTEGER) AS num_days_passed
FROM course_subsections
GROUP BY ID
ORDER BY num_days_passed DESC;
See a simplified demo.
I want count the number of times a single data occured in a column, how can I achieve that using mysqli. For instance I want to know the number of times Victor appeared in the column of name.
If you're using SQL server:
SELECT name, count(1)
from Tablename
where name like 'Victor'
group by name
This query will give you results like - eg Victor appeared 22 times:
Victor 22
Is this what you're looking for?
Please provide more information so its easier to assist.
Try window functions
SELECT ROW_NUMBER() OVER(PARTITION BY Name ORDER BY Name DESC)
AS Total, Name from table
this one will give you count of records with name='Victor'
select count(name) as cnt from t where name='Victor'
this one will give you all names with counts
select name, count(1) as cnt
from t
group by name
order by name
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]
I have an SQL table with duplicate records on FacilityID. FacilityKey is unique. I need to select rows that have duplicates on FacilityID but I only want to show one record for each and I want to choose the one with the most recent (highest) FacilityKey. Can anybody help me figure out how to write my query? I've tried everything I could think of and searched the internet for something similar to no avail. All I can find are examples of identifying the duplicate records.
Something like this should work:
select FacilityID, max(FacilityKey)
from Facilities
group by FacilityID
having count(FacilityID)>1
And then if you want to get all of the fields, something like this:
select *
from facilities
inner join (
select FacilityID, max(FacilityKey) as maxkey
from Facilities
group by FacilityID
having count(FacilityID)>1
) t on t.FacilityID = facilities.FacilityID and t.maxkey=facilities.FacilityKey
try this:
select
FacilityKey , MAX(FacilityID) AS FacilityID
From YourTable
GROUP BY FacilityKey
HAVING COUNT(*)>1
Using SQL Server, I have a table that looks like this:
What I need to do is write a query to identify scenarios where the Name and Permissions field are equal so that I can give give them a unique Set ID.
For instance, rows 2 and 4 would be a set I can give a SetID as well as rows 6 and 7 are a set that I can give another SetID. But rows 2 and 3 are NOT a set.
So far I have tried using DENSE_RANK () Over(Order by Name) which helps to add an id based on like Names but doesn't take into account matching permissions. And have tried joining the table on itself but with millions of rows of data I end up with unwanted duplicates.
The logic I am following is this:
If (Name and Permissions) of one row = (Name and Permissions) of another row give them a SetID to share.
Please help I have been banging my head against the wall with this one. Ideally a SQL query would accomplish this but am open to anything.
Thank you!
You could do it for example like this:
select
Name,
Permission,
row_number() over (order by Name, Permission) as RN
from (
select distinct
Name,
Permission
from
permissions
) TMP
order by Name, Permission
The inner select gets the distinct combinations, and the outer one assigns the numbers.
SQL Fiddle: http://sqlfiddle.com/#!6/c8319/3
This will probably do something similar to what you want.
SELECT
name,
permissions,
accountname,
ROW_NUMBER() OVER (PARTITION BY name,permissions ORDER By name,permissions) as SetID
FROM table;