MS Access Query - One-to-Many Concatenation [duplicate] - ms-access

I'm trying to aggregate some instructor data (to easily show which courses an instructor taught in a semester), and up until now I've just accepted having multiple rows for each instructor. However, it would be beneficial to some business processes if I could have all of an instructor's teaching in a single row. Here is some example data (my tables have a lot more columns, but the general idea won't change much.
tbl_Instructors has:
N_ID | F_Name | L_Name
001 Joe Smith
002 Henry Fonda
003 Lou Reed
tbl_Courses has:
Course_ID | N_ID | Course_Info
AAA 001 PHYS 1
AAB 001 PHYS 2
CCC 002 PHYS 12
DDD 003 PHYS 121
FFF 003 PHYS 224
What I want to return is:
N_ID | First_Name | Last_Name | Course_IDs
001 Joe Smith AAA, AAB
002 Henry Fonda CCC
003 Lou Reed DDD, FFF
I think I need to do something with selecting all N_IDs from tbl_Instructors, then returning the Course_IDs from tbl_Courses via concatenation, but that magic step has alluded me. Any help? Can I do this via SQL selects or will I need to use VB?

This is easy using Allen Browne's ConcatRelated() function. Copy the function from that web page and paste it into an Access standard code module.
Then this query will return what you asked for.
SELECT
i.N_ID,
i.F_Name,
i.L_Name,
ConcatRelated(
"Course_ID",
"tbl_Courses",
"N_ID = '" & [N_ID] & "'"
) AS Course_IDs
FROM tbl_Instructors AS i;
Consider changing the data type of N_ID from text to numeric in both tables. If you do that, you don't need the single quotes in the third argument to that ConcatRelated() expression.
"N_ID = " & [N_ID]
And whenever you need N_ID displayed with leading zeros, use a Format() expression.
Format(N_ID, "000")

Related

SSRS "flatten" sql query

I have data coming into a report (from a stored procedure I can't edit) in the following form:
RecordID | Label | Value
1 Name Alice Arnold
1 Addr 123 Main St
1 City Hometown
1 State US
2 Name Bob Barker
2 Addr 456 Side St
2 City Hometown
2 State US
I need to display it like so:
Name | Addr | City | State
Alice Arnold | 123 Main St | Hometown | US
Bob Barker | 456 Side St | Hometown | US
First, I tried grouping the table by Record ID, but I get 4 detail rows that are waterfalled.
Then I tried using an expression like:
=IIF(Fields!Field_Label.Value = "Name", Fields!Field_Value.Value, Nothing)
in the group header row, but whichever row happens to come first (Name, Addr, City, or State) is the one that shows up in the header row and the rest are blank because it only pulls in the FIRST record if you put detail data in the header row.
Any one have any ideas? Thanks in advance for your help!

Count the number of times of component replacement

I want to find the count on "The number of times a replacement occurred for each component."
For example,
Component_ID | Replacement_ID
001 | NULL
002 | 001
003 | 002
004 | 003
005 | Null
Result :
Component_ID | Number of time a replacement already occurred
004 | 3
005 | 0
Normally you are supposed to have at-least two tables. A components table which contains your components with Component_ID as the primary field together with other fields such as component_name and another table probably called component_replacements which contains a primary field ID such as component_replacement_id together with component_id and other fields such as replacement date. Component_ids can occur more than once in this table. This would allow you to have a basic query such as:
SELECT Component_ID, IFNULL( total,0) replacements FROM
(
SELECT Component_ID, COUNT(*) total
FROM component_replacements GROUP BY Component_ID
) replacements ON components.Component_ID = replacements.Component_ID

Combine values from related rows into a single concatenated string value

I'm trying to aggregate some instructor data (to easily show which courses an instructor taught in a semester), and up until now I've just accepted having multiple rows for each instructor. However, it would be beneficial to some business processes if I could have all of an instructor's teaching in a single row. Here is some example data (my tables have a lot more columns, but the general idea won't change much.
tbl_Instructors has:
N_ID | F_Name | L_Name
001 Joe Smith
002 Henry Fonda
003 Lou Reed
tbl_Courses has:
Course_ID | N_ID | Course_Info
AAA 001 PHYS 1
AAB 001 PHYS 2
CCC 002 PHYS 12
DDD 003 PHYS 121
FFF 003 PHYS 224
What I want to return is:
N_ID | First_Name | Last_Name | Course_IDs
001 Joe Smith AAA, AAB
002 Henry Fonda CCC
003 Lou Reed DDD, FFF
I think I need to do something with selecting all N_IDs from tbl_Instructors, then returning the Course_IDs from tbl_Courses via concatenation, but that magic step has alluded me. Any help? Can I do this via SQL selects or will I need to use VB?
This is easy using Allen Browne's ConcatRelated() function. Copy the function from that web page and paste it into an Access standard code module.
Then this query will return what you asked for.
SELECT
i.N_ID,
i.F_Name,
i.L_Name,
ConcatRelated(
"Course_ID",
"tbl_Courses",
"N_ID = '" & [N_ID] & "'"
) AS Course_IDs
FROM tbl_Instructors AS i;
Consider changing the data type of N_ID from text to numeric in both tables. If you do that, you don't need the single quotes in the third argument to that ConcatRelated() expression.
"N_ID = " & [N_ID]
And whenever you need N_ID displayed with leading zeros, use a Format() expression.
Format(N_ID, "000")

SQL Postcode exist within range

I'm having the following situation:
I have a table with a list of postcodes with the format:
1234 AA (Dutch postcode)
2345 ZF
B-2345 (Belgium postcode)
B-4355
I have another table which contains postcoderanges:
PostcodeFrom
1000 AF
2000 ZF
B-1234
PostcodeTo
1999 ZX
2999 ZF
B-1889
I am looking for a solution how to look up the postcode value between the several ranges.
First I was thinking of
SUBSTRING(MyPostcode,1,4) BETWEEN SUBSTRING(PostcodeFrom,1,4) AND SUBSTRING(PostcodeTo,1,4)
.. but then there is still the problem with the characters (not even thinking about the belgium postcodes aswell).
Could anyone help me?
Yours,
Thanks for your reply!
The table you drew, needs one more field: RegionCode.
RangeTable:
| RCode | PCodeFrom | PCodeTo |
| 001 | 1000 BA | 1999 ZZ |
| 002 | 1000 AA | 1999 AZ |
Notice that if a postcode is 1234 AC, it must return RegionCode: 002 To compare numbers is not hard, but how to compare characters? I had an idea of making a table with AA - ZZ where each combination has a certain INT value, but I hope there is another, easier way.
You can only do this reliably (ignoring the potential un-reliability of doing this sort of range matching with postcodes) by splitting the portions of the postcode into different columns by character type.
I don't know much about Dutch postcodes, but if your formats are correct, you could create a table like:
+-------+------+
| code | city |
+-------+------+
| 1234 | AA |
+-------+------+
Splitting the postcodes up will allow you to do more fine-grained sorting.
Update:
Having looked at the Wikipedia page on Dutch postcodes it looks like this should work for all of them. My labels of code and city are inaccurate though.
Aside: I'm impressed that the Netherlands has such a sane postcode format, unlike the UK one where you need a huge regex to even decide if the format is valid.
Update 2:
Your checking will work with characters too, but you'll be better off storing the postcodes in a separate table, with an ID. The example above was just to show splitting up the characters from the numbers, so what you'll actually want is more like:
mysql> select * from postcodes;
+------+-------+-------+
| id | part1 | part2 |
+------+-------+-------+
| 1 | 1234 | AA |
| 2 | 5678 | BB |
+------+-------+-------+
When you're storing the ranges, don't store the postcodes in the ranges table, store the id for the entry in the postcodes table, like:
mysql> select * from ranges;
+-------------+---------------+-------------+
| region_code | postcode_from | postcode_to |
+-------------+---------------+-------------+
| 1 | 1 | 2 |
+-------------+---------------+-------------+
That record says "region 1 is 1234 AA to 5678 BB"
For an example, I'll say that postcodes start 0001 AA, then move to 0001 AB, all the way to 0001 ZZ, then 0002 AA and so on. This obviously isn't right but it demonstrates the theory. You need to substitute this for the algorithm you're using to define how postcodes are incremented and decremented.
When you want to find out "does postcode 3456 XY fit into region 89?", you split it into character and number, and check whether the values fit into a range. Using my algorithm, I check:
Is the number portion greater or less than the number portion of postcode_from?
If it's greater, then is it less than the number portion of postcode_to?
If you satisfy both conditions, check the letters - this is the important bit - MySQL's character set collation does allow you to say "is AB less than BC, you can have:
WHERE 'AB' < part2;
in your WHERE clause.
Using this method, you can figure out which of your regions has a start and an end that fit the value you're testing.
It's a bit long-winded but it will work without doing any conversions. You may need to check that the collation you're using fits the way the lettering sequence works for the specific type of postcode you're using though.

Group by X or Y?

I'm trying to figure out how to GROUP BY on multiple columns. I want to group items when the SSN or the address matches. For example, here are three records:
account_number | name | ssn | address
---------------+--------------+-------------+----------------------
23952352340 | SMITH INC | 123-45-6789 | P.O. BOX 123
3459450340 | JOHN SMITH | 123-45-6789 | 123 EVERGREEN TERRACE
45949459494 | JANE SMITH | 395-23-1924 | 123 EVERGREEN TERRACE
And here's what I'd like to end up with:
names
----------------------
SMITH INC, JOHN SMITH, JANE SMITH
Any suggestions?
You can't do this easily in MySQL.
The problem is that the relation "is similar to" as you define it is not transitive. In your example, Smith Inc is similar to John Smith (per SSN) and John Smith is similar to Jane Smith (per name), but Smith Inc is not similar to Jane Smith. So there is no single value that all records could be compared with and GROUP BY won't help here.
In other systems which support recursion you could build a transitive closure of this relation which would allow grouping, but this is not an easy task in MySQL.
Like this:
SELECT
name,
ssn,
COUNT(*)
FROM TheTable
GROUP BY
name,
ssn