mysql query with when statement comparing - mysql

I want to write mysql when statement and i can't do it.
explanation => localParty is column in table "data", loc is column from table "o", and i want to compare localParty to loc, if their values are equal then i want to retrieve information from loc_m column (this column is from table "o"), and if not equal then from localParty column (from "data" table)
Please help how to write this script in mysql query ? Thanks
with this script
select (case when data.localparty = o.loc then o.loc_m else data.localparty end)
as customdata from data, o
it is working but it is missing exactly three result ( I mean that then data.localparty equal to o.loca it is giving result from data.localparty 3 times and after it one time it is giving result from loc_m and it is going like so .

You could modify the query in the following way:
SELECT IF(t1.Column1 = t2.Column2,t2.Column1,t1.Column3) FROM TABLE1 AS t1, Table2 AS t2

Try This:
select (case when data.localparty = o.loc then o.loc_m else data.localparty end)
as customdata from data, o

you can use following query
Select O.loc_m as local
from Data
inner join on O on data.localparty=O.loc
UNION
Select data.loacalparty as local
from Data
where data.localparty is not in (select loc from O )

You should use control-flow functions to achieve that goal:
SELECT IF(Column1 = Column2,Column1,Column3) FROM TABLE1

Related

Need to figure out when and ID have two different codes associated in SQL

Hi I need some help with this in SQL.
I have a query like this
select sub.*
from(
select
distinct columnA,
case
'some logic that is working'
end as ColumnB,
columnC,
"ColumnD"
from Table1
where "ColumnD"::date = 'some date'
UNION
select
distinct columnA,
case
'some logic that is working'
end as ColumnB,
columnC,
"ColumnD"
from Table1
where "ColumnD"::date = 'some date'
) sub
order by sub.columnC
Result
Column A Column B Column C Column D
abc old a
abc old a
jhk old b
1ab2 new b
25sa new c
24sb new d
ujy old e
45wr new e
Now in the column A we have code associated to a customer, those codes changed from numerics to alphanumerics. So I need create another query to work in this result (the result of the firt query), to identify all the customers who migrated from old codes to new codes. The output need to be something like this
oldCode currentCode oldType newType
jhk 1ab2 old new
ujy 45wr old new
Thanks
You can use SELF JOIN on a resulted table to get the expected result.
Also, You can use the tamporary table to store the result of the complex query else for the simple query you can use your first query directly in FROM clause of the second query.
CREATE TEMPORARY TABLE test
SELECT * FROM tblname; //this line you can write your first query
CREATE TEMPORARY TABLE test_copy LIKE test;
INSERT INTO test_copy (SELECT * FROM test);
SELECT
t.ColumnA as oldCode,
t1.ColumnA as newCode,
t.ColumnB as oldType,
t1.ColumnB as newType
FROM
test as t JOIN test_copy as t1
ON t.ColumnC=t1.ColumnC
WHERE
t.ColumnB="old" AND t1.ColumnB="New";
DEMO

MySQL query to get fields from a table where the ID exists in another query

This must be fairly straight forward, as I tend to use ORMs I don't have to get my hands dirty often and am therefore struggling!
I have a database and want to get several fields from a table, that bit is easy..
SELECT main_table.registration_number, main_table.registered_name FROM main_table;
I want to filter the results based on another table, which is also easy..
SELECT second_table.registration_number FROM second_table WHERE this_field = '' AND that_field = '0';
Now the problem is I want to run the first query based on the second queries result set, I was thinking something like this:
SELECT main_table.registration_number, main_table.registered_name FROM main_table WHERE main_table.registration_number IN (SELECT * FROM second_table WHERE this_field = '' AND that_field = '0');
This gives me: Error Code: 1241. Operand should contain 1 column(s)
Am I handling this completely wrong?
Your subquery should do something like below,
(select * from table) in subquery is not what you really need to do your
so the subquery should return one column
(SELECT registration_number FROM second_table WHERE this_field = '' AND that_field = '0');
You cannot have multiple columns being returned in a subquery like
that, doing so it will result in such error
You have to select a column
SELECT main_table.registration_number, main_table.registered_name FROM
main_table WHERE main_table.registration_number IN (SELECT
registration_number FROM second_table WHERE this_field = '' AND
that_field = '0');

Count with a subselect yielding double the amount

I'm new to SQL.
Problem: Say if I were to count the amount that is contained in the alias table of "x" COUNT(x.xValue) to be 217. Now when I add the sub-query "y" and then do the count again, I have the COUNT(x.xValue) to suddenly square its self -> 47089. Why is this happening?
(Note: Both alias tables "x" and "y" have the same amount -> 217.)
How do I fix this problem. I don't want to use Variables or Views.
SELECT COUNT(x.xValue) + COUNT(y.yValue) AS CountXY
FROM
(SELECT value AS xValue FROM table1
WHERE
...) AS x,
(SELECT value AS yValue FROM table1
WHERE
...) AS y
Result of 'CountXY' : 94178.
Result I'm expecting 'CountXY' : 434
The problem is that you are doing two sub-queries and then trying to call the values return directly.
This will behave as selecting one value from table x and matching it to every single value in table y. This obviously creates the squared return effect.
What you need to use is the JOIN to combine both data-sets so that you get the 1 to 1 relationship you are trying to achieve.
This is how the above should be done with your previous sub-query:
SELECT COUNT(A.value) AS x, COUNT(B.value) AS y
FROM table1 AS A
JOIN table1 AS B
ON A.attr1 = B.attr1
AND A.attr2 = B.attr2
WHERE B.attr1 != 'whatever'
AND B.attr2 = 'whatever'
AND A.attr3 = 'something'
AND B.attr3 = 'something different'
The above query should return the correct 1 to 1 relationship you are looking for. Replacing your sub-query with the one above should give you the correct answer

Mysql, combining and querying results with sub queries or temp tables

I am running into some trouble with the following circumstances:
I have a query that creates two temp tables, and the following select to join them together--
SELECT * FROM result
INNER JOIN result2 ON result2.packetDetailsId = result.packetDetailsId
I am then trying to create another column from concatenating a few of the resulting fields and then use that to reference/query against another table. Is there a way to accomplish this in one query? Should I get away from the temp tables?
Thank you again in advance.
update: If I try to alias the combination of the two temp tables I get an error message stating [Err] 1060 - Duplicate column name 'packetDetailsId'
select * from (
SELECT * FROM result
INNER JOIN result2 ON result2.packetDetailsId = result.packetDetailsId) as myalias
Another Update: I almost have it working as one query but I get the result "(BLOB)" in the column I concoctenated:
select packet_details.packetDetailsId,products.productId,Credit,AccountNum,OrderStat, CONCAT(products.productId,Credit,'_',OrderStat) as consol from (
select packetDetailsId, GROUP_CONCAT(Credit) AS Credit, GROUP_CONCAT(AccountNum) AS AccountNum, GROUP_CONCAT(OrderStat) AS OrderStat FROM
( SELECT pd_extrafields.packetDetailsId,
CASE WHEN pd_extrafields.ex_title LIKE ('%Credit%')
THEN pd_extrafields.ex_value ELSE NULL END as Credit,
CASE WHEN pd_extrafields.ex_title LIKE ('%Account%')
THEN pd_extrafields.ex_value ELSE NULL END as AccountNum,
CASE WHEN pd_extrafields.ex_title LIKE ('%Existing%')
THEN pd_extrafields.ex_value ELSE NULL END as OrderStat
FROM pd_extrafields )AS TempTab GROUP BY packetDetailsId ) as alias2
INNER JOIN packet_details ON alias2.packetDetailsId = packet_details.packetDetailsId
INNER JOIN sales ON packet_details.packetDetailsId = sales.packetDetailsId
INNER JOIN sold_products ON sales.saleId = sold_products.saleId
INNER JOIN products ON sold_products.productId = products.productId
If I understand correctly, you already have the temporary tables created and you need to "concatenate" the results, using from ... inner join ...
The only possible restriction you may have is that you can only reference your temporary tables once in your from clause; besides that, there are no other restrictions (I frequently use temporary tables as intermediate steps in the creation of my final result).
Tips
Let's say your temp tables are temp_result1 and temp_result2. Both tables have a field packedDetailsId, on which the join will be performed. Remember to create the appropriate indexes on each table; at the very least you need to index packedDetailsId on both tables:
alter table temp_result1
add index PDI(packedDetailsId);
alter table temp_result2
add index PDI(packedDetailsId);
Now, just execute a query with the desired join and concatenation. If concat returns BLOB, then cast the result as char (of course, I'm assuming you need a text string):
select r1.*, r2.*, cast(concat(r1.field1, ',', r2.field2) as char) as data_concat
from temp_result1 as r1
inner join temp_result2 as r2 on r1.packedDetailsId = r2.packedDetailsId;
I see your problem is that GROUP_CONCAT is returning BLOB values... It's normal (MySQL doesn't know a priori how to return the values, so it returns binary data); just use the cast function.
Hope this helps you
so, if the result2 and result are both temp tables, you will have to include the # if local temp table and ## if global temp table
so your statements should be :
SELECT * FROM #result
INNER JOIN #result2 ON #result2.packetDetailsId = #result.packetDetailsId
My Bad. This is only applicable for MS SQL

How to run a case sensative comparison on text fields Access

I am trying to run a query to remove a set of ID's from a table when they are present in a field from another table.
The problem is both ID fields are of type text and the search does not appear to be case sensitive (but I need it to be). (i.e. ABC123 is different than abc123)
I am running a query similar to Select myID from table1 where myID NOT IN (Select otherID from table2)
What modification do I need to make in my Access query to make the results case sensitive when running comparison?
Try this:
SELECT a.*
FROM table1 a LEFT JOIN table2 b
ON a.myID = b.otherID
WHERE StrComp(IIF(IsNull(b.otherID ), a.myID , b.otherID), a.myID, 0) <> 0
OR IsNull(b.otherID)