SQL Conditional Inner Join Where - mysql

I need to a conditional inner join with where.
I have an assessment form that has three particular fields: country, Degree and subject.
There is another form named "responds template" with the same fields: country, Degree and subject.
Advisers create some responds template by different condition to answer faster.
Advisers reply to the assessment inquiries in a reply form then I need to filter list of related responds as a drop down list base on user section in those filed.
for example if a user chose Country: USA, Degree: Master and subject: Math then I need to filter responds template based on these section and show only the responds templates for Country: USA, Degree: Master and subject: Math.
I used this code but it works just for the first condition "country" and doesn't work for second and third AND.
and also I need to filter by country and degree if there is not any item matched with first condition.
WHERE {thistable}.id IN (
SELECT app_responds.id
FROM app_responds AS app_responds
INNER JOIN app_assessment AS app_assessment
ON app_responds.destination = app_assessment.destination_country AND
app_responds.degree = app_assessment.degree AND
app_responds.field_of_study = app_assessment.field_of_study_id AND
app_responds.subject = app_assessment.subject_id
)

Did you try to use OR instead of AND. That gives you any match from your conditions. If you use AND all tree conditions should be TRUE in order to get a value

Related

Match domain and extension of two email addresses in MySQL?

I have two tables with email addresses. Some emails have sub-domains, and others don't. I need to LEFT OUTER JOIN these tables based on the domain name and extension, while ignoring any sub-domains and anything left of the # symbol.
Contacts.Contact
Managers.Manager
bob#api.ibm.com
sarah#ibm.com
jim#sales.ibm.com
jane#mgt.yahoo.ca
joe#ibm.com
fred#google.com
zoe#yahoo.com
sam#facebook.co.uk
elf#api.yahoo.ca
frank#yahoo.ca
jack#oracle.com
SELECT Contact.Contact,
Managers.Manager
FROM Contact
LEFT OUTER JOIN Manager ON ???
should yield the following results:
Field: Contact
Field: Manager
bob#api.ibm.com
sarah#ibm.com
jim#sales.ibm.com
sarah#ibm.com
joe#ibm.com
sarah#ibm.com
zoe#yahoo.com
elf#api.yahoo.ca
jane#mgt.yahoo.ca
frank#yahoo.ca
jane#mgt.yahoo.ca
jack#oracle.com
The ibm.com and yahoo.ca emails were matched. So basically, the pattern is: IGNORE#IF_THIS_EXISTS_THEN_IGNORE.MATCH.MATCH Is this possible? If it is, then what is the correct SELECT statement to yield these results? Thanks.
To extract the (at most) two domain parts on the right, just do:
substring_index(substring_index(email,'#',-1),'.',-2)
Apply that to both Contact and Manager and join on the results being equal.

BO XI 3.1 List of values prompt item description return item key

I'm trying to define a LOV with Universe Designer that when used in a #Prompt shows the user the item description, but gives back the corresponding item key as result of user selection.
e.g. From a dimension table of countries with two columns as COUNTRY_ISO and COUNTRY_NAME, I would like to show COUNTRY_NAME values to user with #prompt, but get back the corresponding COUNTRY_ISO as return value of #prompt.
This is possible using Index Awareness. I'll describe the solution using the sample "Island Resorts Marketing" universe, but the concept should map to your specific need.
For this example, we'll create a prompt on the "Country" object in the "Resort" class. The prompt will display the country names, but the condition will apply to the country_id field.
First, we need to identify the keys. On the Keys tab of the Resort\Country object, add a new Primary Key using Resort_Country.country_id as the source:
(in your case, you'll do this on the COUNTRY_NAME object, using COUNTRY_ISO as the primary key)
Next, we create the predefined condition that will include the prompt. Create a new Predefined Condition, named "Select country", with the following definition:
Resort_Country.country_id = #Prompt('Choose a country','A','Resort\Country',Mono,primary_key)
What we're doing is is applying the condition to the ID field (resort_country.country_id), but using the country name (the Country object) as the source. Note the "primary_key" parameter -- this tells BO to use the PK from the referenced object.
Now to test. Create a WebI report and select the "Select country" filter along with a field to display. I chose Service Line:
I run the report and am presented with a list of country names. I choose France, and I get a list of the Service Lines associated with France. The SQL generated by this query, reflecting my prompt selection, is:
SELECT
Service_Line.service_line
FROM
Service_Line,
Country Resort_Country,
Resort
WHERE
( Resort_Country.country_id=Resort.country_id )
AND ( Resort.resort_id=Service_Line.resort_id )
AND ( Resort_Country.country_id = 2 )
Note the very last line, which shows that the condition is applied using the ID, even though I selected the country name "France".
References:
- XI3.1 Designer Guide #Prompt info, starting on page 520
- Dave's blog on Index Awareness
- Dave's blog on Index Awareness with prompts

Select ALL in SSRS

I have a table based on three drop down lists. The first is a client list, the second is a date list and the third is a stage list. The first two are single value lists. I can only allow the user to select 1 from each of those lists. The third list is a stage list. This contains the values First,Final and Release. My client has come back to me and asked if I can supply them with the ability to select all of the stages as an option. Here is the query as I have it right now. I have tried using (AnnotationDate IN (#Stage)) in place of (AnnotationDate = #Stage) but was unsuccessful. Can anyone give me a helpful hint?
SELECT AdDate, Page_ID, Status, AnnotationNumber, AnnotationBy, [Role Description], AnnotationDate, AnnotationType, BusinessUnit, ActualAgencyError, ErrorType,
AnnotationComments, TeamComments, sgkComments, PA, Client, Activity, Support, Name, BusImpact
FROM vwAgencyErrorOpen
WHERE (Client = #Client) AND (AdDate = #Job) AND (AnnotationDate = #Stage)
ORDER BY Page_ID
Change your #Stage Parameter to be multi-seletion.
Then remove the (AnnotationDate = #Stage) clause from the query.
And then set up a filter on your dataset as below:
You can then select all of your options.

MySQL with two Joins and a Where clause

I have 3 tables I want to join.
OFFICE contains address/contact details for an office.
CRIMECAT contains categories of crime law that an office may deal with and is related to the OFFICE table via the 'f_id'
CIVILCAT contains categories of civil law that an office may deal with and is related to the OFFICE table via the 'f_id' as well.
An office may deal with categories of crime law, civil law, both or none.
The user puts in a location and also decides via a range of checkboxes which areas of law they're interested in before hitting search. This should then return a list of the addresses of offices that deal with any of the checked categories in that location.
This works perfectly for either a crime category or a civil category, but as soon as one or more of each is selected the query returns zero results.
The working style of query is as follows:
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
JOIN crimecat CR ON OF.f_id=CR.f_id
WHERE OF.id ='3946' AND CR.cat = 'crm'
The query I'm banging my head against a brick wall on is:
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
JOIN crimecat CR ON OF.f_id=CR.f_id
JOIN civilcat CI ON OF.f_id=CI.f_id
WHERE OF.id ='3946' AND ((CR.cat = 'crm') OR (CI.cat = 'aap'))
I've also tried using a variant of the WHERE clause which also returns zero:
WHERE (OF.id ='3946' AND CR.cat = 'crm') OR (OF.id ='3946' AND CI.cat = 'aap')
I'm beginning to think the issue is with the JOIN(s) rather than the WHERE clause but can't think of a better way of writing them.
something like this may help. i suspect you are looking for either , but you are requesting both
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
LEFT JOIN crimecat CR ON OF.f_id=CR.f_id
LEFT JOIN civilcat CI ON OF.f_id=CI.f_id
WHERE OF.id ='3946' AND ((CR.cat = 'crm') OR (CI.cat = 'aap'))

MySQL Join not Producing Desired Results

I'm working on writing a snippet for ModX that will find all document with the specified TV set to a user submitted value.
Here is a description of the tables I'm working with.
http://wiki.modxcms.com/index.php/Template_Variable_Database_Tables
Here is my query:
SELECT contentid
FROM prefix_site_tmplvar_contentvalues
JOIN prefix_site_tmplvar_contentvalues
ON prefix_site_tmplvars.id = prefix_site_tmplvar_contentvalues.tmplvarid
WHERE value="Red"
Currently it's producing results such as this:
http://pastebin.com/mEJ1w2be
Where each document ID will have a new row in the results for each Template Variable. So, for 7455 in the example there will be one array for the color="red" one for material="wood" one for size="small". Which, makes it difficult if I want to find a product that is red, small, and made of wood.
Is there a way that I could join these tables so that I could get one row per product with the document id and a set of template variable with associate values—not all broken up?
try
GROUP BY contentid
this will smush all the rows with the same contentid together.