Hoping someone can help me on this. I have two tables where I am trying to grab a field from one table if it contains a value.
Table 1
Value Level
Officer C-Level
Exec C-Level
Table 2
Title Level2
Chief Executive C-Level (desired output)
Info Officer C-Level (desired output)
Oper Officer C-Level (desired output)
Essentially, in table 2, if the "Title" field contains a value from Table 1, then I would want the "Level" from table 1 to be populated in Table 2.
I am essentially looking for 'value' appearing anywhere in 'title'.
When trying to a Dlookup query, I see no results.
Level2: DLookup ("[Level]", "Table1", "[Title]" Like [Value])
I am missing something but not sure what.
As #June7 proposed, or:
SELECT *
FROM Tbl1 AS T1 inner join
Tbl2 AS T2
on T2.Title like '*' & T1.Value & '*'
But this query has a high chance of returning more than one Tbl1 row for any given Tbl2 row (hypothetical example: 'manager' would match both 'senior manager', and 'executive manager'), in these cases DLookup function returns the first occurrence.
For DLOOKUP you might use:
Level2: DLookUp("[Level]","Table1", "InStr('" & Table2.[Title] & "', [Value]) > 0")
Or
Level2: DLookUp("[Level]","Table1","'" & Table2.[Title] & "' like '*'&[Value]&'*'")
Related
Scenario is like this:
list of different types of insurance [ID, name, desc]
each insurance has a different unique table. [ID, user_id, ]
I want to query to show the columns of Insurance like [ID, Name, DESC] and a new column to show whether this user has applied for this insurance or not. No need to worry for the user part.
Just guide me how can I sub-query with dynamic table name.
I tried making a setup table where each insurance maps to its table name. But in my sub query how to do that.
If user has applied then show 1 otherwise 0.
SELECT i1.ID,
i1.name,
i1.desc,
IF(true, 1, 0) AS EXIST
#(SELECT t1.c_user_id FROM #tbl_name t1 WHERE t1.id = '101')
FROM app_fd_pdrm_insur_type i1;
Please, guide me what to replace with true.
Thank you.
Try this:
SELECT i1.ID, i1.name, i1.desc, IF( EXISTS(SELECT t1.c_user_id FROM #tbl_name t1 WHERE t1.id = '101'), 1, 0) as Exists FROM app_fd_pdrm_insur_type i1;
I'm writing a ticket program in vb.net using MySQL.
What I want is to use the Statename that a ticket can have (open/closed/on hold/etc) that I get from query 1, and use it in query 2 to get a list of all statenames, with the result of the first query as first item.
I now use 2 queries after each other, but i'm sure it can be done in 1 query.
Query 1:
SELECT StateName
FROM Tickets t
JOIN States s
ON s.stateID = t.StateID
WHERE TicketNumber = & *intTicketNumber*
--> Result = "Open"
Query 2:
SELECT StateName
from States
ORDER
BY(case when StateName = '" & *StrStateName* & "' then 0 else 1 end)
, StateName desc
The Desired result of the combined query should be:
open
assigned
closed
on hold
How Can I combine there 2 queries?
Any help would really be appreciated!
You can instead make States as your Leftmost table and do a LEFT JOIN to the Tickets table, on stateID and TicketNumber. Match the TicketNumber in the Join condition, instead of Where.
Making States as leftmost table will ensure that all the StateName values will be considered.
For a particular TicketNumber, t.StateId will be NULL (post LEFT JOINing), except for one StateName (assigned to the Ticket currently).
You can them simply sort on the t.StateId in Descending order. Ticket's current StateName will appear first in the list and rest of them sorted afterwards.
Try with Left join instead:
SELECT s.StateName
FROM States AS s
LEFT JOIN Tickets AS t
ON s.stateID = t.StateID AND
t.TicketNumber = $intTicketNumber -- $intTicketNumber is the input variable
ORDER BY t.StateID DESC,
s.StateName DESC
I have two tables
[data] - title,maker,partnum,price
[cross] - product(data.partnum),title,maker,partnum,price
What I want is listing all product via sysn number. How can I get with union all data like that with ordering ->
[data table] Microsoft, "some note", 9989, $20
[cross table] Microsoft reseller, "some note", 1045, $30
[cross table] Apple reseller in Microsoft :), "some note", 2233, $40
virtual spacer :)
[data table] Microsoft, "some note", 9989, $10
[cross table] Lenovo reseller in Microsoft..
Im trying with this
SELECT `title`,'Microsoft' AS `maker`,`partnum`,`price`
FROM data as d
WHERE sysn=%s
GROUP BY partnum
UNION ALL
SELECT `title`,`maker`,`partnum`,`price`
FROM cross as c
WHERE c.product=d.partnum
GROUP BY `partnum`
Thanks
It's not entirely clear what you are asking. Consider setting up an example schema and data in http://sqlfiddle.com.
A "virtual spacer" is going to be very messy to achieve. It's not totally impossible, just messy.
For returning rows in a particular sequence, you are going to need to add an ORDER BY clause.
It's not at all clear why you are including GROUP BY clauses. The GROUP BY on the first SELECT would effectively "collapse" example output rows 1. and 5. given the same value 9989 for partnum. Absent an explanation of why you need a GROUP BY, I'm going to guess that what you really intended was an ORDER BY clause.
It's also not clear why the literal value 'Microsoft' would need to appear in the SELECT list, in place of the maker column from the data table. There's nothing invalid SQL-wise with doing that. But absent an explanation, it just doesn't make much sense.
The question we should to ask... Why is this specific result needed? Is there a different result which would be easier to achieve, which would equally satisfy the requirements?
Setting aside the "virtual spacer" row, the return from a query like this seems like it satisfies most of the specification:
SELECT t.src
, t.title
, t.maker
, t.partnum
, t.price
FROM (
SELECT '[data]' AS `src`
, d1.title AS `title`
, d1.maker AS `maker`
, d1.partnum AS `partnum`
, d1.price AS `price`
, d1.partnum AS `product`
FROM data d1
WHERE d1.sysn = ?
UNION ALL
SELECT '[cross]'
, c2.title
, c2.maker
, c2.partnum
, c2.price
, c2.product
FROM cross c2
JOIN data d2
ON d2.partnum = c2.product
WHERE d2.sysn = ?
)
ORDER BY t.product DESC, t.src DESC, t.price ASC
If you are using MySQL, "cross" is a reserved word and you must enclose cross in backticks to use it as a table name (The backtick is next the the '1' on the keyboard on the upper left).
I have two tables with "Field Name" columns. Some Table B field names are the same as Table A field names. If that is the case, I want to exclude those from the combobox so I don't have a double (I only want the Table A field name in that case). I also need the ID's (unique to each table) in the combobox.
I can't seem to come up with the right SQL logic. Right now, I'm trying the following
SELECT [fldID], [fldName] FROM OISInfo UNION
(SELECT [ID], [Field Name] FROM FldDef
LEFT JOIN OISInfo ON [Field Name] = [fldName] WHERE [fldName] IS NULL)
but Access keeps telling me that the join expression is not supported (in the bracketed part). The table names are definitely correct.
What am I doing wrong?
Tested.Worked perfectly. Table5 is your table A or maybe OISInfo. Table 6 is your table B (FldDef)
SELECT Table5.ID, Table5.Field1
FROM Table5
UNION
SELECT Table6.ID, Table6.Field1
FROM Table6 LEFT JOIN Table5 ON Table6.[Field1] = Table5.[Field1]
WHERE (((Table5.Field1) Is Null));
Union takes cares of doubles, this is all you have to do
SELECT [fldID], [fldName] FROM OISInfo
UNION
SELECT [ID], [Field Name] FROM FldDef
I have a table record source (contains tons of info), and I have a table that contains 1200 records. I would like to append into this destination table (containing 1200 records) any records that currently do not exist. the criteria for my items to be appended is:
"Not In ([TABLE - To Work].[Item Number])"
Problem is, it is returning the record I want, back 1200 times, instead of once.
For Example:
Table A: Table B:
Item Number Item Number
12345 45678
45678
"12345" would append into table B only once (and then never append again!) I looked for a few solutions, and I tried using the unmatched query wizard, but I do not think it was really what I Wanted (It generated where the number is null). What do I need to do to make this sort of look at entire scope of the table and not item by item (I assume thats why it is populating the same number of times as existing records)? What step am I leaving out?.
The general form of your query will be something like
INSERT INTO [Table B] ( [Item Number] )
SELECT [Table A].[Item Number]
FROM [Table A]
WHERE [Table A].[Item Number] NOT IN (SELECT [Item Number] FROM [Table B]);
Note that [Table B] is not in the FROM clause of the main query, it is only in the FROM clause of the NOT IN subquery.
This worked for me:
INSERT INTO [Table B] ( [Item Number] )
SELECT DISTINCT [Table A].[Item Number]
FROM [Table A] LEFT JOIN [Table B] ON [Table A].[Item Number] = [Table B].[Item Number]
WHERE ((([Table B].[Item Number]) Is Null));