Updating null field with the value in the same column A based on the value of another column B where the value of B is the same - ms-access

title
genre
band
wish you were here
rock
Pink Floyd
Have a cigar
Pink Floyd
Time
Pink Floyd
How do I update the genre column based on the band column so that it looks like this:
title
genre
band
wish you were here
rock
Pink Floyd
Have a cigar
rock
Pink Floyd
Time
rock
Pink Floyd
This is what I have so far:
UPDATE [music] INNER JOIN [music] AS [music_1] ON [music].[band] = [music_1].[band] SET [music].[genre] = [music_1].[genre]
WHERE ((([music].[genre]) Is Null));

Try with DLookup:
Update
Music
Set
genre = Nz(DLookup("genre", "Music", "band = '" & music.band & "' And genre Is Not Null"), "<Unknown>")
Where
genre Is Null
It may be slow ...

Related

Access form checkbox AND OR statements

Recently I started creating a form to filter the database by using checkboxes.
I have the following data:
Category Type Country
Fruit Apple NL
Fruit Apple SU
Fruit Banana NL
Fruit Banana DE
Nuts Cashew NL
Nuts Cashew US
Nuts Almond UK
Nuts Almond GR
Now I build in my form the checkboxes for Fruit & Nuts, for Apple, Banana, Cashew & Almond and for NL, SU, DE, US, UK & GR.
At this moment I use only the OR statement in my SQL script, which works well. But if the user would like to know for example which fruit comes from NL and it marks both Fruit and NL then my output will be everything from Fruit and everything from NL (because of the OR statement).
How should I change my script that the filter is used based on the checkboxes that are checked? In my example: if the user checks both Fruit & NL then it will give the output --> Fruit Apple NL & Fruit Banana NL
Or another example: He wants Banana & Cashew, and gets as output --> Fruit Banana NL & Fruit Banana DE & Nuts Cashew NL & Nuts Cashew US
I hope my problem is clear; if not, do not hesitate to ask for clarification!
From what I understand you have a form with 2 Category check boxes, 4 Type check boxes and 6 Country check boxes.
It doesn't sound like any of these filter themselves, so it's possible try and return Nuts, Apple and UK which would return an empty table.
I think you'll have to make extensive use of the LIKE command so any NULL/FALSE values can be converted to *.
As a checkbox returns TRUE/FALSE rather than Apple/False an IIF statement can be used to change TRUE to Apple (you could probably use CHOOSE or SWITCH instead).
So the WHERE clause here says chkFruit is either Fruit or anything.
SELECT Category, Type, Country
FROM Table2
WHERE Category LIKE (IIF([Forms]![Form1]![chkFruit],'Fruit','*')) AND
Category LIKE (IIF([Forms]![Form1]![chkNuts],'Nuts','*')) AND
Type LIKE (IIF([Forms]![Form1]![chkApple],'Apple','*')) AND
Type LIKE (IIF([Forms]![Form1]![chkBanana],'Banana','*')) AND
Type LIKE (IIF([Forms]![Form1]![chkCashew],'Cashew','*')) AND
Country LIKE (IIF([Forms]![Form1]![chkNL],'NL','*')) AND
Country LIKE (IIF([Forms]![Form1]![chkSU],'SU','*')) AND
Country LIKE (IIF([Forms]![Form1]![chkDE],'DE','*'))
Edit:
Reading your comment below it sounds like you want it to filter by the ticked boxes, or if no boxes are ticked then show all of them (as if all boxes in the Category/Type or Country were ticked).
That could get quite complicated if you have lots of tick boxes.
So each section of check boxes needs breaking down to something similar to below.
WHERE ((
Category = IIF([Forms]![Form1]![chkFruit],'Fruit',NULL) OR
Category = IIF([Forms]![Form1]![chkNuts],'Nuts',NULL)
) OR IIF(NOT [Forms]![Form1]![chkFruit] AND
NOT [Forms]![Form1]![chkNuts],Category LIKE '*',NULL))
The full SQL would be:
SELECT Category, Type, Country
FROM Table2
WHERE ((
Category = IIF([Forms]![Form1]![chkFruit],'Fruit',NULL) OR
Category = IIF([Forms]![Form1]![chkNuts],'Nuts',NULL)
) OR IIF(NOT [Forms]![Form1]![chkFruit] AND
NOT [Forms]![Form1]![chkNuts],Category LIKE '*',NULL))
AND
((
Type = IIF([Forms]![Form1]![chkApple],'Apple',NULL) OR
Type = IIF([Forms]![Form1]![chkBanana],'Banana',NULL) OR
Type = IIF([Forms]![Form1]![chkCashew],'Cashew',NULL) OR
Type = IIF([Forms]![Form1]![chkAlmond],'Almond',NULL)
) OR IIF(NOT [Forms]![Form1]![chkApple] AND
NOT [Forms]![Form1]![chkBanana] AND
NOT [Forms]![Form1]![chkCashew] AND
NOT [Forms]![Form1]![chkAlmond],Type LIKE '*',NULL))
AND
((
Country = IIF([Forms]![Form1]![chkNL],'NL',NULL) OR
Country = IIF([Forms]![Form1]![chkSU],'SU',NULL) OR
Country = IIF([Forms]![Form1]![chkDE],'DE',NULL) OR
Country = IIF([Forms]![Form1]![chkUS],'US',NULL) OR
Country = IIF([Forms]![Form1]![chkUK],'UK',NULL) OR
Country = IIF([Forms]![Form1]![chkGR],'GR',NULL)
) OR IIF(NOT [Forms]![Form1]![chkNL] AND
NOT [Forms]![Form1]![chkSU] AND
NOT [Forms]![Form1]![chkDE] AND
NOT [Forms]![Form1]![chkUS] AND
NOT [Forms]![Form1]![chkUK] AND
NOT [Forms]![Form1]![chkGR], Country LIKE '*',NULL))
I've got a feeling this could be shortened quite a bit but it's starting to hurt my head now.

Aurora:Find diff between two tables based on primary key

I have TABLE1 like below
|001000900|aaaaa BELLOWS CORPORATION||N|
|001000905|ddddd DEPARTMENT OF LABOR AND EMPLOYMENT SECURITY|BUREAU OF COMPLIANCE|N|
|001001049|gggg RAVIOLI MFG CO INC|SPINELLI BKY RAVIOLI PASTRY SP|N|
|001001130|dddd ANGELES UNIFIED SCHOOL DISTRICT|TRANSPORTATION BRANCH|N|
|001001143|ffff MUSIC PARTIES, INC||N|
|001001155|BOSTON BRASS AND IRON CO||N|
|001001171|HANCOCK MARINE, INC.||N|
|001001184|TRILLION CORPORATION||N|
|001001192|HAWAII STATE CHIROPRACTIC ASSOCIATION INC||N|
|001001379|THE FRUIT SQUARE PEOPLE INC|L & M BAKERY|N|
|001001416|J & S MARKET||N|
And Table2 like this
|001000145|PARADISE TAN||N|
|001000306|SHRUT & ASCH LEATHER COMPANY, INC.||N|
|001000355|HARRISON SPECIALTY CO., INC.||N|
|001000363|LOUIS M. GERSON CO., INC.||N|
|001000467|SAVE THE SEA TURTLES INTERNATIONAL|ADOPT THE BEACH HI|N|
|001000504|DIRIGO SPICE CORPORATION|CUNNINGHAM SPICE|N|
|001000744|FREEDMAN THREAD COMPANY|COLONIAL THREAD CO|N|
|001000756|AFFORDABLE AIR CONDITIONING|P R ENTERPRISE|N|
|001000900|CLIFLEX BELLOWS CORPORATION||N|
|001000905|FLORIDA DEPARTMENT OF LABOR AND EMPLOYMENT SECURITY|BUREAU OF COMPLIANCE|N|
|001001049|SPINELLI RAVIOLI MFG CO INC|SPINELLI BKY RAVIOLI PASTRY SP|N|
|001001130|LOS ANGELES UNIFIED SCHOOL DISTRICT|TRANSPORTATION BRANCH|N|
|001001143|TOSCO MUSIC PARTIES, INC||N|
|001001155|BOSTON BRASS AND IRON CO||N|
My requirement is to find the rows which is present in both tables (Matching rows) based on primary key which is first column only if their others columns are changed.
For example if primary key is same and their other columns are different at least one then that will come in output .
How can i do this .
Plain except will give me result ?
Are you looking for a join with a where condition?
select t1.*, t2.*
from table1 t1 join
table2 t2
on t1.pk = t2.pk
where not (t1.col2 <=> t2.col2) or
not (t1.col3 <=> t2.col3) or
not (t1.col4 <=> t2.col4) ;

SQL - how to skip a row if it contains similar information when compared to the previous row?

my table looks like this:
ID# Type Color
1. Clothes Red
2. Clothes Orange
3. Clothes OrangeYellow
4. Clothes Green
5. Clothes Red
Desired result:
ID# Type Color
1. Clothes Red
2. Clothes Orange
4. Clothes Green
5. Clothes Red
My SQL doesn't seem to support LAG function
Thanks in advance.
You could use this subquery approach using LIKE with % previous-color %:
SELECT c.ID, c.Color
FROM dbo.Colors c
WHERE NOT EXISTS -- check if previous row has a similar color
(
SELECT 1 FROM dbo.Colors cPrev
WHERE cPrev.ID = (SELECT MAX(cMax.ID)
FROM dbo.Colors cMax
WHERE cMax.ID < c.ID) -- maximum id lower than this
AND c.Color LIKE '%' + cPrev.Color + '%'
)

How to select an item that matches all attributes in a table in SQL?

I have a table (Areas) that contains a list of areas.
**Area**
Area 1
Area 2
Area 3
Then, I have another table (People) with a list of names and areas.
**PersonName** **Area**
Bob Area 1
Dylan Area 4
Jimi Area 6
Hendrix Area 2
Bob Area 3
Bob Area 2
Jimi Area 3
I want to find all names that have ALL areas in the Areas table associated with them. So for the example data above, I would get the output Bob.
My current query returns the names of people that teach ANY of the areas in the Areas table.
SELECT DISTINCT PersonName
FROM People, Areas
WHERE Areas.Area = People.Area
Which returns Bob, Hendrix, Jimi
How can I change this to select only Bob, who has all three areas associated with him?
You can use group by and having. Assuming that all the areas in the People table are valid:
select p.personname
from people p
group by p.personname
having count(distinct area) = (select count(distinct area) from areas);
If you know there are no duplicate areas for a person and no duplicates in areas, then you can replace the count(distinct) with count().

Mysql Join Two Tables With Recycling

Suppose I have two tables:
zoo_tbl
name
------
dog
cat
monkey
lion
tiger
elephant
fish
palette_tbl
rgb
------
pink
yellow
green
I want to do a join on the two tables such that the rgb rows repeat in a cycle.
name rgb
---------------------
dog pink
cat yellow
monkey green
lion pink
tiger yellow
elephant green
fish pink
How should I build this query? I have an idea to create a large temporary table with recurring rgb values before joining, but even if I do that, I would have to enumerate the zoo_tbl and the temporary table before doing a join. There has to be a simpler/more direct way to do this...
Assuming you have PRIMARY KEY on column id of table palette_tbl, this would do the trick for you:
SELECT name,
(SELECT rgb FROM palette_tbl WHERE id = #row_id) AS rgb,
(#row_id:= IF(#row_id = #cnt, 1, #row_id + 1)) AS dummy_id
FROM zoo_tbl a, (SELECT #row_id:= 1, #cnt := (SELECT COUNT(1) FROM palette_tbl)) dummy;
SQLFIDDLE DEMO HERE