I have a query in Access and would like to know if it were possible to use the where not exists clause to display a specific text for each field when there are no returned rows.
Example query:
Select Field1, Field2, Field3
From TableA
Where Field1 = "test";
If there are no returned results I would like the following to return:
Field1 = "test"
Field2 = "not provided"
Field2 = "not provided"
How about:
SELECT Field1, Field2
FROM Table
WHERE ID=3
UNION ALL SELECT DISTINCT "None","None" FROM AnyTableithAtLeastOneRow
WHERE 3 NOT IN (SELECT ID FROM Table)
The usual way to do what you're asking is:
Select Field1, isnull(Field2, 'Not Provided'), isnull(Field3, 'Not Provided')
edit
whoops, you're using Access, in that case the equivalent function is "nz" (what?! :p)
Select Field1, nz(Field2, 'Not Provided'), nz(Field3, 'Not Provided')
Related
Say that you need to query some data and that there are three fields like the following (this is part of a larger query):
Field1, Field2, Field3.
So you select them like this:
SELECT Field1=MyTable.Field1, Field2=MyTable.Field2, Field3=MyTable.Field3
FROM MyTable
I need to compare these values and return the variable Result that is computed as follows:
0 if they are all the same
1/2 if two are the same and one is different
1 if they are all different.
How should I restructure my query? I think I need a subquery but I am not sure how to structure it.
You can use case:
select (case when field1 = field2 and field1 = field3 then 0
when field1 in (field2, field3) or field2 = field3 then 0.5
else 1
end) as result
I'm creating a stored procedure where I want to return a particular result set
I have 2 data sets that I'm trying to somehow join so that it only returns the outlier records (where there not a match). I've thought about using UNION and EXCEPT but it doesn't seem to work with this scenario. TO make this less complicated, i currently have two CTE in my proc (alternatively i can use #TempTables).
Query result 1. In the below result set, this query will return 3 fields. Field3 text value will always be the same here.
Field1 Field2 Field3
123 BAK 'Missing in Query 2'
234 HAO 'Missing in Query 2'
345 OPP 'Missing in Query 2'
Query result 2. Same deal here, Field3 will always have the same text value.
Field1 Field2 Field3
123 BAK 'Missing in Query 1'
234 HAO 'Missing in Query 1'
678 UTO 'Missing in Query 1'
DESIRED RESULT: The reason why these two are returned the first row (Field 345), is Missing in Query 2 and the 2nd row is missing in Query 1. I'm only looking for matches where Query1.Field1=Query2.Field1 and Query1.Field2 = Query2.Field2.
Field1 Field2 Field3
345 OPP 'Missing in Query 2' <- from Query 1
678 UTO 'Missing in Query 1' <- from Query 2
I've tried to use a FULL JOIN to do this, but FULL JOIN returns additional 3 columns with NULL values. I'm trying to avoid that and only display the result as shown above. Any help would be appreciated.
I think you want the rows that are not in both tables. One method is:
select qr1.*
from qr1
where not exists (select 1 from qr2 where qr2.field1 = qr1.field1 and qr2.field2 = qr1.field2)
union all
select qr2.*
from qr2
where not exists (select 1 from qr1 where qr1.field1 = qr2.field1 and qr1.field2 = qr2.field2);
You can use full outer join :
select coalesce(qr1.field1, qr2.field1) as field1,
coalesce(qr1.field2, qr2.field2) as field2,
(case when qr1.field1 is null
then 'Missing in Query 1'
else 'Missing in Query 2'
end) as Field3
from qr1 full outer join
qr2
on qr1.field1 = qr2.field1 and qr1.field2 = qr2.field2
where (qr1.field1 is null or qr2.field2 is null);
Hello I have a query where I want to select the value of one of two fields depending if one is empty.
field1 and field2
I want to select them as complete_field
IF field1 is empty, then complete_field is field2
ELSE complete_field is field1
In php it would be done like:
$complete_field = $field1 == '' ? $field2 : $field1;
How would I do this in PostgreSQL?
I tried:
SELECT
(IF field1 = '' THEN field2 ELSE field1) AS complete_field
FROM
table
But it doesnt work.
Please help me :) Thanks
Use CASE WHEN .. THEN .. ELSE .. END, e.g.:
SELECT
(CASE WHEN (field1 IS NULL OR field1 = '') THEN field2 ELSE field1 END)
FROM table;
Check it out at the official docs.
Try COALESCE with NULLIF:
SELECT COALESCE(NULLIF(field1, ''), field2) AS the_field FROM my_table;
The ANSI SQL function Coalesce() is what you want.
select Coalesce(field1,field2)
from table;
I want to be able to compare 2 records in the same SQL table and tell if they are different. I do not need to tell what is different, just that they are different.
Also, I only need to compare 7 of 10 columns in the records. ie.) each record has 10 columns but I only care about 7 of these columns.
Can this be done through SQL or should I get the records in C# and hash them to see if they are different values?
You can write a group by query like this:
SELECT field1, field2, field3, .... field7, COUNT(*)
FROM table
[WHERE primary_key = key1 OR primary_key = key2]
GROUP BY field1, field2, field3, .... field7
HAVING COUNT(*) > 1
That way you get all records with same values for field 1 to 7, along with the number of occurrences.
Add the part between brackets to limit your search for duplicates, either with OR, or with IN (...).
IF EXISTS (SELECT Col1, Col2, ColEtc...
from MyTable
where condition1
EXCEPT SELECT Col1, Col2, ColEtc...
from MyTable
where condition2)
BEGIN
-- Query returns all rows from first set that are not column for column
-- also in the second (EXCEPT) set. So if there are any, there will be
-- rows returned, which meets the EXISTS criteria. Since you're only
-- checking EXISTS, SQL doesn't actually need to return columns.
END
No hash is necessary. Normal equality comparison is enough:
select isEqual = case when t1.a <> t2.a or t1.b <> t2.b bbb then 1 else 0 end
SELECT
CASE WHEN (a.column1, a.column2, ..., a.column7)
= (b.column1, b.column2, ..., b.column7)
THEN 'all 7 columns same'
ELSE 'one or more of the 7 columns differ'
END AS result
FROM tableX AS a
JOIN tableX AS b
ON t1.PK = #PK_of_row_one
AND t2.PK = #PK_of_row_two
Can't you just use the DISTINCT keyword? All duplicates will not be returned, so each row you receive is unique (and different from the others).
http://www.mysqlfaqs.net/mysql-faqs/SQL-Statements/Select-Statement/How-does-DISTINCT-work-in-MySQL
So you could make this query:
SELECT DISTINCT x,y,z FROM RandomTable WHERE x = something
Which will only return one row for each unique x,y,z combination.
Is it possible in an SQL query to only show a field if another field has data? For example, if Field1 <> '', then show the value in Field2 else don't show the value?
It can be done using a case statement. (At least in SQL Server)
select case when Field1 <> ''
then Field2
end as Field2
from YourTable
Sure (this works in Oracle and SQLite):
select
field1,
(case
when field1 is null then null
else field2
end) field2_wrapped
from my_table
if 'has no data' means the empty string (''), you need to use this statement:
SELECT Filed2 FROM Table1 WHERE Filed1<>''
If 'no data' means NULL value, you need use
SELECT Filed2 FROM Table1 WHERE NOT (Filed2 IS NULL)
Take a look at Standard SQL functions COALESCE() and NULLIF():
COALESCE(NULLIF(Field1, ''), Field2)