SQL query that choose all values when no input - sql

I've write this query in an access 2007 database:
SELECT Ordini.ID, Ordini.Data, Clienti.Cognome, Clienti.Nome,
DettagliOrdine.IDProdotto,
Prodotti.Descrizione, Prodotti.Prezzo, DettagliOrdine.Quantità,
([Prezzo]*[Quantità]) AS Totale, Ordini.Note, Produttori.Nome, Ordini.Ordinato,
Ordini.Arrivato, Ordini.Ritirato
FROM Produttori
INNER JOIN (Prodotti
INNER JOIN ((Clienti INNER JOIN Ordini ON Clienti.ID = Ordini.IDCliente)
INNER JOIN DettagliOrdine ON Ordini.ID = DettagliOrdine.IDOrdine)
ON Prodotti.ID = DettagliOrdine.IDProdotto)
ON Produttori.ID = Prodotti.IDFornitore
WHERE (((Clienti.Cognome)=[Cognome: ])
AND ((Clienti.Nome)=[Nome: ])
AND (([Cognome: ]) Is Not Null))
ORDER BY Ordini.Data;
This query brings up an input box for the COGNOME and NOME field of research.
I need that if user write nothing (press ENTER) for that field research tag is . (all value of that field).
es. If user write a COGNOME but not a NOME (press ENTER when popup NOME window) research to be done on all the NOME with that "COGNOME".
How can I do this?

If I understand the question correctly, I think you want something like this ...
WHERE
(
Clienti.Cognome=[Cognome: ]
OR [Cognome: ] Is Null
)
AND
(
Clienti.Nome=[Nome: ]
OR [Nome: ] Is Null
)
When the parameter value is Null, no filtering is performed based on the corresponding field. But when the parameter value is not Null, it will be used to filter the result set so that it includes only rows with matching values.

For a query that is executed from with Access itself you can use the Nz() function along with the LIKE operator. For example, the query
PARAMETERS [LastName: ] TEXT(255);
SELECT ID, LastName, FirstName
FROM Clients
WHERE LastName LIKE Nz([LastName: ],"*")
will return all records* if enter nothing at the LastName: prompt, but will return only the records with the specified [LastName] if I type something in at the prompt.
*Actually, all records for which a LastName exists (i.e., IS NOT NULL). Thanks to HansUp for the correction.

Related

Dynamic where condition PostgreSQL

I am building a CRUD application that allows the user to input some search criteria and get the documents corresponding to those criteria. Unfortunately i have some difficulties in creating a query in postgres that uses different conditions in the where part, based on the input sent by the user.
For example if the user set as search criteria only the document number the query would be defined like this:
select * from document where document_num = "value1"
On the other hand if the user gave two criteria the query would be set up like this:
select * from document where document_num = "value1" and reg_date = "value2"
How can i set up a query that is valid for all the cases? Looking in other threads i saw as a possible solution using coalesce in the where part:
document_num = coalesce("value1", document_num)
The problem with this approach is that when no value is provided postgres converts the condition to document_num IS NOT NULL which is not what i need (my goal is to set the condition to always true).
Thanks in advance
So the solution by #D-shih will work if you have a default value and you can also use COALESCE as below.
SELECT *
FROM document
WHERE document_num = COALESCE("value1", default_value)
AND reg_date = COALESCE("value2", default_value);
If you don't have default values then you can create your query using CASE WHEN(here I am supposing you have some variables from which you will determine which conditions to apply like when to apply document_num or when to apply reg_date or when to apply both). Giving a little example below.
SELECT *
FROM document
WHERE
(
CASE
WHEN "value1" IS NOT NULL THEN document_num = "value1"
ELSE TRUE
END
)
AND (
CASE
WHEN "value2" IS NOT NULL THEN reg_date = "value2"
ELSE TRUE
END
)
You can read more how to use CASE WHEN here.
If I understand correctly, you can try to pass the user input value by parameter.
parameter default value might design that the user can't pass if the user didn't want to use the parameter it will use the default value.
we can use OR to judge whether use parameter otherwise ignores that.
SELECT *
FROM document
WHERE (document_num = :value1 OR :value1 = [default value])
AND (reg_date = :value2 OR :value2 = [default value])

where clause returns nothing

This is a very simple query:
select br.BranchCode, br.BranchName,cn.Country, cd.AggregateCreditExposure as [CreditExposure]
from Branches br join CreditDetails cd
on br.BranchCode = cd.BranchCode join Country cn
on br.CountryCode = cn.CountryCode
and it gives me the result set:
When I try to put where clause, it returns no result:
select br.BranchCode, br.BranchName,cn.Country, cd.AggregateCreditExposure as [CreditExposure]
from Branches br join CreditDetails cd
on br.BranchCode = cd.BranchCode join Country cn
on br.CountryCode = cn.CountryCode
where cn.Country = 'France'
Expected Result:
Strangely it returns nothing. Why doesn't it work??
Schema of Country: (providing only related table)
CountryCode int PK
Country varchar(50)
You've either got whitespace or some invisible characters in your data.
The way I always test for this in SSMS is to take the result of your first query, that does return data, click on the box in the grid that contains "France" (in this instance) go up to the query editor and paste (ctrl-v) in between two characters like ''. If there's some white space, you'll see something like this:
'France '
instead of this:
'France'
The only thing I can think of is you have a newline '\n' at the start of "France" which doesn't actually show up in "Select * from " > Try and use LIKE 'France', if that works, it has to be a newline character.
try
= '\nFrance'
or
= '\nFrance\n'
PS: i know this because i had this similar issue sometime back.

Bigquery - Substitute field if other field is blank

I've got this code here:
select DealID,ExternalReference,order_number, sales_rule
from flostream.orders
join mobileheads.surveys on flostream.orders.ExternalReference = mobileheads.surveys.order_number
//where DealID is null
What I want to happen is IF DealID (in flostream.orders) is null, replace it with sales_rule (in mobileheads.surveys)
Please let me know if this can be done with Bigquery or if you can think of some workaround?
Thanks!
Nik
You need
IFNULL(expr, null_default)
If expr is not null, returns expr, otherwise returns null_default.
You should read more about them in the manual.
Your query would look like:
SELECT IFNULL(DealID,sales_rule) as DealID,
ExternalReference,
order_number,
sales_rule
FROM flostream.orders
JOIN mobileheads.surveys ON flostream.orders.ExternalReference = mobileheads.surveys.order_number

How to get over error 40 "Access Denied -- You do not have access" in SugarCRM?

Here are the request params (url = http://sugarcrm.localhost/service/v4_1/rest.php):
method=get_entry_list&input_type=json&response_type=json
&rest_data {"session":"2q67jvlce802e4htsqc94oshkat9henvicvfclprhrbd8ef6k0o0",
"module_name":"Contacts",
"query":"email1=ychaouche#feeder.fr",
"order_by":"",
"offset":0,
"select_fields":[],
"link_name_to_fields_array":[],
"max_results":0,
"deleted":false}
I get this as a result :
"{"name":"Access Denied","number":40,"description":"You do not have access"}"
Edit
This error is fired whenever the subquery is malformed, not necessarily when one doesn't have access to a module, so one should be aware of it.
Well, one solution to my problem is to replace that query with this one :
contacts.id
IN
(
SELECT email_addr_bean_rel.bean_id
FROM email_addr_bean_rel
JOIN email_addresses
ON email_addr_bean_rel.email_address_id = email_addresses.id
WHERE
email_addresses.email_address = 'ychaouche#feeder.fr'
)
Since appearantly the email1 and email2 fields are not queriable (they're not in the Contacts table). The workaround consists of querying the email_address table via a subquery.
EDIT : so that the whole query should look something like this :
method=get_entry_list&input_type=json&response_type=json
&rest_data {"session":"2q67jvlce802e4htsqc94oshkat9henvicvfclprhrbd8ef6k0o0",
"module_name":"Contacts",
"query":
"
contacts.id
IN
(
SELECT email_addr_bean_rel.bean_id
FROM email_addr_bean_rel
JOIN email_addresses
ON email_addr_bean_rel.email_address_id = email_addresses.id
WHERE
email_addresses.email_address = 'ychaouche#feeder.fr'
)
"
"order_by":"",
"offset":0,
"select_fields":[],
"link_name_to_fields_array":[],
"max_results":0,
"deleted":false}

SQL query based on criteria

it's working if all the feilds is entered by user. i need a code that combine all the sql command. user may enter in the one field or two field or all the three fields. i need to search the database with one field query or two feild query or three feilds query.
i have try it with where help is a table & search-test is form & contract no,username & date of feild in database and forms.
where the help(table).cont_no(field) is equal or not equal to search-test(form name).cont_no(text box field)
SELECT *
FROM help
WHERE ( forms ! [search-test] ! cont_no = ''
OR help.cont_no = forms ! [search-test] ! cont_no )
AND ( forms ! [search-test] ! username = ''
OR help.username = forms ! [search-test] ! username )
AND ( forms ! [search-test] ! cbo_date = ''
OR help.DATE = forms ! [search-test] ! cbo_date );
I think what you mean is that you want a record included in the result set if the record fields contain a match to the parameters supplied. If a specific parameter is not supplied, then the corresponding field should always be considered a positive match.
That is, if no parameters were supplied, the entire table should be returned.
As for a solution, just a guess (I'm not an MS Access expert, is this for MS Access?), but can you use the iif function to force a match instead? Again, I'm not sure about the syntax, but the underlying logic should work.
Change your WHERE clause to
WHERE
(IIF(forms![search-test]!username is not null, forms![search-test]!username, help.cont_no) = help.cont_no
AND
(IIF(forms![search-test]!cont_no is not null, forms![search-test]!cont_no, help.username) = help.username
AND
(IIF(forms![search-test]!cbo_date is not null, forms![search-test]!cbo_date, help.dbo_date) = help.dbo_date