Showing posts with label lookup. Show all posts
Showing posts with label lookup. Show all posts

Wednesday, March 28, 2012

query (deals with exclusions)

I have 2 tables. The first table is a master table with 3 fields: record id, list, and value. The second table is a lookup table that has the list and value in it, as well as fieldname. I need a query that will return a count of records in the master table that do not exist in the lookup table based on list and value. It seems straightforward but my brain doesn't seem to be working. I keep returning the count of records that don't match in the lookup table, instead of the master table. can anyone help? This was my code that isn't working:

select m.list, m.value, count(*)
from master m, lookup l
where m.list = l.list and
m.value <> l.value
and fieldname = 'BOC'
group by m.list, m.value

Thanks in advance.If the columns LIST and VALUE are the joining columns between the MASTER table and the LOOKUP table then try this query:

SELECT COUNT(*)
FROM MASTER m
WHERE NOT EXISTS
(
SELECT *
FROM LOOKUP l
WHERE l.LIST = m.LIST
AND l.VALUE = m.VALUE
)

query

I have a query that I need help on. The cc_division table is a lookup. In my results I want to display all divisions regardless of the results. The statement works fine except I am limiting my results in the where clause. Can anyone tell me how I could do the exact thing below so that I can return all of the items in my lookup?
Thanks
Here is my SQL statement:
SELECT DISTINCT dbo.cc_division.division, dbo.cc_division.division_id, COUNT(dbo.cc_employee.employee_key) AS total
FROM dbo.cc_employee RIGHT OUTER JOIN
dbo.cc_division ON dbo.cc_employee.division_id = dbo.cc_division.division_id
WHERE (dbo.cc_employee.employee_key NOT IN
(SELECT employee_key
FROM cc_card
WHERE active = 1))
GROUP BY dbo.cc_division.division, dbo.cc_division.division_id
One way to do it would be like this:
SELECT DISTINCT dbo.cc_division.division, dbo.cc_division.division_id, COUNT(cc_employee.employee_key) AS total
FROM (SELECT *FROM dbo.cc_employee WHERE dbo.cc_employee.employee_key NOT IN
(SELECT employee_key
FROM cc_card
WHERE active = 1)) AS cc_employee RIGHTOUTER JOIN
dbo.cc_division ON cc_employee.division_id =dbo.cc_division.division_id
GROUP BY dbo.cc_division.division, dbo.cc_division.division_id
I haven't tested it so there might be syntax errors.
|||perfect thank you