Why is my count function only working on certain tables? - mysql

I know this is an amateur question, but I've searched every resource I can think of, and now I'm at my wit's end. The following query works perfectly on most of my tables, but for some reason it is not working on the tables that I desperately need it for:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE daypart LIKE 'AM';
The query works exactly how I want it to for nearly all of my tables, but for some reason it is returning a value of "0" on the tables I need it for (even though there are over 20 instances of "AM" in the "daypart" column on this table). I have checked and double-checked everything I can think of.
I'm relatively new to SQL but I've never encountered a problem like this before. Anyone have any ideas or resources that might help? Thanks so much for your time!
EDIT: I don't have enough reputation points to post a screen shot on here... but here's a link where you can see one: http://imgur.com/ZhyEqJY
There are 29 columns in this table. If there's any other info that might help just let me know, thanks!

You need to add the like part as shown below:
where column_name like '%AM%'
when you write like 'AM' it is searching for the full match

Try this.
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
If you want to order it using the count,
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%' ORDER BY COUNT(*) DESC;
DESC - Descending order
ASC - Ascending order

LIKE statements are really slow in sql. If you have a daypart column then I assume you have a date column. I recommend something like this instead:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE HOUR(dateColumn) < 13

Unless the string you're matching is exactly AM you need to use wildcard characters to match the string:
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
The % matches any number of characters before and after AM.
See the documentation for more information.

Related

mysql min and max values in subquery

thanks in advance for the help. I'm a noob with sql and this is probably a pretty basic question, but I have been working on it for hours and can't seem to figure out what I'm doing wrong.
Here is the question posed:
Using aggregate functions, display the employee name (first and last name concatenated) in a field named Employee Name and the hire date of the employee who has been employed the longest and the employee who was hired last.
I have tried to code this different ways to no avail. I always get one result instead of the two I know I should get. (I know I should get two results because I'm working with a very small table and can calculate the answer by looking at it - easy to make sure I'm getting the right results that way.)
Here is one way I coded it:
subquery
Here, I used a sub query for the min and max. I used 'and' in between because I want both results. I only get one. If I use 'or', I get too many results
I have also tried it this way:
min/max in select statement
Here, I still get only one employee name back, but now I get both dates. The dates are the correct dates, but I need the output to look like the first query output.
Any help anyone can provide would be greatly appreciated!
How about
select concat(first_name,' ',last_name), hire_date
from l_employees
where hire_date = (select max(hire_date) from l_employees)
or hire_date = (select min(hire_date) from l_employees)

Mysql not working when used double alias

Hey guys am really new to mysql.I have heard of alias in mysql and i have tried double alias with the string like
select ('name' as bae,'age' as ages) as person;
When i run the above code it doesnt give me the output and raises the error.I dont understand why the double alias didnt works in mysql.
Any help to make this one correct would be really appreciated..Thanx in advance
The problem with your query are the parentheses. They represent a single expression and you don't put aliases (or commas usually) inside expressions. So, this fixes the immediate problem:
select 'name' as bae, 'age' as ages
I'm not sure what the second "as person" is supposed to be. Perhaps you want a subquery:
select person.*
from (select 'name' as bae, 'age' as ages) person
A really basic MySQL query should look something like:
SELECT
[COLUMN_NAME] AS [ALIAS_NAME]
FROM
[TABLE_NAME]
Based on your query it appears you are trying to retrieve the two columnns name and age from the person table.
If this is the case, the following query can help you:
SELECT
name AS bae,
age AS ages
FROM
person
But, this is still just one big guess, if you really want us to help you with your problems, you should give some more information about what you are trying to achieve.

I want to use field from select statement in AVG function in Mysql

select count(distinct installation_id), SUM(hit_block), AVG(???)
This is the select statement. I want to divide the value returned by first field (count(distinct installation_id)) by the second field. Please suggest me how to do it.
select count(distinct installation_id)/SUM(hit_block),AVG(??)
Basically you can do numerical operations in between what could be separate selectable clauses.
I'm not sure what you want to find the average of?
i dont think i understand very clearly what is it that you want to do, perhaps if you add an example data set, and the a description of the problem you are trying to solve by running that query, if what you want to do is just divide you can apply the arithmetic operands
note that this is just an example and wont probably work as i dont have a way of testing this on your enviroment
SELECT (COUNT(DISTINCT installation_id) / SUM(hit_block))
,AVG(???)
FROM ????
GROUP BY ????
note that you should probably replace the question marks with real values ???, is this what you were looking for? if not please explain i can probably still help

MySql skips first record found

in my database i have 10 records with almost exact same data , they differ only by one field ( the field is not in the query) and when i run the following query
SELECT * FROM friends WHERE user_id= 'MyUserName' AND follow_back = 0 AND until_date= '2009-10-13' LIMIT 12
it shows only 9 records , any one stumbled upon similar problem ?
Thanks & waiting for your answers !
The short answer is there's nothing wrong with your query, so
user_id!='MyUserName'
or
follow_back != 0
or
until_date != '2009-10-13'
Try just querying on one criterion at a time and see if you can norrow it down. Perhaps follow_back is NULL?
When trying to debug problems like these, what I would usually do is to try solving it using a divide and conquer approach.
So try and remove one where condition at a time, then execute the query. That way you will be able to isolate the offending condition.
Good luck
Are you sure, that all values in column user_id are the same? Maybe that one missing record has user_id = 'MyUserName ' (note the space).
I had the same problem a minute ago. It turned out it wasn't the query that was the problem, but the IF where I check if anything's returned. Might want to check that.

MS-Access design pattern for last value for a grouping

It's common to have a table where for example the the fields are account, value, and time. What's the best design pattern for retrieving the last value for each account? Unfortunately the last keyword in a grouping gives you the last physical record in the database, not the last record by any sorting. Which means IMHO it should never be used. The two clumsy approaches I use are either a subquery approach or a secondary query to determine the last record, and then joining to the table to find the value. Isn't there a more elegant approach?
could you not do:
select account,last(value),max(time)
from table
group by account
I tested this (granted for a very small, almost trivial record set) and it produced proper results.
Edit:
that also doesn't work after some more testing. I did a fair bit of access programming in a past life and feel like there is a way to do what your asking in 1 query, but im drawing a blank at the moment. sorry.
After literally years of searching I finally found the answer at the link below #3. The sub-queries above will work, but are very slow -- debilitatingly slow for my purposes.
The more popular answer is a tri-level query: 1st level finds the max, 2nd level gets the field values based on the 1st query. The result is then joined in as a table to the main query. Fast but complicated and time-consuming to code/maintain.
This link works, still runs pretty fast and is a lot less work to code/maintain. Thanks to the authors of this site.
http://access.mvps.org/access/queries/qry0020.htm
The subquery option sounds best to me, something like the following psuedo-sql. It may be possible/necessary to optimize it via a join, that will depend on the capabilities of the SQL engine.
select *
from table
where account+time in (select account+max(time)
from table
group by account
order by time)
This is a good trick for returning the last record in a table:
SELECT TOP 1 * FROM TableName ORDER BY Time DESC
Check out this site for more info.
#Tom
It might be easier for me in general to do the "In" query that you've suggested. Generally I do something like
select T1.account, T1.value
from table T as T1
where T1 = (select max(T2.time) from table T as T2 where T1.account = T2.Account)
#shs
yes, that select last(value) SHOULD work, but it doesn't... My understanding although I can't produce an authorative source is that the last(value) gives the last physical record in the access file, which means it could be the first one timewise but the last one physically. So I don't think you should use last(value) for anything other than a really bad random row.
I'm trying to find the latest date in a group using the Access 2003 query builder, and ran into the same problem trying to use LAST for a date field. But it looks like using MAX finds the lates date.
Perhaps the following SQL is clumsy, but it seems to work correctly in Access.
SELECT
a.account,
a.time,
a.value
FROM
tablename AS a INNER JOIN [
SELECT
account,
Max(time) AS MaxOftime
FROM
tablename
GROUP BY
account
]. AS b
ON
(a.time = b.MaxOftime)
AND (a.account = b.account)
;