SQL Server 2008 AND/OR operators and parentheses - sql

I am having a bit of trouble with AND/OR operators. It's easy stuff, but I'm having trouble with the following scenario:
WHERE (ID = '111') OR (charge_desc LIKE '%garn%') OR
(charge_desc LIKE '%levy%') OR
(charge_desc LIKE '%exe%')
Basically, I am trying to find the keywords based only on ID 111. However, what's happening with the current query is that it looks for those keywords based on a bunch of different IDs instead. I've tried to manipulate the parenthesis, and I see the issue. Because it can't be "111 OR LIKE XYZ..." but it can't be AND either because if you do AND, then it looks for that ID based on EXACTLY those 3 criteria.
I am looking to do ID 111 and look for garn or levy or exe - not "AND" but it should be every variation that comes up but ONLY for 111.
I hope this makes sense. I feel its something with the parenthesis.
I just tried this:
WHERE ID = 111
HAVING garn or levy or exe
Forgive my lack of syntax there. :)

If you absolutely must have id = 111 then this is an and relationship, not an or relationship. You can then have a series of or operators between the other conditions:
WHERE ID = '111' AND
(charge_desc LIKE '%garn%' OR
charge_desc LIKE '%levy%' OR
charge_desc LIKE '%exe%')

The problem is with the parenthesis.
Let's look at this scenario:
WHERE (ID = '111') OR (charge_desc LIKE '%garn%') OR
(charge_desc LIKE '%levy%') OR
(charge_desc LIKE '%exe%')
You are saying where ID = 111 OR Charg_Desc like garn...etc.
WHERE (ID = '111') AND
(charge_desc LIKE '%garn%' OR
charge_desc LIKE '%levy%' OR
charge_desc LIKE '%exe%')
Now, you're saying WHERE ID = 111 AND the following conditional OR is applied to bring in the additional match. Remember, the match is in addition to the ID match.

Related

Microsoft SQL Server generates two select queries and puts data in separate columns

I am looking for a query that separate the data with the condition WHERE in the same output but in separates columns.
Example: I have the table Product_2:
I have two separates queries (to separate the products by Produt_Tag):
SELECT
Product_Mark AS "PIT-10_Product_Mark",
Product_Model AS "PIT-10_Product_Model"
FROM Product_2
WHERE Product_Tag = 'PIT-10';
SELECT
Product_Mark AS "PIT-11_Product_Mark",
Product_Model AS "PIT-11_Product_Model"
FROM Product_2
WHERE Product_Tag = 'PIT-11';
And I get this output:
But I need the output to be like this:
Can someone tell me how I need to modify my query to have the four columns in the same table/ output?
Thank you
I forgot to tell that in the data I Have the “Porduct_Mark” that only appears one time. (in reality the data in “Product_Mark” is the name of the place where the instrument is located and one place can have one or two instruments “Product_Model”. At the end I’m looking for the result show in the image here below. I tried to use LEFT JOIN but that don’t work.
here is the new table "Product_2"
Result that I'm looking for:
Luis Ardila
I am assuming Product_PK is the primary key for the table and the repeated value 1002 shown in the question is a mistake. Considering this assumption, you can get the result set using self join as below.
SELECT pa.Product_Mark AS "PIT-10_Product_Mark", pa.Product_Model AS "PIT-10_Product_Model",
pb.Product_Mark AS "PIT-11_Product_Mark", pb.Product_Model AS "PIT-11_Product_Model"
FROM Product_2 pa
INNER JOIN Product_2 pb
ON pa.Product_Mark = pb.Product_Mark
WHERE pa.product_pk != pb.product_pk
and pa.Product_Tag = 'PIT-10'
and pb.Product_Tag = 'PIT-11';
verified same in https://dbfiddle.uk/NiOO8zc1

Trouble getting information from DB, loosing information while using LIKE function SQL

I am new to coding and i really need some help if u can have an idea from you guys. I am working on a WP Plugin which is basically an admin interface. I create a table based on my BD which contains differents information but I have some trouble with some sql request.
I have a lot of tables, I needed to combine them to get what I wanted to get something like this first :
license key
license ID
meta_value
def5002313131
1
some text
def5056462131
1
some text
def5064654631
1
some text
def5646162131
2
some text
def5541541531
3
some text
using the following code :
SELECT SUBSTRING(W551je5v_lmfwc_licenses.license_key,1,15), W551je5v_lmfwc_licenses.user_id, W551je5v_posts.post_author, W551je5v_postmeta.meta_value FROM W551je5v_lmfwc_licenses
LEFT JOIN W551je5v_users ON W551je5v_lmfwc_licenses.user_id = W551je5v_users.ID
LEFT JOIN W551je5v_posts ON W551je5v_lmfwc_licenses.user_id = W551je5v_posts.post_author
LEFT JOIN W551je5v_postmeta ON W551je5v_postmeta.post_id = W551je5v_posts.ID
WHERE W551je5v_lmfwc_licenses.user_id IS NOT NULL AND W551je5v_users.ID IS NOT NULL
GROUP BY W551je5v_lmfwc_licenses.license_key
ORDER BY user_id;
getting meta_value from postmeta, every license information from license but i had to pass by the 2 others table to get to postmeta.
By grouping by license_key, I am only getting the first line of meta_value corresponding to the license_id.
In those meta_value, there is a mac adress that i have to get (that's the final point).
So i used LIKE function to get meta_value which had meta_key equal to 'mac' (meta_key is an other col in the postmeta table) W551je5v_postmeta.meta_key LIKE 'mac'. But when I do it, I loose some of IDs cause they didn't have a mac adress and I still want to get them to create a table like this
license key
license ID
meta_value
def5002313131
1
mac adress
def5056462131
1
mac adress
def5064654631
1
mac adress
def5646162131
2
NULL
def5541541531
3
mac adress
I couldn't found any solution cause LIKE is only keeping the lines with an mac adress.
I tried also to GROUP_CONCAT every meta_value information and using REGEX to get the mac adress, but GROUP_CONCAT is limited in number of character and I am over the limit. I can't found how to use the SET ##global.group_concat_max_len = 1000000 on my website, if you have any ideas of how I can do it without using GROUP CONCAT or LIKE but something that is doing the same with keeping every lines, I would appreciate a lot.
Sorry for my bad english, and if I am not very clear, but I tried to explain it as much I as can, ask if u need more information.

Multiple Table Access Query

I am trying to combine multiple tables in Microsoft Access to return a LIKE wildcard value. However, I want it done by a list of things, not just a specific item. I have the below table labled "SomeData"
ID Fund TextFld Type1
2 Sewer COMP - DZ Docs
3 Sewer SEW: ZO Docs
I have a second table labeled GL where in I am looking up on TextFld: For instance there might be:
COMP - ABC
COMP - DEF
SEW: GHS
SEW: XYZ
I put into SQL:
SELECT GL.Text, SomeData.Type1
FROM SomeData INNER JOIN GL ON SomeData.Fund = GL.Type
WHERE GL.Text LIKE SomeData.TextFld;
However, this is only returning exact matches. How can I do this so that any similar match will be found.
Please note that there will be many lines of these, so I cannot just use anything such as LIKE "COMP*" OR "SEW*"
Thank you very much in advance. Any help would be greatly appreciated.
You don't have the wildcard in your query. The wildcard in MS Access is the asterisk so your query should look like this:
SELECT GL.Text, SomeData.Type1
FROM SomeData INNER JOIN GL ON SomeData.Fund = GL.Type
WHERE GL.Text LIKE '*' & SomeData.TextFld & '*';
I haven't ever used a LIKE statement while using a field from another table but I'm fairly certain that syntax is correct.
Note: In this query you are getting any records where the COMP or SEW exists anywhere in the field. If you want it to show only when it is found at the start of the field it would be:
WHERE GL.Text LIKE & SomeData.TextFld & '*';
And likewise if you want it to only match where it is found at the end:
WHERE GL.Text LIKE '*' & SomeData.TextFld;
You should concatenate wild character at the end (or beginning, or both) of the field TextFld
WHERE GL.Text LIKE '%' + SomeData.TextFld + '%'

SQL - Getting a column from another table to join this query

I've got the code below which displays the location_id and total number of antisocial crimes but I would like to get the location_name from a different table called location_dim be output as well. I tried to find a way to UNION it but couldn't get it to work. Any ideas?
SELECT fk5_location_id , COUNT(fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
from CRIME_FACT
WHERE fk1_time_id = 3 AND fk3_crime_id = 1
GROUP BY fk5_location_id;
You want to use join to lookup the location name. The query would probably look like this:
SELECT ld.location_name, COUNT(cf.fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
from CRIME_FACT cf join
LOCATION_DIM ld
on cf.fk5_location_id = ld.location_id
WHERE cf.fk1_time_id = 3 AND cf.fk3_crime_id = 1
GROUP BY ld.location_name;
You need to put in the right column names for ld.location_name and ld.location_id.
you need to find a relationship between the two tables to link a location to crime. that way you could use a "join" and select the fields from each table you are interested in.
I suggest taking a step back and reading up on the fundamentals of relational databases. There are many good books out there which is the perfect place to start.

One result from join between 2 tables?

It is a bit complicated to explain into the main caption, but the situation is as follow :
- I have 2 tables :
Users, Preferences.
defined as follow
Users :
ID | NAME | FK_NODE_PREFERENCES
Preferences :
ID | FK_USERS | PREFERENCE_DESCRIPTION
the idea is to have alot of preferences for each user...
the results shall look as follow :
USER | ALL_PREFERENCES
I need to search by a part of the preference string and i need to have only 1 row into the result select query, which having all the preferences related ot the user as a text into a single record ?
I'm not familiar with Firebird, but it seems to have a LIST() function which should be an equivalent to the GROUP_CONCAT() function in MySQL. (http://www.firebirdsql.org/refdocs/langrefupd21-aggrfunc-list.html)
So, basically the query should look something like this:
SELECT Users.name, LIST(Preferences.preference_description, ' ') AS ALL_PREFERENCES FROM Users JOIN NodePreferences ON Users.fk_node_preferences = Preferences.id WHERE Preferences.preference_description LIKE '%abc%' GROUP BY Users.name
So... not sure if this actually works, but the direction should be the right one...
Hope that helps!