Access SQL Query - Top 25 containing /images/ - sql

I'm a university student (thus a beginner) with some Access tasks to do, but I need help because so far it does not provide the desired result.
I have a table Table1 containing web log records. My field of interest is "cs-uri-stem", as it contains all the URL GET requests.
I want to select the TOP 25 images (records must contain /images/ in the "cs-uri-stem" field). So far I tried the following, with no success:
SELECT TOP 25
FROM Table 1
WHERE "cs-uri-stem"="cs-uri-stem"
HAVING [/images/];
An alert window keeps appearing saying that the SELECT request is not correct, but I don't know if this is caused by the fact that the Access is in Spanish in my university.
Thanks in advance!
Possible answer to my question, provided with Siyual and MCP_infiltrator's help [Thanks again!]:
SELECT TOP 25
Table1.[cs-uri-stem],
Count(Table1.[cs-method]) AS TotalHits
FROM Table1
WHERE (((Table1.[cs-uri-stem]) Like '*/images*'))
GROUP BY Table1.[cs-uri-stem]
ORDER BY Count(Table1.[cs-method]) DESC`
This will provide a top25 list of only the visited images, with no duplicates, instead of all the URLs.

There are several things wrong with your query, but this should work for what you need:
SELECT TOP 25 Images
FROM Table1
WHERE cs-uri-stem Like '%/images/%'
As for the what/why things are wrong...
For your Select statement, you aren't specifying any fields that you're wanting to get. I'm assuming by your question that you have a field named images that you're wanting to get back. If it's some other field, change that, or just use SELECT TOP 25 * to get everything.
Your From clause has a space in the table name.
Your Where clause makes no sense. This is where you need to be putting your logic for your query. In this case, you want anything that has /images/ in the cs-uri-stem field. Like is the operator you need to use here.
Finally, your Having is just plain wrong. It's not used correctly, nor is it even in the right context.

SELECT TOP 25 *
FROM TABLE1
WHERE CS-URI-STEM LIKE "*/images*"
This will select the top 25 records and all columns from the table where the cs-uri-stem has /images in it. When you use the * that is telling the db that you want all the columns of the table to be pulled for viewing.
Here is a link describing some of it
See Here: Select Top n in MS-Access
Another link
from office.microsoft.com

I would create three queries:
query1:
SELECT Images
FROM Table1
WHERE cs-uri-stem Like '*/images/*'
query2:
select images, count(images) as image_count
from query1 as q
group by images
query3:
select top 25 *
from query2
order by image_count desc
query3 should have the 25 results that you're interested in.

SELECT TOP 25 * FROM Table1 WHERE cs-uri-stem LIKE '%/images/%'
http://www.w3schools.com/sql/sql_like.asp

Related

Group By Using Wildcards in Big Query

I have this query:
SELECT SomeTableA.*
FROM SomeTableB
LEFT JOIN SomeTableA USING (XYZ)
GROUP BY SomeTableA.*
I know that I cannot do the GROUP BY part with wildcards. At the same time, I don't really like listing all the columns (can be up to 20) manually.
Could this be added as new feature? Or is there any way how to easily get the list of all 20 columns from SomeTableA for the GROUP BY part?
If you really have the exact query shown in your question - then try below instead - no grouping required
#standardSQL
SELECT DISTINCT *
FROM `project.dataset.tableA`
WHERE xyz IN (SELECT xyz FROM `project.dataset.tableB`)
As of Group By Using Wildcards in Big Query this sounds more like grouping by struct which is not supported so you can submit feature request if you want - https://issuetracker.google.com/issues/new?component=187149&template=0

iterating through "IN" clause in SQL without a loop

Is there a way to iterate through the "IN" clause in a SQL statement without having to implement some sort of loop?
My example is:
SELECT *
FROM store_table
WHERE store_a IN (1,2,3,4,5,6,7,8,9,10);
Instead of having to list out all of the stores, is there a way to list write some sort of code that it will look like this:
SELECT *
FROM store_table
WHERE store_a IN (1,2,...10);
I understand that the above is not real SQL syntax but for examples sake I wanted to use it in that manner. I know that if you wanted to view all stores there are many different ways, syntactically to do so but I am just worried for the first 100 or the last 20 or etc....
The reason I am asking this is because if you had 1000 stores and you wanted to see the first 500, you would have some pretty long and annoying code.
Any help is appreciated!
If you want to see the first 500 stores, then use:
select st.*
from (select st.*
from store_table st
order by store_a
) st
where rownum <= 500;
This assumes one row per "store", which makes sense for a table with that name.
In Oracle 12C+, you can use fetch first 500 rows only and not have to use a subquery.
Use between instead of the in operator, e.g.
SELECT *
FROM store_table
WHERE store_a BETWEEN 1 AND 10

How to compare data using WHERE clause TSQL

Basically, I want to see the difference in data based on a site code, for instance
SELECT
*
FROM
data.base.table
WHERE
site = 1
site = 2
Even though it's the same table, I want to compare differences based on that site code, but I can't figure out how. My initial chain of thought was to use a left join, but there's only one table.
Thanks
You can get the differences by using EXCEPT
Try this:
SELECT
* --Your column list minus site
FROM
data.base.table
WHERE
site = 1
EXCEPT
SELECT
*--Your column list minus site
FROM
data.base.table
WHERE
site = 2
I am not sure but you may try this:
SELECT
*
FROM
data.base.table
WHERE
site in(1,2)
This will give the data having the site 1 and 2 and then you can compare the two rows

Optimize annotated query in Django

I'm trying to convert this simple SQL query into something Django can handle:
SELECT *
FROM location AS a
WHERE a.travel_distance = (
SELECT MAX(travel_distance)
FROM location AS b
WHERE b.person_id = a.person_id
)
ORDER BY a.travel_distance DESC
What this basically does is fetching all traveled locations and select only the rows that contain the maximum travel distance.
This is what i got so far:
travels = Location.objects.filter(pk__in=Location.objects.order_by().values('person_id').annotate(max_id=Max('id')).values('max_id')).order_by('travel_distance')[::-1]
Although the results match each other. It takes a whole lot longer for the second method to return results.
Is there anyway I can rewrite this query, so it becomes faster?
If I understand correctly you want the maximum distance travelled for each person. Assuming there is a Person model, perhaps ask from the other direction. Something like:
Person.objects.values('id').annotate(max_distance=Max('location__travel_distance'))
I haven't tested this since I don't have an equivalent data schema handy, but does this work for you?
Isn't this works? Something like:
select max(id), sum(travel_distance) from table group by person_id;

changing sorting criteria after the first result

I am selecting from a database of news articles, and I'd prefer to do it all in one query if possible. In the results, I need a sorting criteria that applies ONLY to the first result.
In my case, the first result must have an image, but the others should be sorted without caring about their image status.
Is this something I can do with some sort of conditionals or user variables in a MySQL query?
Even if you manage to find a query that looks like one query, it is going to be logicaly two queries. Have a look at MySQL UNION if you really must make it one query (but it will still be 2 logical queries). You can union the image in the first with a limit of 1 and the rest in the second.
Something like this ensures an article with an image on the top.
SELECT
id,
title,
newsdate,
article
FROM
news
ORDER BY
CASE WHEN HasImage = 'Y' THEN 0 ELSE 1 END,
newsdate DESC
Unless you define "the first result" closer, of course. This query prefers articles with images, articles without will appear at the end.
Another variant (thanks to le dorfier, who deleted his answer for some reason) would be this:
SELECT
id,
title,
newsdate,
article
FROM
news
ORDER BY
CASE WHEN id = (
SELECT MIN(id) FROM news WHERE HasImage = 'Y'
) THEN 0 ELSE 1 END,
newsdate DESC
This sorts the earliest (assuming MIN(id) means "earliest") article with an image to the top.
I don't think it's possible, as it's effectively 2 queries (the first query the table has to get sorted for, and the second unordered), so you might as well use 2 queries with a LIMIT 1 in the first.