Recently I am using MySQL as database for small tasking to joining 2 table like below:-
But I have problem regarding complexity query and some of data are not well-formatted like table A.Bill_No (01X) ---> B.No (1X) where "0"1X are not appear as value on column table B.No
I also need to choose on B.From = ''ext'' only as condition WITH longest Duration as you can see desired output like below:-
I don't have idea how to create query statement like that output but I am hoping anybody on this forum please help me if you facing a same problem like I am now.
Anyway thanks very much on advance for read and replying this question.
I have a small progress to archive my output where I use SUBSTRING to equalize some value are not well-formatted as #Gordon Linoff mention earlier. To archive that I use the sql statement like below:-
Select
a.id,
b.no
From
a Inner Join
b On SubString(a.bill_no From 2 For 8 ) = b.no
Now, I need to filter the longest one duration for each ID if have more than one record on Table B to completed my query.
Please forums give me some advice to archive that, your kindness and helping I am are really appreciated.
I have two tables ini mysql database
First
1.2.3.4
5.6.7.8
9.10.11.12
13.14.15.16
17.18.19.20
21.22.23.24
Second
a.b.c.d
e.f.g.h
1.2.3.4
13.14.15.16
21.22.23.24
25.26.27.28
29.30.31.32
33.34.35.36
I want to select values from the two tables and print Only values that not exist in the first table ignoring value that not exist in the second table.
The result should be :
5.6.7.8
9.10.11.12
17.18.19.20
I think I've search all the result in stackoverflow. And I know some basic mysql . But nothing gave me the result I expected. Hard to explain I know. Or maybe I made simple think complicated I don't know :D.
Thank you.
use sql distint statement.....
eg : SELECT DISTINCT City FROM Customers
I've found the answer, as it's been answered many times ,as I suspected I've made a simple thing complicated with to much data. :D.
I know this is an amateur question, but I've searched every resource I can think of, and now I'm at my wit's end. The following query works perfectly on most of my tables, but for some reason it is not working on the tables that I desperately need it for:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE daypart LIKE 'AM';
The query works exactly how I want it to for nearly all of my tables, but for some reason it is returning a value of "0" on the tables I need it for (even though there are over 20 instances of "AM" in the "daypart" column on this table). I have checked and double-checked everything I can think of.
I'm relatively new to SQL but I've never encountered a problem like this before. Anyone have any ideas or resources that might help? Thanks so much for your time!
EDIT: I don't have enough reputation points to post a screen shot on here... but here's a link where you can see one: http://imgur.com/ZhyEqJY
There are 29 columns in this table. If there's any other info that might help just let me know, thanks!
You need to add the like part as shown below:
where column_name like '%AM%'
when you write like 'AM' it is searching for the full match
Try this.
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
If you want to order it using the count,
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%' ORDER BY COUNT(*) DESC;
DESC - Descending order
ASC - Ascending order
LIKE statements are really slow in sql. If you have a daypart column then I assume you have a date column. I recommend something like this instead:
SELECT COUNT(*)
FROM radio_r1_own_it
WHERE HOUR(dateColumn) < 13
Unless the string you're matching is exactly AM you need to use wildcard characters to match the string:
SELECT COUNT(*) FROM radio_r1_own_it WHERE daypart LIKE '%AM%';
The % matches any number of characters before and after AM.
See the documentation for more information.
I know this might shock some but I just cant figure out what's wrong with this SQL statement.. it kills my head.
SELECT * FROM groups WHERE gname LIKE '%m%';
I am using mysql 5.1.41
If you don't get any rows back, perhaps that's the right answer. Mistakes usually happen when your assumptions don't match the situation at hand.
Are you sure you have values in the gname column with lower case 'm' in them?
Are you sure you're running the query against the database you think you are?
This statement will retrieve every column and every row from the groups table where the gname column contains a lowercase m somewhere. Is this what you wanted to achieve?
It's common to have a table where for example the the fields are account, value, and time. What's the best design pattern for retrieving the last value for each account? Unfortunately the last keyword in a grouping gives you the last physical record in the database, not the last record by any sorting. Which means IMHO it should never be used. The two clumsy approaches I use are either a subquery approach or a secondary query to determine the last record, and then joining to the table to find the value. Isn't there a more elegant approach?
could you not do:
select account,last(value),max(time)
from table
group by account
I tested this (granted for a very small, almost trivial record set) and it produced proper results.
Edit:
that also doesn't work after some more testing. I did a fair bit of access programming in a past life and feel like there is a way to do what your asking in 1 query, but im drawing a blank at the moment. sorry.
After literally years of searching I finally found the answer at the link below #3. The sub-queries above will work, but are very slow -- debilitatingly slow for my purposes.
The more popular answer is a tri-level query: 1st level finds the max, 2nd level gets the field values based on the 1st query. The result is then joined in as a table to the main query. Fast but complicated and time-consuming to code/maintain.
This link works, still runs pretty fast and is a lot less work to code/maintain. Thanks to the authors of this site.
http://access.mvps.org/access/queries/qry0020.htm
The subquery option sounds best to me, something like the following psuedo-sql. It may be possible/necessary to optimize it via a join, that will depend on the capabilities of the SQL engine.
select *
from table
where account+time in (select account+max(time)
from table
group by account
order by time)
This is a good trick for returning the last record in a table:
SELECT TOP 1 * FROM TableName ORDER BY Time DESC
Check out this site for more info.
#Tom
It might be easier for me in general to do the "In" query that you've suggested. Generally I do something like
select T1.account, T1.value
from table T as T1
where T1 = (select max(T2.time) from table T as T2 where T1.account = T2.Account)
#shs
yes, that select last(value) SHOULD work, but it doesn't... My understanding although I can't produce an authorative source is that the last(value) gives the last physical record in the access file, which means it could be the first one timewise but the last one physically. So I don't think you should use last(value) for anything other than a really bad random row.
I'm trying to find the latest date in a group using the Access 2003 query builder, and ran into the same problem trying to use LAST for a date field. But it looks like using MAX finds the lates date.
Perhaps the following SQL is clumsy, but it seems to work correctly in Access.
SELECT
a.account,
a.time,
a.value
FROM
tablename AS a INNER JOIN [
SELECT
account,
Max(time) AS MaxOftime
FROM
tablename
GROUP BY
account
]. AS b
ON
(a.time = b.MaxOftime)
AND (a.account = b.account)
;