|
I have a MS Access database with a table INFO that has Name column (only contains First Name), since it contains a huge list, it will require very long time to queury all of them. I prefer to devide them by character groups (A,B,C ... Z) in order to reduce the queury time
I couldn't find the the SQL command that can queury table INFO with column Name starting with A character
Anyone know how to do it?
Thanks to any help 
|
|
|
|
|
WHERE Name LIKE 'A%' ORDER BY Name should do it as % is the wildcard character
|
|
|
|
|
I did try 'A%' somehow it doesn't work, but if I try with 'A*' like the latter response then it work!
Anyway, thank for help just might be syntax from me 
|
|
|
|
|
When I use Microsoft.Jet.OLEDB.4.0 to work with an Access database, it takes % as the wildcard character.
|
|
|
|
|
It's been a long time since I used Access, but IIRC, a % symbol represented a single arbitrary character, while the * meant any number of characters, much like DOS and the ?/* pair. "A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Roger Wright wrote: IIRC, a % symbol represented a single arbitrary character
Not quite.
Apparently there are two standards, one using * the other % for "any string", see here[^]. I have a C#/Access application that works well with % (and not at all with *)but reading that page seems to tell me I should use *, not %.
|
|
|
|
|
Try a select statement like this:
select * from INFO where Name like "A*"
This will return all rows with column, Name, beginning with the Letter A.
|
|
|
|
|
It works beautifully
Thanks for help 
|
|
|
|
|
Hello there
I have a table which contains the following fields...
artist
title
count_played
date_title_played
I am trying to figure out how to present a top ten most played, but I can't seem to get my head around how to stop it showing duplicate count_played. At the moment I have used the following Select...
SELECT top 10 artist, title, count_played, date_title_played
FROM dbo.songlist
WHERE count_played >0
ORDER BY count_played DESC, date_title_played DESC
However this is what shows...
Artist Title Count Last Played
Wanted By The FBI Until the End of the World 22 09/03/2010 11:22:16
Koko Conley Keep Your Head Up 22 09/03/2010 10:35:07
Red Sun Compromise 21 09/03/2010 11:08:57
Cara Friend of Mine 21 09/03/2010 11:01:09
Preet Whos going to love you live 20 09/03/2010 10:51:22
The Ambersons Oh, my Isabelle 19 09/03/2010 10:51:22
Patchwork Grace Boozawaffle 19 09/03/2010 10:49:01
VK Lynne Find Me 18 09/03/2010 10:45:33
Clara Barker Wishing 17 09/03/2010 10:43:45
Red Sun Just To Hold You 17 09/03/2010 10:11:00
What I actually want is...
Artist Title Count Last Played
Wanted By The FBI Until the End of the World 22 09/03/2010 11:22:16
Red Sun Compromise 21 09/03/2010 11:08:57
Preet Whos going to love you live 20 09/03/2010 10:51:22
The Ambersons Oh, my Isabelle 19 09/03/2010 10:51:22
VK Lynne Find Me 18 09/03/2010 10:45:33
Clara Barker Wishing 17 09/03/2010 10:43:45
and so on.
I am not that experienced with sql queries but if someone could point me in the right direction on this it would be very much appreciated. Is it some sort of grouping that i need to use?
Regards
Ray
|
|
|
|
|
For a start I am just a little bit confused. If you wanted the top 10 most played songs, then surely it doesn't matter if 2 songs have been played the same amount of times. If you really want to do it this way, then you need to specify DISTINCT on your count_played field i.e SELECT DISTINCT countplayed, artist, title FROM ... etc Hope this helps
[EDIT]
To limit the resultset to the top 10 would depend on your database. SELECT TOP 10 ... would work with access and SQL Server, but with MySql you need to go SELECT DISTINCT count_played FROM songlist ORDER BY count_played DESC LIMIT 10 modified on Tuesday, March 9, 2010 8:18 AM
|
|
|
|
|
Hi
Thank you for your help, I am using sql 2000 however if I use DISTINCT the results seem to stay the same, if I only use the count_played field then yes it shows only single rows of a particular count number but then how do I include the artist and title fields?
I may not be seeing the wood for the trees as I don't really know much sql sorry.
Regards
Ray
|
|
|
|
|
Try a sub query that finds max [Last Played] by Count.
select ...
from songlist
inner join (select count, max([last played])...
If you need more let me know.
djj
|
|
|
|
|
Sorry I was pressed for time.
The sub query should be something like:
SELECT top 10 A.artist, A.title, A.count_played, B.MaxDate
FROM dbo.songlist A
INNER JOIN (
SELECT count_played, MAX(date_title_played) AS MaxDate
FROM dbo.songlist
GROUP BY count_played
) AS B
ON A.date_title_played = B.MaxDate
WHERE count_played >0
ORDER BY count_played DESC, date_title_played DESC
This most likely will not run but again as this is of the top of my head. The join condition (ON) may need work.
|
|
|
|
|
Sorry so long with this reply I also get pressed with work time unfortunately.
I tried the suggested query and have now got it running fine, for this I thank you, however I do have a small issue and can't seem to find the answer to it.
If 2 songs exist with the same name
i.e.
"only you" by the flying pickets which has 47 plays
"only you" by the platters which has 14 plays
It will show both even though 14 plays does not fit with the top 5 most played
I slightly altered you code to fit my needs as follows...
SELECT top 5 A.artist, A.title, A.count_played, B.MaxDate FROM dbo.songlist A INNER JOIN (SELECT count_played, MAX(date_title_played) AS MaxDate
FROM dbo.songlist GROUP BY count_played ) AS B ON A.date_title_played = B.MaxDate
WHERE B.count_played >0
ORDER BY B.count_played DESC, date_title_played DESC
Any ideas ?
Thanks in advance
|
|
|
|
|
Try a sub query (top 5) that is grouped by title and ordered by number of plays.
Let me think about this and hopefully get back to you soon.
djj
|
|
|
|
|
How do I update a date/time field in a Microsoft Access database using SQL ?
I am using VB .net and up to now all my sql statements work fine.
I have tried several variations of this statement but none work. They all give me a generic "Error in UPDATE statement" message.If i try any other column of my database, the UPDATE statement works fine.It only has a problem with the TIME field which is declared in the database as DATE/TIME.
For instance the following will NOT work.
UPDATE tablename SET timecolumn='12:34' where id=82
|
|
|
|
|
Never mind.I figured it out.
I needed brackets for the time field.
So it should be :
UPDATE tablename SET [time]='12:34' where id=80
|
|
|
|
|
Hi all,
I am having a table in which one field data type is text and the data stored in it is characters + numbers for example SD01TU9,SD01TU8,SD01TU10.
Now the problem is i want the max from it.As in the above example the max should show SD01TU10 but when i use max function on that field it shows SD01TU9.
Please any one can solve it.
Thanks in advance.
|
|
|
|
|
You have a number of choices, you have a requirement to do numerical operations on a field with mixed data.
Create another field and extract the numeric component from the mixed field
Use a view (query in Access I think) to do the same thing just don't store it
Go back to your initial design that put the mixed data in there and address the problem at the source.
As your data seems to be consistent (SD and TU) you could do this with nested replace statements. It gets a little tricky if your text data changes from the standard, then use regex.Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Are you stalking me?
|
|
|
|
|
If all the strings include the letters "TU" before the numbers at the end, you could use a combination of instr() and len() and right() to get what you need...
I'm doing this from memory and without access to Access to test, but it would be something like this:
clng(right(FIELDNAME, len(FIELDNAME)-instr(FIELDNAME, "TU")))
Which basically says locate the position of TU in the field, then take the characters to the right of them, convert to long and then you can happily find the max of those numbers.
|
|
|
|
|
If you have the option, you might consider ensuring that the data all have the same number of digits at end.
E.g. use SD01TU09 instead of SD01TU9, or SD01TU009 if there can be three digits.
This would mean you don't have to monkey around with substrings.Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Hi everyone,
I'm creating a database installer where I have some code to create database, tables, stored procedures, etc.
I don't have problems when creating the database, tables and inserting required data. My problem is when my code gets to the stored procedures section.
Here is what I do:
I have different text files (database.txt, tables.txt, insertdata.txt, storeprocedures.txt) where my sql syntax is located.
I execute every process/text file using the following:
ExecuteSql("master", GetSql("database.txt")) ' Creating 1 database.
ExecuteSql("mynewdatabase", GetSql("tables.txt")) ' Creating 15 tables.
ExecuteSql("mynewdatabase", GetSql("insertdata.txt")) ' Inserting data to many tables.
ExecuteSql("mynewdatabase", GetSql("storeprocedures.txt")) ' Creating 6 stored procedures.
Here is part of the error:
In exception handler: Incorrect syntax near the keyword 'PROCEDURE'. "This is the second stored procedure because the variables I get below belong to the second one"
Must declare the scalar variable "@intVariable1".
Must declare the scalar variable "@intVariable1".
Must declare the scalar variable "@intVariable1".
Must declare the scalar variable "@intVariable1".
When trying to create 6 or even 2 stored procedures using the same file ‘storeprocedures.txt' is not possible. If I leave just 1 it works but I really want to keep them together.
Creating 15 tables using the same file 'tables.txt' works, inserting data in different tables using the same file 'insertdata.txt' works.
This is the general syntax I use:
Create Procedure sp_stored1
@intVariablex int
As
Begin
Code
End
Create Procedure sp_stored2
intVariabley int
As
Begin
Code
End
..........
..........
..........
Is is possible to create more than 1 stored procedure the way I'm trying to?
Do I need to end every stored procedure with specific sql syntax, other than the 'end'?
I appreciate your response!!!
Thanks,
Israel
|
|
|
|
|
Try placing GO between each store proc creation script to separate each batch I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Thanks Mark but I have tried that and only works when using the sql editor. I actually had to remove the GO from my other text files (database.txt, tables.txt) in order to work.
Thanks,
Israel
|
|
|
|