Filtering out some rows - mysql

I'd like to filter out some rows in SQL. This is my table.
ID | year
1 2002
1 2003
2 2003
2 2004
3 2002
I'd like to filter the ID's that has got a 2003 in the year column.
That means in this case, there will be no ID's that has got 2003 in the year column.
What is the SQL code I should use?

Your question is a bit ambiguous. One interpretation is to get the list of ids that do not contain the year 2003:
select id
from t
group by id
having sum(year = 2003) = 0

Have a look here http://www.sqlfiddle.com/#!2/bd0d6/3
select id
from dateyear
where year<> 2003
group by id

If you want to list all IDs that do not have a 2003 in the year column then year != 2003, as it was suggested by other users, won't be enough because that will return id = 2 as it contains 2004 too.
A good approach is to list get all IDs and remove from them the ones that contain 2003 in the year. You can do that this way:
SELECT DISTINCT id FROM t
WHERE id NOT IN (
SELECT id FROM t
WHERE YEAR = 2003
)
This will only output id = 3.
Fiddle here.

Related

Subquery in the WHERE clause exercise

I have a migration table with attributes (name, departure year) and a stork table with attributes(name, year of birth). I want to write a query giving the names of all storks that migrated before the birth of the youngest stork which is 2002 (using the migration table in the main query and the Stork table in a sub-request of the WHERE clause). N.B: Not allowed to use a clause such as WHERE, GROUP BY, ORDER BY, LIMIT, unless it is necessary/mandatory
Migration table
Name DEPARTURE YEAR
Annamarie 2001
Felix 2002
Annamarie 2003
Felix 2004
Jonas 2001
Stork table
Name YEAROFBIRTH
Annamarie 1998
Felix 1999
Max 2000
Jonas 2001
Christina 2002
Using the DISTINCT operator gives the desired result with 15 names, removing the DISTINCT operator gives me 25 names with duplicates
SELECT DISTINCT migration.NAME
FROM migration
WHERE migration.DEPARTUREYEAR IN(SELECT stork.YEAROFBIRTH
FROM stork
WHERE stork.YEAROFBIRTH <2002)
I expect to obtain the 15 names without using the DISTINCT operator
Just going by the desired result, and your example's use of the literal year; this should be all you need.
SELECT DISTINCT migration.NAME
FROM migration
WHERE migration.DEPARTUREYEAR < 2002
;
However, if you want to not use a literal year, the subquery can be used to determine it like so:
SELECT DISTINCT migration.NAME
FROM migration
WHERE migration.DEPARTUREYEAR < (SELECT MAX(stork.YEAROFBIRTH) FROM stork)
;
Try this-
SELECT NAME -- You can apply DISTINCT if required
FROM Migration
WHERE Name IN (
SELECT Name FROM Stork
WHERE YEAROFBIRTH < 2002
)

Ranking Values in .asp using Access

fairly new to .asp and the boards, so please bear with me. I am trying to utilize an access DB which stores 24 unique records (baseball teams) with values for TeamName, TeamUID (unique ID), TotalHR, TotalRS, TotalWins and TotalLosses. I would like to use this table to display where a team ranks in each category. For example each team has their own page which is based on their TeamUID. I want to show where they rank (1st through 24th) in each category but cannot figure out how to do that in the page. Any suggestions?
You can assign a rank with MS Access, for example:
SELECT table1.ANumber,
(SELECT count(*)
FROM table1 t
WHERE t.anumber<=table1.anumber) AS rank
FROM table1
WHERE (((table1.ANumber) Is Not Null))
ORDER BY table1.ANumber;
Run against an ADO connection, you can use GetString or GetRows to assign this to a string or array.
The above example will give a duplicate number where there is a duplicate value for ANumber. For example:
ANumber Rank
-1 1
1 7
1 7
1 7
1 7
1 7
1 7
2 11
2 11

Trouble with a SQL using between and multiple occurences

everyone. I'm new here and I have a question on SQL which I seem to think that it may be easy to do in SQL but I'm out of ideas and I could use some help with my problem.
So I have a table (sql also here http://www.sqlfiddle.com/#!2/b9a37/1/0)
With multiple occurences for each person, different id's with auto increment and I have 3 fields, "Category", "In" and "Out"
What I'm trying to implement is a way for me to know when some of the users on the table belongs to the system using the "In" and "Out" as the limits.
Example
User A - Category A - IN 2002 OUT 2010
User A - Category B - IN 2011 OUT 9999
User B - Category B - IN 2002 OUT 2010
In the above example I would like to know who belongs to the system in year 2010:
so for User A, he Left Category A in 2010 but joined in 2011 Category 2011 so he SHOULD be a user in 2010 with Category A
for User B he should NOT belong to the system because there's no new entry for 2010+
At the end I'm tring to have the following output for 2010
Smith Category A
Pablo Category A
I'm not achieving the results I want, for example
SELECT * from users
WHERE (users.In<=2010 and users.Out=2010)
Outputs
Smith Category A In 2002 Out 2010
Roger Category B In 2002 Out 2010
Miller Category B In 2008 Out 2010
Pablo Category A In 2002 Out 2010
But Roger and Miller left the system in 2010 so they should NOT be members in 2010, and Smith and Pablo changed their category in 2010+ so they should show in the output.
A few notes:
Don't ask me why this solution is developed like this, I just stumbled on it and trying to solve this issue.
I'm considering doing a function in php do filter the data based on the requirements that I want because i'm running out of ideas to solve this.
If you find a way to implement this better than what I saw here, be my guest and i'm open to new ideas.
Thanks in advance.
Final Note:
Perhaps using http://www.sqlfiddle.com/#!2/b9a37/1/0 and try to find the desired output will help you out.
Give this one a shot:
select Name, Category
from users
where `In` <= 2010 and (
`Out` > 2010 or name in (
select name
from users
where `In` = 2011
)
)
SQL Fiddle Example
This appears to work:
select * from users a, users b where a.name = b.name and a.out + 1 = b.out
However, it will not work correctly if more than one user has the same surname and it assumes that the out year is one less than the in year.

Group and Sum with dynamic column names

I need to GROUP a table by Year and SUM all the possible Types (unknown) with dynamic column names.
Sample Table:
Type|Year
a 2001
a 2001
c 2002
b 2002
c 2003
a 2003
z 2003
Sample Result:
Year: 2001, Type_a: 2
Year: 2002, Type_c: 1, Type_b: 1
Year: 2003, Type_c: 1, Type_a: 1, Type_z: 1
You could group and sum types using a query like this -
SELECT year, type, COUNT(type) FROM table_name GROUP BY year, type;
It gives another result set, but with data you want.
SELECT year,COUNT(type) from tableName GROUP BY(type)
try that one
SOL is not designed for that, the result could never have a various number of column for each line.
I think the best way to get that is to change the design of your resultset with concatenation of information by example. Or having fixed number of.column filled by null or empty values.
In the other hand you can do it programmaticaly if your language allows dynamic number of column for each row.

Running count of distinct values in Access

I have data stored as below in an MS Access database:
Date User
20090101 1001
20090101 1002
20090102 1001
20090103 1001
20090103 1003
I'm attempting to create a query which shows the daily running count of unique users. For example:
Date Daily Count Unique User Running Count
20090101 2 2
20090102 1 2
20090103 2 3
What's the best way to achieve this?
In most SQL implementations you could select using the aggregate function count(distinct user). But Access doesn't support that construct. I think the best you could do is to select the distinct values in a subquery and count them.
I was going to write a query but this link seems to do a good job.
HTH
Tom
Your query will look something like this...can't test it without the data though:
SELECT Date, Count(Date) As [Daily Count], Count(User) As [Unique User Running Count]
FROM TableName
GROUP BY Date
I did it! A simple solution is the best: no SQL coding needed.
In Access, Query Design,
Column 1 =
Field=Date
tablename=yourname
Total=Groupby
Column2 =
Field=Date
Table=yourname
Total=Count