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
Related
I have to SELECT data from a table using REPLICATE function in such a way that if the Field 4 has Numeric data then in the select statement that data should appear in 10th column. If the Field 4 is not numeric then that value should appear in 20th column and the 10th column should be blank.
The table has data as follows:
Field1 field2 Field3 Field4
1 a b 205
2 s t A25
Any advice on how to do this please.
Why are you using REPLICATE for this? Check if the value for Field4 is numeric, and if so add in Field10, if not then add in Field20.
SELECT Field1, Field2, Field3, Field4,
/*... other columns... */
CASE WHEN isnumeric(Field4) THEN Field4 ELSE null END AS Field10,
/*... other columns */
CASE WHEN isnumeric(Field4) THEN NULL ELSE Field4 END as Field20
FROM myTable
WHERE ....
I have a table with 3 fields, let us say field1, field2, and field3. The value in field2 is either 0 or 1.
I am trying to fetch field1 in a way to always have all the rows where the value is 1 in field2 displayed first and arranged by field3, and then display the rest of data, also arranged by field3.
My research told me one can order by two fields, let us say 'Order by field2 Desc, field 3' but this really is not giving the expected result. Any idea?
One small change is required for MySQL:
ORDER BY field2 = 1 DESC, field3
Or for standard SQL:
ORDER BY CASE WHEN field2 = 1 THEN 1 ELSE 0 END DESC, field3
try this:
order by
case when field2=1 then 0 else 1 end,
field3
If I insert the following values into my table
Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 1, 100, 5, "5/10/2012")
Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 2, 100, 99, "5/10/2012")
Insert into Table1 (Field1, Field2, Field3, Field4, DateField) values (1, 3, 100, 3, "5/10/2012")
What my query is going to try and get is the value from Field4 with the maximum value in Field2 by a certain date.
Example
select isnull(Field4,0) from Table1
where Field1 = 1 and Field3 = 100 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2
Which works great. I get 3 which is expected. Now this is where my questions comes from. It is possible Field3 could have other values, such as 110. When I run that query
select isnull(Field4,0) from Table1
where Field1 = 1 and Field3 = 110 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2
I don't get any results. It should be null, and the isnull(Field4,0) should then spit out 0. But it doesn't. I've tried replacing the selection with count(*) to see if it returns 0, but it doesn't return anything. I'm at a loss. I need it to return 0 as this will be going into a temp table and then summed up with values from another table. Thanks.
Edit - New question part
I understand that I may have been using the isnull for the wrong thing here. Which I can accept. However, if I wanted to write a case statement to handle nothing being returned, it doesn't return 0 if no rows are returned.
select count(*) from Table1
where Field1 = 1 and Field3 = 110 and datediff(day,DateField,"5/21/2012") > 0
having Max(Field2) = Field2
The above code doesn't return anything, instead of 0 like I would think it would.
You do not get any results because none of the 3 records you inserted has 110 for Field3. As a result, no rows are returned from the query. Your ISNULL would only be used if instead of 5, 99, or 3, those values were NULL for a row returned.
If this record was in your table:
INSERT INTO Table1 (Field1, Field2, Field3, Field4, DateField)
VALUES(1, 3, 110, NULL, "5/10/2012")
This record would match your Field3 = 110 requirement, and then have Field4 Null, being set to 0 by your SELECT logic, but since it is not, nothing will be returned by your query.
Part 2
It appears that the HAVING is removing the 0 record from the result set since no records are left after the WHERE clause to match against the HAVING criteria.
If you want to see whether any results match your criteria, you can use the EXISTS clause
IF EXISTS(
SELECT COUNT(*)
FROM Table1 AS t
WHERE t.Field1 = 1
AND t.Field3 = 110
AND DATEDIFF(day, t.DateField, "5/21/2012") > 0
HAVING MAX(Field2) = Field2
)
BEGIN
--Code for when the record Exists
END
ELSE
BEGIN
--No records logic here
END
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.
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')