I had (see below) a table of (fictional) stars and then two tables showing its equally fictional satellites which could be planets or asteroid belts. tblStars, tblAsteroids and tblPlanets respectively. Each of the two satellite tables had a position field which was unique across the two tables - by this I mean that the star with ID 1 had only one satellite in position 1, 2 etc which could have been in either of those two tables but not both. I wanted to sort the satellites in order of position on my reports but couldn't see a way of sorting across the combination of those fields:
tblAsteroid:
Asteroid ID Position
1 1
2 3
tblPlanet:
Planet ID Position Biome
1 2 Ice
Giving:
Position AsteroidOrPlanet Biome
1 Asteroid N/A
2 Planet Ice
3 Asteroid N/A
For the avoidance of doubt, I recognise that this problem was caused by a flaw in my database design and I should have had a tblSatellite which contained that position and was in a 1 to many with tblStar and in 1 to 0-1's with tblAsteroid and tblPlanet. I've since fixed this, I'm just wondering if it would have been possible.
To get a combined list, you need a UNION query anyway. This you can sort by a common field.
SELECT Position, 'Asteroid' AS AsteroidOrPlanet, 'N/A' AS Biome
FROM tblAsteroid
UNION ALL
SELECT Position, 'Planet' AS AsteroidOrPlanet, Biome
FROM tblPlanet
ORDER BY Position
Related
There are two id descriptions which are similar that is have say 80% similarity. I need both to be given same id.
There are other id descriptions having say 60% similarity. These should retain their own ids. Once an id desc has been considered and modified, it should not be taken as a reference. Further
example:
id id description
1 pepsodent
2 pepsodent salt
3 pepsod
4 pepsodent and salt
5 peps
Now, pepsodent matches with pepsodent salt.therefor both should be given id as 1
Now as pepsodent salt has already been modified,it cannot be used as a scale of reference further.
As I said in my comment above, you need to define exactly what the rules are for matching two records. In this example, I am giving the a New ID to any records that contain the entire string 'pepsodent'. The New ID for these records will be 999 but you can modify as you see fit:
SELECT ID, ID_Description,
CASE
WHEN ID_Description LIKE 'Pepsodent%' THEN 999
ELSE ID
END AS New_ID
FROM Table
I want to structure a table to mimic column level filters as row level filter just to avoid adding new columns.
Let's say i have following table to store cars' details
-------------------------------------
Type Color Year
-------------------------------------
Mini Silver 2010
Standard Silver 2011
Fullsize White 2011
Luxury Black 2010
Sports Red 2011
Convertible Red 2009
If i want to store Make of these cars as well and for this i have to add an additional column and another column if i have automobiles other than cars.
So the question is how can i structure this table to avoid adding new columns? The structure should require only to add rows to define properties of my records.
[Hint] The structure may have multiple tables, one to store rows/records and other to store columns/properties and then some kind of mapping between them OR entirely new structure.
EDIT
Some of the properties of my data are fixed and some are dynamic. Fixed properties can be mapped to the given sample Car model as Availability, Condition and the dynamic could be anything which a person may ask about an automobile. Now i don't need all columns to be mapped as rows but few and these are dynamic and i don't even know all of them. My apologies that i didn't mention this earlier.
You could use the entity-attribute-value design (EAV).
entity attribute value
1 Type Mini
1 Color Silver
1 Year 2010
1 Make Foobar
2 Type Standard
2 Color Silver
etc...
You may also wish to store the attribute names in a separate table.
However you should consider carefully if you really need this, as there are a few disadvantages. The value column must have a type that can store all the different types of values (e.g. string). It is much more cumbersome to write queries as you will need many joins, and these queries will run more slowly as compared to a traditional database design.
To give you a head start: Think about redesigning to allow multi-colored vehicles like motorbikes:
vehicle
Id Year vehicle_type vehicle_make
-------------------------------------------
1 2010 1 1
2 2011 2 2
color
Id Name
-----------
1 Black
2 White
3 Red
4 Blue
vehicle_color
vehicle_id color_id
-----------------------
1 3
2 1
2 2
vehicle_type
Id Name
-----------
1 Car
2 Motorbike
vehicle_make
Id Name
-----------
1 Porsche
2 BMW
Bonus
Since I'm quite familiar with the car domain, I'll throw in an extension for your vehicle colors: There are tons of color names ("Magentafuzzyorangesunset") invented by manufacturers and you'll want to map them to "real" base color names ("Red, "Blue", "Green", etc.) to enable searching for both.
Your color table then could look like that
Id Name base_color
-----------------------------
1 Midnight 1
2 Snow 2
and you'll add a base_color table
Id Name
-----------
1 Black
2 White
I have a question about updating multiple rows with different values in MS Access 2010.
Table 1: Food
ID | Favourite Food
1 | Apple
2 | Orange
3 | Pear
Table 2: New
ID | Favourite Food
1 | Watermelon
3 | Cherries
Right now, it looks deceptively simple to execute them separately (because this is just an example). But how would I execute a whole lot of them at the same time if I had, say, 500 rows to update out of 1000 records.
So what I want to do is to update the "Food" table based on the new values from the "New" table.
Would appreciate if anyone could give me some direction / syntax so that I can test it out on MS Access 2010. If this requires VBA, do provide some samples of how I should carry this out programmatically, not manually statement-by-statement.
Thank you!
ADDENDUM (REAL DATA)
Table: Competitors
Columns: CompetitorNo (PK), FirstName, LastName, Score, Ranking
query: FinalScore
Columns: CompetitorNo, Score, Ranking
Note - this query is a query of another query, which in turn, is a query of another query (could there be a potential problem here? There are at least 4 queries before this FinalScore query is derived. Should I post them?)
In the competitors table, all the columns except "Score" and "Ranking" are filled. We would need to take the values from the FinalScore query and insert them into the relevant competitor columns.
Addendum (Brief Explanation of Query)
Table: Competitors
Columns: CompetitorNo (PK), FirstName, LastName, Score, Ranking
Sample Data: AX1234, Simpson, Danny, <blank initially>, <blank initially>
Table: CompetitionRecord
Columns: EventNo (PK composite), CompetitorNo (PK composite), Timing, Bonus
Sample Data1: E01, AX1234, 14.4, 1
Sample Data2: E01, AB1938, 12.5, 0
Sample Data3: E01, BB1919, 13.0, 2
Event No specifies unique event ID
Timing measures the time taken to run 200 metres. The lesser, the better.
Bonus can be given in 3 values (0 - Disqualified, 1 - Normal, 2 - Exceptional). Competitors with Exceptional are given bonus points (5% off their timing).
Query: FinalScore
Columns: CompetitorNo (PK), Score, Ranking
Score is calculated by wins. For example, in the above event (E01), there are three competitors. The winner of the event is BB1919. Winners get 1 point. Losers don't get any points. Those that are disqualified do not receive any points as well.
This query lists the competitors and their cumulative scores (from a list of many events - E01, E02, E03 etc.) and calculates their ranking in the ranking column everytime the query is executed. (For example, a person who wins the most 200m events would be at the top of this list).
Now, I am required to update the Competitors table with this information. The query is rather complex - with all the grouping, summations, rankings and whatnots. Thus, I had to create multiple queries to achieve the end result.
How about:
UPDATE Food
INNER JOIN [New]
ON Food.ID=New.ID
SET Food.[Favourite Food] = New.[Favourite Food]
I have a cross tab query and it pulls only the row name if there is data associated with it in the database. For example, if I have three types of musical instruments:
Guitar
Piano
Drums
Other
My results will show up as:
Guitar 1
Drums 2
It doesn't list Piano because there is no ID associated with Piano in the DB. I know I can specify columns in the properties menu, i.e. "1, 2, 3, 4, 5" will put columns in the DB for each, regardless of whether or not there is data to populate them.
I am looking for a similar solution for rows. Any ideas?
Also, I need NULL values to show up as 0.
Here's the actual SQL (forget the instrument example above)
TRANSFORM Count(Research.Patient_ID) AS CountOfPatient_ID
SELECT
Switch(
[Age]<22,"21 and under",
[Age]>=22 And [AGE]<=24,"Between 22 And 24",
[Age]>=25 And [AGE]<=29,"Between 25 And 29",
[Age]>=30 And [AGE]<=34,"30-34",
[Age]>=35 And [AGE]<=39,"35-39",
[Age]>=40 And [AGE]<=44,"40-44",
[Age]>44,"Over 44"
) AS Age_Range
FROM (Research
INNER JOIN (
SELECT ID, DateDiff("yyyy",DOB,Date()) AS AGE FROM Demographics
) AS Demographics ON Research.Patient_ID=Demographics.ID)
INNER JOIN [Letter Status] ON Research.Patient_ID=[Letter Status].Patient_ID
WHERE ((([Letter Status].Letter_Count)=1))
GROUP BY Demographics.AGE, [Letter Status].Letter_Count
PIVOT Research.Site In (1,2,3,4,5,6,7,8,9,10);
In short, I need all of the rows to show up regardless of whether or not there is a value (for some reason the LEFT JOIN isn't working, so if you can, please use my code to form your answer), and I also need to replace NULL values with 0.
Thanks
I believe this has to do with the way you are joining the instruments table to the IDs table. If you use a left outer join from instruments to IDs, Piano should be included. It would be helpful to see your actual tables and queries though, as your question is kind of vague.
What if you union the select with a hard coded select with one value for each age group.
select 1 as Guitar, 1 as Piano, 1 as Drums, 1 as Other
When you do the transform, each row will have a result that is +1 of the result you want.
foo barTmpCount
-------- ------------
Guitar 2
Piano 1
Drums 3
Other 1
You can then do a
select foo, barTmpCount - 1 as barCount from <query>
and get something like this
foo barCount
-------- ---------
Guitar 1
Piano 0
Drums 2
Other 0
I want to implement graph coloring using databases.
There is a table that will store all the vertices (1,2,3...) and a table that stores the name of all colors(red,blue,green,etc..).
Now a want to create a coloring table with columns vertex and color which will take all possible combinations from the above tables and then check the constraints in each of those tables. Whichever table satisfies the constraints of graph coloring is a solution.
Now how to create tables for each combinations??
Guys please help. Stuck on it from a while...
An example instance:
vertex
1
2
3
Colors
red
blue
coloring
a)
1 red
2 blue
3 red
b)
1 red
2 red
3 blue
c)
1 blue
2 red
3 red
.
.
.
6 tables
I'm not sure I understand your question, so I'll make some assumptions. Assuming you have a table called Vertex, with the following rows:
1
2
3
... and a table called Color, with the following rows:
Red
Green
Blue
... you can generate a table of all possible combinations with a simple unconstrained join, like this:
SELECT *
INTO VertexColor
FROM Vertex, Color
The result will be a new table, with the following rows:
1, Red
1, Green
1, Blue
2, Red
2, Green
2, Blue
3, Red
3, Green
3, Blue
Happy to help further if this does not answer your question.
SELECT Vertices.vertex, Colors.Color from Vertices
CROSS JOIN Color from Colors
EDIT: Seeing the new comments: This doesn't sound like a problem that is well suited for SQL, mainly because your number of columns in your resultset is dependent on the number of rows in your vertices table. That's not something that is easy in SQL (you probably need a multistep process, using dynamic sql through sp_execute). Since the ordering of the colums carries significance, you can't return a resultset containing only each vertex - color pair either, because the order in which the rows are returned may vary. To me it sounds like a problem better handled outside the database engine. You can still use the above cross join to get a preliminary dataset, where you filter out some conditions you have on the set.