Access query where one field is LIKE another - ms-access

I'm trying to query on on field in one table where it is LIKE a field in another table but am having trouble getting valid results.
I want to find the Pager_ID in tbl_Emergin_Current_Device_Listing_20121126 where it is like the Pager_ID in tbl_AMCOM_PROD.
Some relevant information:
Pager_ID in tbl_Emergin_Current_Device_Listing_20121126 is at most 10 characters and are always numeric characters (example of 10 character Pager_ID: 3145551212).
However, Pager_ID in tbl_AMCOM_PROD can be alpha-numeric (3145551212#att.txt.com, which would be the same user.
All data is stored as text.
I want to be able to find "3145551212#att.txt.com" in tbl_Amcom_Prod.Pager_ID when "3145551212" is present in tbl_Emergin_Current_Device_Listing_20121126.Pager_ID. However, with the code below I'm only finding exact matches (EQUAL instead of LIKE).
current code:
SELECT DISTINCT tbl_emergin_current_device_listing_20121126.userrecno,
tbl_emergin_current_device_listing_20121126.username,
tbl_emergin_current_device_listing_20121126.department,
tbl_emergin_current_device_listing_20121126.carriername,
tbl_emergin_current_device_listing_20121126.protocol,
tbl_emergin_current_device_listing_20121126.pin,
tbl_emergin_current_device_listing_20121126.pager_id,
Iif([tbl_amcom_group_call_leads_and_id].[amcom listing msg id],
[tbl_amcom_group_call_leads_and_id].[amcom msg group id],
[tbl_amcom_prod].[messaging_id])
AS [Amcom Messaging or Message Group ID]
FROM ((tbl_emergin_current_device_listing_20121126
LEFT JOIN tbl_amcom_prod
ON tbl_emergin_current_device_listing_20121126.pager_id =
tbl_amcom_prod.pager_id)
LEFT JOIN tbl_amcom_group_call_leads_and_id
ON tbl_emergin_current_device_listing_20121126.pager_id =
tbl_amcom_group_call_leads_and_id.[ams group call lead])
LEFT JOIN tbl_deactivated_pager_list
ON tbl_emergin_current_device_listing_20121126.pager_id =
tbl_deactivated_pager_list.[pager number];
Sample Results:
UserRecNo UserName Department CarrierName Protocol PIN PAGER_ID Amcom Messaging or Message Group ID
43 Brown, Lewis BJH Verizon 0 3145550785 3145550785 3145550785
52 Wyman, Mel BJH Airtouch (Verizon) (SNPP) 3 3145558597 3145558597 3145558330
I'd also like to see this record but am not with current code:
57 Johnson, Mick BJH AT&T 3 3145551234 3145551234#att.txt.com 3145559876
What change should I be making?
Thanks in advance!

Something like:
SELECT Pager_ID
FROM tbl_Amcom_Prod a
LEFT JOIN [tbl_Emergin_Current_Device_Listing_20121126] b
On a.Pager_ID & "*" Like b.Pager_ID
This will only work in SQL view, not design view.
You could also use a mixture of Instr & Mid.
SELECT IIf(InStr([Pager_ID] & "",".")>0,
Mid([Pager_ID],1,InStr([Pager_ID],".")-1),[Pager_ID ]) AS PID
FROM [tbl_Amcom_Prod]
WHERE IIf(InStr([Pager_ID] & "",".")>0,
Mid([Pager_ID],1,InStr([Pager_ID],".")-1),[Pager_ID])
In (SELECT Pager_ID
FROM [tbl_Emergin_Current_Device_Listing_20121126])

Related

MySQL: Return the highest number of the same table after making one query

We have these tables:_
table Groupmessages:
and table Groups (Called circles here):
If a user is new to a circle he or she will automatically create a message in the group message table with the content: ";;--;;!!IAMNEW". This is done for dataflow reasons, and will be sorted out when we query the table for the actual messages.
It does however tell us what user is in what group. In this example, user 216 and 215 both wrote 2 messages in circle ID 72. So 4 messages in total.
What we wanna do now is query those ables and find out if I am a part of this group and have written my IAMNEW message. This looks like this:
var sqlString1 = "SELECT a.*, b.smallPic \
FROM 10561_12865_tblGroupMessages a \
LEFT JOIN 10561_12865_tblCircles b ON (a.fromCircles = b.tblCircleID) \
WHERE a.fromUser = '" + userID + "' AND \
a.message = ';;--;;!IAMNEW' \
LIMIT " + start + "," + count
The result is fine, and will look something like this with the input being: userID: 216, start: 0, count: 10.
message->;;--;;!IAMNEW
fromUser->216
fromCircles->72
tblGroupMessageID->24
timeCreated->2022-12-23 13:27:23
timeUpdated->0000-00-00 00:00:00
unixCreated->1671805643
It does also return us our own message with the content IAMNEW, and when it was written in UNIX time. But we also need something else inside this result:
As we wanna order the groupmessages by LAST message and not by our message we would need to add to the query something like:
And return the highest unixCreated from table Groupmessages where fromCircles is equal to (in this case) 72.
But the 72 is a result of the query above already.
Is there a way to query the table and then be returned the highest number from unixCreated from the same table inside the same query?
Best,
J

MySQL query to gather incorrectly stored data

I have recently taken over a email campaign project and need to generate a report for the customer. However the data has been stored very strangely.
Basically the client wants a report of the subscribers first name and last name that have subscribed to a emailing list.
Example table data.
------------------------------------------------------------
id | owner_id | list_id | field_id | email_address | value
------------------------------------------------------------
1 10 1 137 me#example.com John
2 10 1 138 me#example.com Doe
So as you can see, John Doe has subscribed to mailing list 1, and field_id 137 is his first name and field_id 138 is his last name.
The client is looking for a export with the users first name and last name all is one field.
I tred the following sql query
SELECT value
FROM Table_A AS child
INNER JOIN Table_A AS parent
ON parent.email_address = child.email_address
WHERE child.owner_id = '10'
But unfortunately the query gives me the results in many rows but not appending the first name and last name into one field,
If anyone can provide some assistance that would be awesome.
Thanks.
SELECT
concat( parent.value,' ',child.value)name
FROM mytable AS child
left JOIN mytable AS parent
ON parent.email_address = child.email_address
WHERE child.owner_id = '10'
and parent.field_id=137 and child.field_id=138
Check at-http://sqlfiddle.com/#!9/199b4b/45
I think you have to use a variable to put in there everything you have to and then select the variable with the desired name of yours.
For example:
DECLARE #yourvariable VARCHAR(MAX)
SELECT #yourvariable = COALESCE(#yourvariable + " ") + value
FROM table_A
WHERE owner_id = 10
SELECT #yourvariable as FullName
Try that, it might help.
You can try this code(column name equals value in your original DB):
select a.name
from
table_a a inner join table_a b
on a.email_address = b.email_address and a.field_id <> b.field_id
where a.owner_id=10
order by a.field_id
Here is the example link:
http://sqlfiddle.com/#!9/5fbdf6/25/0
As per assumptions, first name has the field id 137 and last name has the field id 138.
You can try the following query to get the desired result.
SELECT CONCAT(SUBSTRING_INDEX(GROUP_CONCAT(`value`),",",1)," ",SUBSTRING_INDEX(GROUP_CONCAT(`value`),",",-1)) AS client_name
FROM Table_A
WHERE owner_id = 10
AND field_id IN (137, 138)
GROUP BY email_address;

AS statement in SQL with the value of another cell

I'm having an issue with this SQL query, though I think it's mainly a syntax issue.
"SELECT grantYear, grantAmount AS /?\
FROM granttoassociation
JOIN grantprovider ON granttoassociation.grantProvider = grantprovider.id
WHERE grantReceiver = ? "
I would like the grantAmount column to be returned under the name of the corresponding grantProvider, which can be found under grantProvider.name column in the grantprovider table.
(so in the end there would be one column returned for each different grantProvider.name)
So what should I put instead of the /?\ in my code ? grantProvider.name doesn't seem to be working.
EDIT
SELECT grantYear, grantAmount, grantprovider.name
FROM granttoassociation JOIN grantprovider
ON grantProvider = grantprovider.id
WHERE grantReceiver = 2891
Result:
grantYear grantAmount name
2009 3000 besancon
2010 1000 besancon
2011 0 besancon

Concatenating multiple rows into single line in MS Access [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Combine rows in Access 2007
Access 2007 - Concatenate fields from one column in one table into a single, comma delmited value in another table
Currently I have a table structure that is somewhat like this:
Name --- Cat --- Desc --- Thresh --- Perc --- Err --- BP
Bob -------C1-------Inf--------7Per--------0.05------0-----ADC2
Bob -------C1-------Inf--------7Per--------0.05------2-----BAC2
Bob -------C1-------Inf--------7Per--------0.05------0-----RBE2
Bob -------C1-------Inf--------7Per--------0.05------8-----VBE2
Bob -------C1-------Inf--------7Per--------0.05------6-----AEC2
Bob -------C1-------Inf--------7Per--------0.05------0-----PBC2
Bob -------C2-------Com------8Per--------0.45------1-----XBC4
Bob -------C2-------Com------8Per--------0.45------0-----AEC2
Bob -------C2-------Com------8Per--------0.45------0-----PBC2
Bob -------C2-------Com------8Per--------0.45------3-----ADC2
Bob -------C2-------Com------8Per--------0.45------0-----ADC2
Bob -------C2-------Com------8Per--------0.45------0-----BAC2
Joe--------C1-------Inf---------7Per--------0.05------0-----PBC2
Joe--------C1-------Inf---------7Per--------0.05------0-----ZTM2
Joe--------C1-------Inf---------7Per--------0.05------2-----QYC2
Joe--------C1-------Inf---------7Per--------0.05------0-----FLC2
Joe--------C1-------Inf---------7Per--------0.05------1-----KSC2
Joe--------C1-------Inf---------7Per--------0.05------0-----JYC2
What i'm looking to do is have 1 line per "Name" and per "Cat", that will sum up all the "Err" (per "Name" and "Cat") and concatenate only the "BP" fields into a single line. Such as:
Name --- Cat --- Desc --- Thresh --- Perc --- Err --- BP
Bob -------C1-------Inf--------7Per--------0.05-----16-----BAC2, VBE2, AEC2
Bob -------C2------Com------8Per--------0.45------4------XBC4, ADC2
Joe--------C1-------Inf--------7Per--------0.05------3------QYC2, KSC2
There have been similar questions asked but I cannot seem to apply it as my knowledge of VBA scripting is beginner. Is there any way to do all of this via SQL? If VBA scripting is the only option (ie. creating a function), any help would be greatly appreciated. Thank You in advance.
Question part 2:
I created the function as per Allen Browne's guide. The module is saved as modConcatRelated. Now, i've tried to run this query (im not sure if this is the correct SQL to get the result that i'm looking for):
SELECT
[Name],
[Cat],
[Desc],
[Thresh],
[Perc],
sum([Err]),
ConcatRelated("[BP]", "make_table_bp", "[Err] = " & [BP])
FROM make_table_bp
GROUP BY
[Name],
[Cat],
[Desc],
[Thresh],
[Perc],
[Err],
[BP];
It said "Error 3061. Too few parameters. Expected 1." Also it said "Undefined Function ConcatRelated." I'm looking for guidance on how to create the correct SQL statement so that I can call the ConcatRelated function correctly and yield the result as depicted above. Thanks again.
Next question:
What if the table had a unique date field tagged on as the last column in the table. Something like this:
Name --- Cat --- Desc --- Thresh --- Perc --- Err --- BP --- Date
Bob -------C1-------Inf--------7Per--------0.05------0-----ADC2--12/02/2011
Bob -------C1-------Inf--------7Per--------0.05------2-----BAC2--09/05/2011
Bob -------C1-------Inf--------7Per--------0.05------0-----RBE2--11/02/2011
Bob -------C1-------Inf--------7Per--------0.05------8-----VBE2--08/14/2012
Bob -------C1-------Inf--------7Per--------0.05------6-----AEC2--02/25/2009
Bob -------C1-------Inf--------7Per--------0.05------0-----PBC2--07/02/2011
Bob -------C2-------Com------8Per--------0.45------1-----XBC4--09/05/2011
Bob -------C2-------Com------8Per--------0.45------0-----AEC2--02/02/2010
Bob -------C2-------Com------8Per--------0.45------0-----PBC2--08/14/2012
Bob -------C2-------Com------8Per--------0.45------3-----ADC2--05/05/2001
Bob -------C2-------Com------8Per--------0.45------0-----ADC2--08/02/2010
Bob -------C2-------Com------8Per--------0.45------0-----BAC2--06/17/2010
Joe--------C1-------Inf---------7Per--------0.05------0-----PBC2--08/14/2012
Joe--------C1-------Inf---------7Per--------0.05------0-----ZTM2--09/05/2011
Joe--------C1-------Inf---------7Per--------0.05------2-----QYC2--05/17/2010
Joe--------C1-------Inf---------7Per--------0.05------0-----FLC2--3/19/2010
Joe--------C1-------Inf---------7Per--------0.05------1-----KSC2--09/05/2011
Joe--------C1-------Inf---------7Per--------0.05------0-----JYC2--08/14/2012
Let's say I wanted to build a query to say something like: show me all records still within this same format:
Name --- Cat --- Desc --- Thresh --- Perc --- Err --- BP
Bob -------C1-------Inf--------7Per--------0.05-----16-----BAC2, VBE2, AEC2
Bob -------C2------Com------8Per--------0.45------4------XBC4, ADC2
Joe--------C1-------Inf--------7Per--------0.05------3------QYC2, KSC2
But for a date range of 01/01/2009 to 09/31/2011
#HansUp could you help with this?
I used a subquery for the GROUP BY which computes the Sum of Err for each group. Then I added the ConcatRelated function (from Allen Browne) with the fields returned by the subquery. This is the query and the output (based on your sample data in make_table_bp) from the query:
SELECT
sub.[Name],
sub.Cat,
sub.[Desc],
sub.Thresh,
sub.Perc,
sub.SumOfErr,
ConcatRelated("BP",
"make_table_bp",
"[Err] > 0 AND [Name] = '" & sub.[Name]
& "' AND Cat = '"
& sub.Cat & "'",
"BP")
AS concat_BP
FROM
(SELECT
q.[Name],
q.Cat,
q.[Desc],
q.Thresh,
q.Perc,
Sum(q.[Err]) AS SumOfErr
FROM make_table_bp AS q
GROUP BY
q.[Name],
q.Cat,
q.[Desc],
q.Thresh,
q.Perc
) AS sub
ORDER BY
sub.Name,
sub.Cat;
The query outputs this result set:
Name Cat Desc Thresh Perc SumOfErr concat_BP
Bob C1 Inf 7Per 0.05 16 AEC2, BAC2, VBE2
Bob C2 Com 8Per 0.45 4 ADC2, XBC4
Joe C1 Inf 7Per 0.05 3 KSC2, QYC2
Notice I enclosed Name, Desc, and Err with square brackets every place they were referenced in the query. All are reserved words (see Problem names and reserved words in Access). Choose different names for those fields if possible. If not, use the square brackets to avoid confusing the db engine.
But this will not work unless/until your copy of the ConcatRelated function is recognized by your data base engine. I don't understand why it's not; I followed the same steps you listed for storing the function code, and this works fine on my system.
Edit: I tested that query with my version of the table, which has [Err] as a numeric data type. Sounds like yours is text instead. In that case, I'll suggest you change yours to numeric, too. I don't see the benefit of storing numerical values as text instead of actual numbers.
However if you're stuck with [Err] as text, you can adapt the query to deal with it. Change this ...
"[Err] > 0 AND [Name] = '" & sub.[Name]
to this ...
"Val([Err]) > 0 AND [Name] = '" & sub.[Name]
That change prevented the "Data type mismatch in criteria expression" error when I tested with [Err] as text data type. However, I also changed this ...
Sum(q.[Err]) AS SumOfErr
to this ...
Sum(Val(q.[Err])) AS SumOfErr
AFAICT that second change is not strictly necessary. The db engine seems willing to accept numbers as text when you ask it to Sum() them. However I prefer to explicitly transform them to numerical values rather than depend on the db engine to make the right guess on my behalf. The db engine has enough other stuff to deal with, so I try to tell it exactly what I want.
Edit2: If you want only unique values concatenated, you can modify the ConcatRelated() function. Find this section of the code ...
'Build SQL string, and get the records.
strSql = "SELECT " & strField & " FROM " & strTable
and change it to this ...
'Build SQL string, and get the records.
strSql = "SELECT DISTINCT " & strField & " FROM " & strTable

Mysql multiple tables select

I've got a table, called for example, "node", from which I need to return values for as shown:
SELECT nid FROM node WHERE type = "book"
After I get a list of values let's say:
|**nid**|
|123|
|12451|
|562|
|536|
Then I need to take these values, and check another table, for rows where column 'path' has values as "node/123", "node/12451" (numbers the previous request returned) in one joined request. It all would be easier if collumn 'path' had simple numbers, without the 'node/'.
And then also count the number of identical i.e. 'node/123' returned.
End result would look like:
nid | path | count(path) | count(distinct path)
123 |node/123| 412 | 123
562 |node/562| 123 | 56
Works fine if done in multiple separated queries, but that won't do.
select a.nid from node a join othertable b
on b.path = concat("node/", a.nid) where type='book'
You can probably do something like the following (nid may require additional conversion to some string type):
SELECT *
FROM OtherTable
JOIN node ON path = CONCAT('node/', nid)
WHERE type = 'book'
Thank you all for your help. Basically, the problem was that I didn't know how to get nid and node/ together, but concat helped.
End result looks something like:
SELECT node.nid, accesslog.path, count(accesslog.hostname), count(distinct accesslog.hostname)
FROM `node`, `accesslog`
WHERE node.uid=1
AND node.type='raamat'
AND accesslog.path = CONCAT('node/', node.nid)
GROUP BY node.nid