Multiple WHERE conditions using AND => No query result - mysql

I have a table with a few thousand entries. And My purpose is to select all entries from all versions that correspond to a given one. And the resulted entries must correspond exactly to the given entry.
But somehow, the SQL query does not work. The original project uses Access 2007. But I have tried also in MySQL and no success
I put here the sql query, but I also made a SQL fiddle:
SELECT
idvalue,
idtag,
iddevice,
idversion,
idtext,
description,
idaccess,
defaultvalue,
minimumvalue,
acceptedvalue,
maximumvalue,
outofrangevalue,
iddatatypepn,
iddatatypeopc,
size,
idresolution,
idunit,
idaccuracy,
enumerationvalues,
comments,
remanentvolatile,
storedatpn,
storedatmain,
`generated`,
edittime
FROM
SomeValues
WHERE
idtag = 2 AND iddevice = 1
AND idtext = 433
AND description = 'Input voltage (AC)'
AND idaccess = 12
AND defaultvalue IS NULL
AND minimumvalue =0
AND acceptedvalue = 5300
AND maximumvalue = 10050
AND outofrangevalue = 11000
AND iddatatypepn = 2
AND iddatatypeopc = 19
AND size = 2
AND idresolution = 2
AND idunit = 1
AND idaccuracy = 2
AND enumerationvalues IS NULL
AND comments IS NULL
AND remanentvolatile IS NULL
AND storedatpn = FALSE
AND storedatmain = FALSE
AND `generated` = TRUE
Fiddle: here
Can you please explain what is wrong with the sql query?
The result should be those 3 entries from the fiddle table.
And yes, I must use all the conditions from the "Where" clause, since the entries can match 90% but also have small differences

You have problem in line:
AND description = 'Input voltage (AC)'
change it to:
AND description = '"Input voltage (AC)"'
and everything will works.
Problem lies in the fact that you searched for text Input voltage (AC) instead of "Input voltage (AC)" (how is stated in column description).

Related

mysql group by and ratio between two sums

I have a query which sums field 'value' from rows with foreign key value V1 and divides that sum with sum of the field 'value' from rows with foreign key value V2. Somehow I am getting wrong results. What am I doing wrong here?
SELECT
iv1.year,
1,
iv1.country_code,
SUM(iv1.value) / SUM(iv2.value) AS value, # Formula
'Added using stored procedure for creation of 1 indicator as ratio of 2 over 6 indicators' AS additional_info
FROM
indicators_values iv1
JOIN indicators i1 ON (i1.no_of_indicator IN('B1.1.2.3', 'B1.2.2.2')
AND iv1.indicator_id = i1.indicator_id
AND i1.extracted_from_file_type = 'consolidated-a')
JOIN indicators_values iv2 ON (iv2.country_code = iv1.country_code AND iv2.year = iv1.year)
JOIN indicators i2 ON (i2.no_of_indicator IN('B1.1.2.2', 'B1.1.2.3', 'B1.1.2.4', 'B1.2.2.1', 'B1.2.2.2', 'B1.2.2.3')
AND iv2.indicator_id = i2.indicator_id
AND i2.extracted_from_file_type = 'consolidated-a')
WHERE
1=1
AND iv1.country_code = 'AT'
AND i1.no_of_indicator IN('B1.1.2.3', 'B1.2.2.2')
AND i2.no_of_indicator IN('B1.1.2.2', 'B1.1.2.3', 'B1.1.2.4', 'B1.2.2.1', 'B1.2.2.2', 'B1.2.2.3')
GROUP BY
iv1.year, iv1.country_code;
I know that I am getting wrong results, because most of the results that I am getting are bigger than 1, and they should all be smaller than 1, since I should have in the denominator same values contained in the numerator, plus some additional ones. Please advise? Thanks!
Schematically (too lazy to understand your conditions - maybe something is missing or changed places):
SELECT iv.year,
iv.country_code,
( SUM(CASE WHEN i.no_of_indicator IN('B1.1.2.3', 'B1.2.2.2')
THEN iv.value
END)
/
SUM(CASE WHEN i.no_of_indicator IN('B1.1.2.2', 'B1.1.2.3', 'B1.1.2.4', 'B1.2.2.1', 'B1.2.2.2', 'B1.2.2.3')
THEN iv.value
END
) AS value
FROM indicators_values iv
JOIN indicators i ON iv.indicator_id = i.indicator_id
WHERE i.extracted_from_file_type = 'consolidated-a'
AND iv.country_code = 'AT'
GROUP BY iv.year, iv.country_code
PS. Divisor may formally be zero - take care not to get a division by zero error.

Update database record by selected id and reset the initial record

Please how do i update all record to 0 and set only the selected ID to 1
UPDATE address SET default_addr = 1
WHERE addr_id = 100 AND user = 'peter'
The above query will update the selected address to 1 which is good, but i want to set other address or the old selected default to 0 with one query
In MySQL, you can do:
UPDATE address
SET default_addr = (addr_id = 100 AND user = 'peter');
(This shorthand uses the fact that MySQL treats booleans as numbers in a numeric context, with "0" for false and "1" for true.)
If you want only one default address for the user named Peter, then use a where:
UPDATE address
SET default_addr = (addr_id = 100)
WHERE user = 'peter';
I suspect this is the logic that you really want.
use a conditional update using case statement
update address set default_address = case when addr_id = 100 and user = 'peter' then 1 else 0 end
here is a functional example
I built a sample schema. These are often helpful to provide in your future questions.

SQL BINARY Operator not showing exact match

I have a table with the following columns...
[Name] = [Transliteration] = [Hexadecimal] = [HexadecimalUTF8]
...with multiple rows of UTF-8 characters, such as:
ङ = ṅa = 0919 = e0a499
ञ = ña = 091e = e0a49e
ण = ṇa = 0923 = e0a4a3
न = na = 0928 = e0a4a8
In order to search for the row that exactly matches ña in the Transliteration column , I enter the following command:
SELECT DISTINCT * FROM Samskrta
WHERE BINARY (Transliteration = concat(0xc3b161))
ORDER BY HexadecimalUTF8;
...which produces 4 rows.
Why is the SQL command not producing only the row that exactly matches ña?
What SQL command produces only the row that exactly matches ña?
The following command produces the same results:
SELECT DISTINCT * FROM Samskrta
WHERE BINARY (Transliteration = 'ña')
ORDER BY HexadecimalUTF8;
FIRST OF ALL, your query can't work as indicated: you are applying BINARY() to the result of the logical comparison, NOT comparing the BINARY() of whatever to whatever.
Try reproducing your code PRECISELY if you expect people to be able to tender assistance.

Stymied building this cross-database query

I'm trying to migrate some data from an old database to a new one with a slightly different schema and my SQL isn't terribly strong.
Old schema: There is a table we'll call "Person" with a field which can have set of permutations of 3 flags. The Person table has a foreign key to another table we'll call "Flags". They Flags table has rows for each of these flag combinations in a String:
1 - Yes No No
2 - Yes Yes No
3 - Yes No Yes
4 - Yes Yes Yes
5 - No Yes No
6 - No Yes Yes
7 - No No Yes
The new schema doesn't require this table (thankfully). These flags are simply fields in the "Person" table now as BIT fields.
What I want to do is something like a:
UPDATE database2.Person SET (flag1, flag2, flag3) VALUES (true, false false) WHERE database1.Person.flag_id = 1;
I could then run 7 different queries changing the IDs and the values accordingly. The problem, of course, is that the above isn't correct SQL. I think I need some kind of JOIN ...or a subselect in the where clause or something?
Stumped on the best way forward. My parting thought here is that this doesn't need to be compressed into a single query, or particularly elegant. I expect to run this query once and be done with it.
You could try something like:
update database2.Person p2 join database1.Person p1 on p1.PersonId = p2.PersonId
set flag1 = case when p1.Flag_id in (1,2,3,4) then true else false end case,
flag2 = case when p1.Flag_id in (2,4,5,6) then true else false end case,
flag3 = case when p1.Flag_id in (3,4,6,7) then true else false end case
(edited for mySQl syntax)
This should work if I'm understanding your question. It assumes you have a Person_Id matching in each table.
UPDATE db2.Person p
JOIN db1.Person p2 ON p.Person_Id = p2.Person_Id
SET p.Flag1 = 1,
p.Flag2 = 0,
p.Flag3 = 0
WHERE p2.Flag_Id = 1;
If the flag values in the Flags table were columns, you could easily run a single query, but as I'm understanding it, it's just a string field. This is an example:
UPDATE db2.Person p
JOIN db1.Person p2 ON p.Person_Id = p2.Person_Id
JOIN db1.Flags f ON p2.Flag_Id = f.Flag_Id
SET p.Flag1 = CASE WHEN f.Flag1 = 'Yes' THEN 1 ELSE 0 END,
p.Flag2 = CASE WHEN f.Flag2 = 'Yes' THEN 1 ELSE 0 END,
p.Flag3 = CASE WHEN f.Flag3 = 'Yes' THEN 1 ELSE 0 END

Updating column values as per our format

There are two types of records in my Db such as MS-NW and CS in the same column of table DICIPLINE I want to wrap if its CS (ANY TWO STRING LIKE CS,TE OR THE LIKE) then wrap it to BS(CS) (OR BS(TE) ETC) or if its MS-NW (Or MS-CS, MS-TE and the like) then wrap it to MS(NW) from the column dicipline.
I updated for two strings successfully and following is the query for that kindly let me know how can i do it for values like MS-NW OR MS-CS and convert it to the format like MS(NW) from following query .
UPDATE DEG set DICIPLINE = concat("BS(",DICIPLINE,")") where CHAR_LENGTH(DICIPLINE) = 2
The below query helps you to update your data.
update deg set DISIPLINE = if(length(DISIPLINE)= 2,concat('BC(',DISIPLINE,')')
,concat('MS(',substr(DISIPLINE, 4,4),')'));
See Sqlfiddle demo.
For safety, create a temporary column of same type and perform an update like this:
UPDATE deg
SET dicipline_temp = CASE
WHEN CHAR_LENGTH(dicipline) = 2
THEN CONCAT('BS(', dicipline, ')')
WHEN CHAR_LENGTH(dicipline) = 5 AND SUBSTRING(dicipline, 3, 1) = '-'
THEN CONCAT(REPLACE(dicipline, '-', '('), ')')
END
WHERE CHAR_LENGTH(dicipline) = 2 OR (CHAR_LENGTH(dicipline) = 5 AND SUBSTRING(dicipline, 3, 1) = '-')
If results are acceptable, update the actual column.