MySQL query - only exact result or every choice - mysql

I've a query that I need some help with -
As part of a form I've got a serial number field that is populated if there is a serial number, blank if it's not, or no result if it's an invalid serial number.
select *
from cust_site_contract as cs
where cs.serial_no = 'C20050' or (cs.serial_no <> 'C20050' and if(cs.serial_no = 'C20050',1,0)=0)
limit 10;
Here's a sample of the regular data:
+----------------------+-----------+-----------+-----------
| idcust_site_contract | system_id | serial_no | end_date
+----------------------+-----------+-----------+-----------
| 561315 | SH001626 | C19244 | 2009-12-21
| 561316 | SH001626 | C19244 | 2010-06-30
| 561317 | SH002125 | C19671 | 2010-05-31
| 561318 | SH001766 | C14781 | 2010-09-25
| 561319 | SH001766 | C14781 | 2011-02-15
| 561320 | SH002059 | C19020 | 2008-07-09
| 561321 | SH002639 | C18889 | 2008-03-31
| 561322 | SH002639 | C18889 | 2008-06-30
| 561323 | SH002715 | C20051 | 2010-04-30
| 561324 | SH002719 | C20057 | 2010-04-30
And an exact result would look something like this:
| 561487 | SH002837 | C20050 | 2012-07-04
I was writing this as a subquery so I could match the system_ids to customer and contract names, but realised I was getting garbage pretty early on.
I'm tempted to try and simplify it by saying the third case might not hold true (i.e. if it's an invalid serial number, allow the choice of any customer name and simply flag it in the data)
Has anyone got any ideas of where I'm going wrong? The combination of conditions is clearly wrong, and I can't work out how to make each side of the or statement mutually exclusive
Even if I try to evaluate only the if(sn = 'blah') I get the wrong result for obvious reasons, but can't think of a sane way to express it.
Many thanks
Scott

If there is is no contract with a serial number of C20050, this query will return all rows, otherwise, it will return only one row where serial_no is C20050:
SELECT a.*
FROM cust_site_contract a
INNER JOIN
(
SELECT COUNT(*) AS rowexists
FROM cust_site_contract
WHERE serial_no = 'C20050'
) b ON b.rowexists = 0
UNION ALL
(
SELECT *
FROM cust_site_contract
WHERE serial_no = 'C20050'
LIMIT 1
)

If you just write the query as below you will get blank if doesn't exists or it's an invalid serial number.
select cs.serial_no from cust_site_contract as cs where cs.serial_no = 'C20050'

Related

MySQL - Recursive - getting email addresses from 2 different tables and columns

I have a first table called emails with a list of all the emails of my colleagues
| email |
| ----------------------- |
| saramaia#email.com |
| miguelferreira#email.com |
| joaosilva#email.com |
| joanamaia#email.com |
I have a second table called aliases, with a list of all the secondary emails/aliases my colleagues are using
| alias1 | alias2 |
| ------------------------ | ------------------- |
| joanamaia#email.com | maiajoana#email.com |
| maiajoana#email.com | maia#email.com |
| miguelferreira#email.com | miguel#email.com |
| maia#email.com | joana#email.com |
| joanamaia#email.com | jomaia#email.com |
| joana#email.com | jmaia#email.com |
I can see that the users joanamaia#email.com and miguelferreira#email.com are using aliases. But let's focus on the user joanamaia#email.com.
I need to get a list of all the email addresses the user joanamaia#email.com is using. The difficult part is that I need to get a list with the main email address plus all the intersections where the first email and consecutive ones are being used by this user. The end result should look like this
| emails |
| ------------------- |
| joanamaia#email.com |
| jomaia#email.com |
| maiajoana#email.com |
| maia#email.com |
| joana#email.com |
| jmaia#email.com |
If I do WHERE email='joanamaia#email.com' it should look like this, but I also need the same result if I do
WHERE email='jmaia#email.com'
I've been through some days of testing queries and I don't seem to have a solution for this (I've been using right joins, full outer joins and unions, but no luck so far). Is there a good way to do this?
You can use a recursive CTE to walk the graph and get the full list of interconnected aliases. Care needs to be taken to handle cycles; that requires the query to use UNION instead of the traditional UNION ALL to separate the anchor and recursive member of the CTE.
The query can take the form:
with recursive
n as (
select 'joanamaia#email.com' as email
union
select case when a.alias1 = n.email then a.alias2 else a.alias1 end
from n
join aliases a on (a.alias1 = n.email or a.alias2 = n.email)
and a.alias1 <> a.alias2
)
select * from n;
Result:
email
-------------------
joanamaia#email.com
maiajoana#email.com
jomaia#email.com
maia#email.com
joana#email.com
jmaia#email.com
See running example at DB Fiddle.

List Last record of each item in mysql

Each item(item is produced by Serial) in my table has many record and I need to get last record of each item so I run below code:
SELECT ID,Calendar,Serial,MAX(ID)
FROM store
GROUP BY Serial DESC
it means it must show a record for each item which in that record all data of columns be for last record related to each item but the result is like this:
-------------------------------------------------------------+
ID | Calendar | Serial | MAX(ID) |
-------------------------------------------------------------|
7031053 | 2016-05-14 14:05:14 79.5 | N10088 | 7031056 |
7053346 | 2016-05-14 15:17:28 79.8 | N10078 | 7053346 |
7051349 | 2016-05-14 15:21:29 86.1 | J20368 | 7051349 |
7059144 | 2016-05-14 15:50:27 89.6 | J20367 | 7059144 |
7045551 | 2016-05-14 15:15:15 89.2 | J20366 | 7045551 |
7056243 | 2016-05-14 15:25:34 85.2 | J20358 | 7056245 |
7042652 | 2016-05-14 15:18:33 83.9 | J20160 | 7042652 |
7039753 | 2016-05-14 11:48:16 87 | J20158 | 7039753 |
7036854 | 2016-05-14 15:18:35 87.5 | J20128 | 7036854 |
7033955 | 2016-05-14 15:20:45 83.4 | 9662 | 7033955 |
-------------------------------------------------------------+
the problem is why for example in record related to Serial N10088 the ID is "7031053", but MAX(ID) is "7031056"? or also for J20358?
each row must show last record of each item but in my output it is not true!
If you want the row with the max value, then you need a join or some other mechanism.
Here is a simple way using a correlated subquery:
select s.*
from store s
where s.id = (
select max(s2.id)
from store s2
where s2.serial = s.serial
);
You query uses a (mis)feature of SQL Server that generates lots of confusion and is not particularly helpful: you have columns in the select that are not in the group by. What value do these get?
Well, in most databases the answer is simple: the query generates an error as ANSI specifies. MySQL pulls the values for the additional columns from indeterminate matching rows. That is rarely what the writer of the query intends.
For performance, add an index on store(serial, id).
try this one.
SELECT MAX(id), tbl.*
FROM store tbl
GROUP BY Serial
You can try with this also...
SELECT ID,Calendar,Serial
FROM store s0
where ID = (
SELECT MAX(id)
FROM store s1
WHERE s1.serial = s0.serial
);

Only return an ordered subset of the rows from a joined table

Given a structure like this in a MySQL database
#data_table
(id) | user_id | time | (...)
#relations_table
(id) | user_id | user_coach_id | (...)
we can select all data_table rows belonging to a certain user_coach_id (let's say 1) with
SELECT rel.`user_coach_id`, dat.*
FROM `relations_table` rel
LEFT JOIN `data_table` dat ON rel.`uid` = dat.`uid`
WHERE rel.`user_coach_id` = 1
ORDER BY val.`time` DESC
returning something like
| user_coach_id | id | user_id | time | data1 | data2 | ...
| 1 | 9 | 4 | 15 | foo | bar | ...
| 1 | 7 | 3 | 12 | oof | rab | ...
| 1 | 6 | 4 | 11 | ofo | abr | ...
| 1 | 4 | 4 | 5 | foo | bra | ...
(And so on. Of course time are not integers in reality but to keep it simple.)
But now I would like to query (ideally) only up to an arbitrary number of rows from data_table per distinct user_id but still have those ordered (i.e. newest first). Is that even possible?
I know I can use GROUP BY user_id to only return 1 row per user, but then the ordering doesn't work and it seems kind of unpredictable which row will be in the result. I guess it's doable with a subquery, but I haven't figured it out yet.
To limit the number of rows in each GROUP is complicated. It is probably best done with an #variable to count, plus an outer query to throw out the rows beyond the limit.
My blog on Groupwise Max gives some hints of how to do such.

MySQL: optimize query for scoring calculation

I have a data table that I use to do some calculations. The resulting data set after calculations looks like:
+------------+-----------+------+----------+
| id_process | id_region | type | result |
+------------+-----------+------+----------+
| 1 | 4 | 1 | 65.2174 |
| 1 | 5 | 1 | 78.7419 |
| 1 | 6 | 1 | 95.2308 |
| 1 | 4 | 1 | 25.0000 |
| 1 | 7 | 1 | 100.0000 |
+------------+-----------+------+----------+
By other hand I have other table that contains a set of ranges that are used to classify the calculations results. The range tables looks like:
+----------+--------------+---------+
| id_level | start | end | status |
+----------+--------------+---------+
| 1 | 0 | 75 | Danger |
| 2 | 76 | 90 | Alert |
| 3 | 91 | 100 | Good |
+----------+--------------+---------+
I need to do a query that add the corresponding 'status' column to each value when do calculations. Currently, I can do that adding the following field to calculation query:
select
...,
...,
[math formula] as result,
(select status
from ranges r
where result between r.start and r.end) status
from ...
where ...
It works ok. But when I have a lot of rows (more than 200K), calculation query become slow.
My question is: there is some way to find that 'status' value without do that subquery?
Some one have worked on something similar before?
Thanks
Yes, you are looking for a subquery and join:
select s.*, r.status
from (select s.*
from <your query here>
) s left outer join
ranges r
on s.result between r.start and r.end
Explicit joins often optimize better than nested select. In this case, though, the ranges table seems pretty small, so this may not be the performance issue.

mysql query returns an empty result set

I have been tasked with a query I am having problems with. Here is the query:
Given a user id and a month, produce a list containing student name, list of files they own (largest to smallest) including total number of files and number of bytes used in a month specified.
Here is what I have so far:
(Select * from htmp_cs368
Join roster_cs368 ON htmp_cs368.userId =
roster_cs368.lastName Where htmp_cs368.userId =
(SELECT lastName FROM roster_cs368 WHERE userId = 'userId' AND htmp_cs368.monthIn = 'monthIn'))
UNION
(Select * from atmp_cs368
JOIN roster_cs368 ON atmp_cs368.userId =
roster_cs368.userId Where roster_cs368.userId =
'userId' AND atmp_cs368.monthIn = 'monthIn') ORDER BY fileSize DESC;
I am getting a result of empty set. My tables are full. I am hoping somone can correct my mistakes.
I have included my schema:
mysql> select * from roster_cs368
-> ;
+--------+-----------+-----------+
| userId | firstName | lastName |
+--------+-----------+-----------+
| apn7cf | Allen | Newton |
| atggg3 | andrew | goebel |
Primary key is userId
mysql> select * from htmp_cs368;
+------------+----------+------------+----------+----------+-------+------+-------+----------------------+
| filePerms | numLinks | userId | idGroup | fileSize | monthIn | day | time | fileName |
+------------+----------+------------+----------+----------+-------+------+-------+----------------------+
| drwx------ | 2 | schulte | faculty | 289 | Nov | 7 | 2011 | Java |
| -rw-r--r-- | 1 | schulte | faculty | 136 | Apr | 29 | 2012 | LD |
| drwxr-xr-x | 3 | schulte | faculty | 177 | Mar | 20 | 2012 | Upgrade |
No primary key here
select * from atmp_cs368;
+------------+----------+--------------+----------+----------+-------+------+-------+-----------------------------+
| filePerms | numLinks | userId | idGroup | fileSize | monthIn | day | time | fileName |
+------------+----------+--------------+----------+----------+-------+------+-------+-----------------------------+
| drwxr-xr-x | 2 | remierm | 203 | 245 | Sep | 17 | 14:40 | 148360_sun_studio_12 |
| drwx---rwx | 31 | antognolij | sasl | 2315 | Oct | 24 | 12:28 | 275 |
| -rwx------ | 1 | kyzvdb | student | 36 | Sep | 19 | 13:05 | 275hh |
No primary key here as either.
I have had very little experience with mysql. I also have to come up with:
If no user id is specified, all files, if no month specified, all users and if neither specified, all months and users.
I am stuck and at a lost. I appreciate any help! Thanks!
You seem to have a number of problems in the SQL.
First
Join roster_cs368 ON htmp_cs368.userId = roster_cs368.lastName
You try to join the userId field to the lastName field, which definitely won't work. It should be userId in both tables.
Then
WHERE userId = 'userId' AND htmp_cs368.monthIn = 'monthIn'
Assuming those really are literal strings, they won't match anything in the table. You need to use a parameterized query, and substitute question marks in the SQL, as in
WHERE userId = ? AND htmp_cs368.monthIn = ?
and provide the actual values to be used in the Java code.
I think you're looking for something along these lines (untested, but this will give you a starting point)
List of files
select r.lastName, r.firstName, t.fileName, t.fileSize
from htmp_cs368 t join roster_cs368 r on t.userId=r.userId
where t.userId=? and t.monthIn=?
order by fileSize desc
Summary:
select r.lastName, r.firstName, count(t.fileName), sum(t.fileSize)
from htmp_cs368 t join roster_cs368 r on t.userId=r.userId
where t.userId=? and t.monthIn=?
group by t.userId
This is a simple approach that does not take into account files appearing and disappearing during a month, but you don't seem to have data in your tables for this.
Also, it's not clear what atmp_cs368 is for, or why the time column in one table seems to have year values.
As pointed out by others you seem to have a number of problems in your SQL. I dont think it can compile as well.
Try:
SELECT r.userId, files.*
FROM roster_cs368 AS r
JOIN (
Select * from htmp_cs368 WHERE userId = 'userId' AND monthIn = 'monthIn'
UNION
Select * from atmp_cs368 Where userId = 'userId' AND monthIn = 'monthIn'
) AS files ON files.userId = r.userId
ORDER BY files.fileSize DESC;
You need only one JOIN. This lists users and all their files. And take care to equate apples to apples (userId != lastName).
Now to get count of files and file sizes etc you need a GroupBy effectively. But you cannot list files and get count of files together "easily". It will have to be one way or other. Just for the count you can use Jim's solution.
This JOIN looks a tad suspicious...
JOIN roster_cs368 ON htmp_cs368.userId = roster_cs368.lastName
Even if userId in htmp_cs368 has an equivalent value in the lastName column of roster_cs368, this is very bad form. JOINS should typically be done on like-named columns.
If these two columns are unrelated (it's hard to tell when roster_cs368 also has a userId column), then that would be at least part of your problem.
Also, htmp_cs368.monthIn = 'monthIn' doesn't make sense. This won't match anything in that column either.