To write a query where it would extract the number of requests on various fields . [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want write a query which would give two columns :
1> Type of Query 2> Count
This result set should have following structure
Here the 1st column values should be predefined and the count has to
be calculated . I want to check the Request column of source table and
find specific pattern . If that pattern is found increase the count .
For Example :
If there is a word "greenhopper" found in the request column then that
belongs to type GREENHOPPER .
OR
If there is a word "gadgets" found it is of the type DASHBOARD . and
so on ...
So I want to analyze the usage of various categories by using the log
table .
Hence Finally I can get the amount of usage and after that I can build
a pie chart out of it .

SELECT 'Greenhopper' AS TypeOfQuery, COUNT(*) AS Cnt
FROM YourTable
WHERE Request LIKE '%Greenhopper%'
UNION ALL
SELECT 'Dashboard', COUNT(*)
FROM YourTable
WHERE Request LIKE '%gadgets%'
-- And so forth
You said they were predefined right? So you'd have ~10 different statements UNION'd together.

WITH Requests AS
(
SELECT
CASE
WHEN Request LIKE '%Greenhopper%' THEN 'GreenHopper'
WHEN Request LIKE '%gadgets%' THEN 'Gadgets'
-- and so on
ELSE 'Misc'
END RequestType
FROM YourTable
)
SELECT
RequestType,
COUNT(*) RequesCount
FROM Requests
GROUP BY RequestType
;
No data to test but I believe this approach will perform better as the table will be scanned less times. Performance is never going to be ideal though because of the LIKE and first wild card. This will prevent seeks.
Further explanation of why LIKE does not perform here
Having just re looked at the question you may be able to improve performance further by changing the search string so it only has a wildcard on the right
e.g. LIKE 'GET /rest/gadget%' and so on.

Related

Is there an easy / built-in way to find relevant concept from a given one? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
Based on
keywords extracted from URLs
a given keyword
I would like to find the most relevant keywords where the relevance score will be calculated by looking up how many times a random keyword has been extracted from the same URL that the given one
As an example:
I'm providing Soccer keyword which has been extracted from 100 URLs
From those 100 URLs, I have also extrated Argentina 80 times and France 75 times
=> I would then have (Soccer, Argentina) = 0.8 & (Soccer, France) = 0.75
I'm using a relational database, PostgreSQL, where I created 2 tables URL & Keywords.
Database schema
I can easily, from a given keyword, get all the URLs where the keyword has been extracted.
From those URLs I can retrieve all other keywords and "count/group by" them to further determine the relevance score:
SELECT SCE1.keyword, count(*) as count from Keywords SCE1
LEFT JOIN Keywords SCE2 ON SCE1.URL_id = SCE2.URL_id
WHERE SCE2.keyword ='Arsenal Football Club'
GROUP BY SCE1.keyword
ORDER BY count DESC
What is a better way to do that in PostgreSQL?

Wildcard operator ^ Not working as expected [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 28 days ago.
Improve this question
When I use:
SELECT *
FROM Customers1
WHERE email LIKE '%[^#]%'
There are no customers on the output at all, when there should be two on the output. Any ideas what might be causing this? I need to find customers email addresses which don't contain the '#'symbol.
I've tried running code:
SELECT *
FROM Customers1
WHERE email LIKE '%[#]%';
This shows 13 on the output.
I've also tried the '%[!#]%'; but this makes no difference.
In my understanding, you are trying to get all the email in a table that is not a valid mail address (by checking that email must have # symbol in it).
The way you are trying to achieve this will not work as this will fetch all data in the column except the null and blank (empty string) data.
[^] wildcard will check LIKE and PATINDEX in some special cases like.
A column has only single characters.
You are fetching a string where a specific position has not that value, etc.
For this scenario I will rather suggest, use a simple TSQL like.
SELECT *
FROM Customers1
WHERE email NOT LIKE '%#%';

Database table columns names [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have noticed that some people in database columns instead of for example id, user, addressare using something like h_id, e_user, f_address etc...
Is that some kind of security aspect? or maybe these are some shortcuts of words?
Its because there might be many id fields like user_id,category_id,that's why they use so that code is understandable.
And talking about columns name like f_address, they are just shortcut for say first address. It doesn't have anything to do with security but to increase the query readability use proper name to fields so that people can understand just by seeing column name what data it saves.
If there are fields like category_id and sub_category_id , it is understandable from the field name, but if i denote it using c_id and s_id, its hard to depict.
Well, 'User' is a security object in SQL Server, so using that is kind of scary. 'ID' and 'address' are way too generic to provide any semantics when used as attribute names.
If a purpose of design is to be maintainable and readable, then there some words that simply don't work.
Definitely not security related.
Some use it for readability or speed (you don't have to remember which table you gave a certain name->see following example) when writing queries.
i.e.
select a.name, b.name from table1 a join table2 b on a.id=b.id
Like this you have to remember that table1 is named a and table2 is named b etc.
But if you use tablename_field (which you can shorten by using only the first letter of the tablename). That way you never have duplicate fieldnames when creating join queries.

Overcoming the reserved word "IN" in sql server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Just for reference I am using SQL Azure.
I noticed when I am trying to select data from a table based on a license plate and the state of that plate I get no results back if the state is "IN". I realize the word "IN" is reserved in SQL server; however, I am containing that within quotes in my query. I currently am in testing phase and have only one record in the table which has a lisence plate 287YGB and state IN.
If I write my query as follows I get nothing back.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB' AND tblVehicles.PlateState = 'IN'
If I write my query this way I get back my result. But this is not good enough.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB'
And finally, if I write my query this way I get the only row in the table.
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles
From these tests I can see that the last where parameter is causing the problem. I am assuming it is due to the fact that the word "IN" is reserved. Is there a way around this?
Reserved words usually only cause problems if you're using them as field names, and in that case you need to wrap them with brackets ("[]") to eliminate the problem. I will amost guarantee you that your PlateState has some garbage in it, so you need to either trim it first (LTRIM(RTRIM(PlateState)) = 'IN') or use Like '%IN%' instead, and this will return the results you expect.
try this
SELECT MakeModel, CitizenID, VehicleID FROM tblVehicles WHERE tblVehicles.Lisence = '287YGB' AND LTRIM(RTRIM(tblVehicles.PlateState)) = 'IN'

Display endless amount of data in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If I have an endless amount of data, can I display all of it in sql?
I know there is obviously select *, but then it will never complete.
Is there a command for this?
You can use TOP to select subset of total records
SELECT TOP 100 * from table
This selects top 100 records.
By using Order By clause , you can specify the basis on which subset of records is returned.
Now if you are asking about limits of Sql Server database management system then please see this link - Maximum Capacity Specification of Sql Server
Eg
Max Databases per instance of SQL Server ( both 32 bit and 64 bit ) = 32,767
Usually, you will prefer to use some kind of paging, since you cannot actually show "endless amount of data" in user-friendly way on application.