MS Access 07 - wildcard in JOIN query - join

I've got the following query
SELECT tblUsers.userfullname,
tblReports.reportdate,
tblReports.reportnumber,
tblRawData.reportcategory,
tblRawData.reportissue
FROM tblRawData
RIGHT JOIN (tblUsers RIGHT JOIN tblReports ON tblUsers.userID = tblReports.userID) ON tblReports.reportnumber LIKE "*" & tblRawData.reportnum
WHERE (
((tblUsers.username) Like "*" & [Forms]![frmSelect]![txtUser] & "*")
AND
((tblUsers.userShift) Like "*" & [Forms]![frmSelect]![txtShift] & "*")
);
Which works - except the part of
ON tblReports.reportnumber LIKE "*" & tblRawData.reportnum
what i'm trying to match is instances where
tblReports.reportnumber = 410145
and
tblRawData.reportnum = 12345.410145
or just
tblRawDatw.reportnum = 410145
but for some reason it just will not find that first match (ex: 12345.410145) unless the number is identical like the second match (ex: 410145). I've tried formatting it as a number as well as text - and no luck.
any idea what I may be missing?
Update: I tried making another query with just the two tables and it doesn't like to match. i tried removing the "." (example: 12345.410145 into 12345410145) and no luck. here's my second query.
SELECT tblReports.userID,
tblRawData.reportnum,
tblRawData.reportcategory,
tblRawData.reportissue,
tblReports.reportdate,
tblReports.reportnumber
FROM tblReports
LEFT JOIN tblRawData ON tblReports.reportnumber LIKE "*" & tblRawData.reportnum;
where if the data is like such.
tblReports Report numbers:
410145
410144
410143
410142
410141
and tblRawData report numbers are such:
12345.410145
410143
12344.410141
the resulting query should show me all 5 records from tblReports - but three of those records have the notes and such from tblRawData.

Rewritten to allow for no match.
SELECT
u.userfullname,
r.reportdate,
r.reportnumber,
q.reportcategory,
q.reportissue
FROM (tblusers u
LEFT JOIN tblreports r
ON u.userid = r.userid)
LEFT JOIN (
SELECT
Val(Mid([reportnum],InStr([reportnum],".")+1)) AS RepNo,
r.reportcategory,
r.reportissue
FROM Rawdata AS r) AS q
ON r.reportnumber = q.RepNo
AND q.username Like "*" & [Forms]![frmSelect]![txtUser] & "*"
AND q.userShift Like "*" & [Forms]![frmSelect]![txtShift] & "*"
It is a common enough convention that LEFT JOINs are used rather than RIGHT JOINs.

I didn't check everything, but I have some ideas. Maybe you have to swap the LIKE condition?
tblRawData.reportnum LIKE "*" & tblReports.reportnumber
if that's not the problem, could you try to use the trim function?
Trim(tblReports.reportnumber) LIKE "*" & Trim(tblRawData.reportnum)

The wildcard character for SQL queries is the percent sign '%' not the asterisk. Try using that instead. But if you are including every match then why have that in your query at all?
Access wildcard character reference

Related

Joining on column names with spaces

I'm trying to join to tables using PROQ SQL. One of the columns I'm using for the join has a space in the column name. The query I'm using:
PROC SQL;
CREATE TABLE TEST AS
SELECT a.*, b.*
FROM TABLE_1 a
INNER JOIN TABLE_2 b
ON a.CONTNO = b."Contract Number";
RUN;
This is the error I'm getting:
ERROR 22-322: Syntax error, expecting one of the following: a name, *.
How do I fix this?
You just need to add square brackets around the Column name. For example:
b.[Contract Number]
Tips: Using alias (a, b) can be costly. When you only have one table to join, consider typing out the table rather than doing an alias.

Run query where column = *

Need to run a query on google sheets where the column match any(*) value. I'm doing this because the criteria is filled by refering to another cell (like a filter).
Probably I'm missing something in the syntax.
My last try:
=QUERY(PROD!A:U;"SELECT L, SUM(M), SUM(O), SUM(Q), (1-(SUM(Q)/SUM(O)))
WHERE T = '*' AND D = '*' AND U = '*'
GROUP BY L
ORDER BY SUM(M) DESC
LABEL L 'PRODUTO', SUM(M) 'QUANTIDADE', SUM(Q) 'CUSTO', SUM(O) 'VENDA', (1-(SUM(Q)/SUM(O))) 'MARK-UP'")
Thank you in advance.
In Google Visualization API Query Language (not to be confused with T-SQL, MySQL, etc) the clause WHERE T = '*' means that the content of column T is literally the string *.
To test for a cell being nonempty, use T <> '' (for text columns) or T is not null (for numeric columns).
There are also like and matches for more complex text filtering.

Using distinct in a join

I'm still a novice at SQL and I need to run a report which JOINs 3 tables. The third table has duplicates of fields I need. So I tried to join with a distinct option but hat didn't work. Can anyone suggest the right code I could use?
My Code looks like this:
SELECT
C.CUSTOMER_CODE
, MS.SALESMAN_NAME
, SUM(C.REVENUE_AMT)
FROM C_REVENUE_ANALYSIS C
JOIN M_CUSTOMER MC ON C.CUSTOMER_CODE = MC.CUSTOMER_CODE
/* This following JOIN is the issue. */
JOIN M_SALESMAN MS ON MC.SALESMAN_CODE = (SELECT SALESMAN_CODE FROM M_SALESMAN WHERE COMP_CODE = '00')
WHERE REVENUE_DATE >= :from_date
AND REVENUE_DATE <= :to_date
GROUP BY C.CUSTOMER_CODE, MS.SALESMAN_NAME
I also tried a different variation to get a DISTINCT.
/* I also tried this variation to get a distinct */
JOIN M_SALESMAN MS ON MC.SALESMAN_CODE =
(SELECT distinct(SALESMAN_CODE) FROM M_SALESMAN)
Please can anyone help? I would truly appreciate it.
Thanks in advance.
select distinct
c.customer_code,
ms.salesman_code,
SUM(c.revenue_amt)
FROM
c_revenue c,
m_customer mc,
m_salesman ms
where
c.customer_code = mc.customer_code
AND mc.salesman_code = ms.salesman_code
AND ms.comp_code = '00'
AND Revenue_Date BETWEEN (from_date AND to_date)
group by
c.customer_code, ms.salesman_name
The above will return you any distinct combination of Customer Code, Salesman Code and SUM of Revenue Amount where the c.CustomerCode matches an mc.customer_code AND that same mc record matches an ms.salesman_code AND that ms record has a comp_code of '00' AND the Revenue_Date is between the from and to variables. Then, the whole result will be grouped by customer code and salesman name; the only thing that will cause duplicates to appear is if the SUM(revenue) is somehow different.
To explain, if you're just doing a straight JOIN, you don't need the JOIN keywords. I find it tends to convolute things; you only need them if you're doing an "odd" join, like an LEFT/RIGHT join. I don't know your data model so the above MIGHT still return duplicates but, if so, let me know.

concatenate values from two columns and compare it against one value in sqlite

I am using sqlite in one my ios project where among other columns there are two columns. one has country code for mobile dialing like +880 and other column has rest of the mobile number like 1912353697. Now i want to join values from these two columns and compare the result value against like +8801912353697 and if matches pull the corresponding name value in that table . what would be the right query. I have tried like SELECT name FROM CONTACT_LIST WHERE (SELECT mblDc || mbl FROM CONTACT_LIST) = "+8801617634317" ; but that does not work .
A help would be appreciated.
Try:
SELECT name FROM CONTACT_LIST
WHERE mblDc || mbl = "+8801617634317";
From SQLite documentation: The || operator is "concatenate" - it joins together the two strings of its operands.
please check
How to concatenate strings with padding in sqlite
using substr function we can do the job and the query should be like
SELECT name FROM CONTACT_LIST WHERE (mblDc || mbl) = "+8801617634317" ;

SQLite 3 LEFT JOIN where 'IN' is search term

I've got a database table that needs to be joined to a table that contains countries. The LEFT JOIN is done on country ISO code and is as follows:
SELECT table1.city, table2.Country, table2.Flag
FROM table1
LEFT JOIN table2
ON (table2.ISO = table1.country)
WHERE table1.id = ?
This goes pretty well unfortunately the ISO code for India is 'IN'. This is the country that fails to provide the country data and I think this is because IN is a reserved word for SQL. But how can I preform this query anyway?
Thanks
Ron
The use of a reserved word in the data should not affect the results at all. Reserved words are in issue when compiling the query, not when running it.
If your query is failing for India, then there is some other problem.

Resources