In the MySQL Workbench tool, this is what I need to accomplish. Table A contains one unique member. Table B can have anywhere from 1 to 7 rows for the member because this table contains all the different IDs a member can have, ie, first row contains ssn #, 2nd row contains subscriber_id, etc. I need to link the 2 tables, obtaining ALL the member ids in the Table B rows, BUT I only want to produce one output line for the member. Here is an example of the ouput line:
member_name ssn# subscriber# another_id another_id another_id
John doe 123456789 634 0018 blank 3876
Basically, what I'm trying to do is loop thru Table B to obtain each type of id for a member.
Thank you for any suggestions.
Related
I know it’s vague but I think there’s a simple answer I’m missing. I want to distinct select from column A while creating new columns from counts of specific variables in column B. If column A has names and column B has pets. I want a distinct column A with counts for each animal creating a new column. I.e column B is cats, column C is dogs, column D is horses, etc with the values being the counts for those animals with that person.
I have two columns, I want the second column to have the same values as the first column always, in PostgreSQL.
The columns are landmark_id (integer) and name (varchar), I want the name column to always have the same values (id's) from landmark_id.
landmark_id (integer) | name (varchar)
1 1
2 2
3 3
I don't understand why you would want to do that, but I can think of two ways to accomplish your request. One is by using a generated column
CREATE TABLE g (
landmark_id int,
name varchar(100) GENERATED ALWAYS AS (landmark_id::varchar) STORED
)
and the other is by enforcing a constraint
CREATE TABLE c (
landmark_id int,
name varchar(100),
CONSTRAINT equality_cc CHECK (landmark_id = name::varchar)
)
Both approaches will cause the name column to occupy disk space. The first approach will not allow you to specify the name column in INSERT or UPDATE statements. In the latter case, you will be forced to specify both columns when inserting.
You could also have used a trigger to update the second column.
Late edit: Others suggested using a view. I agree that it's a better idea than what I wrote.
Create a view, as suggested by #jarlh in comments. This automatically generates column name for you on the fly. This is usually preferred to storing essentially the same data multiple times as in an actual table, where the data occupies more disk space and also can get out of sync. For example:
CREATE VIEW landmarks_names AS
SELECT landmark_id,
landmark_id::text AS name
FROM landmarks;
I have a raw file csv with personal information including addresses: there are two columns each for an address, and two distinct addresses (four fields in total).
So I might have Fred, Flinstone, ID#12345678, 123 Street, Apt 6, Seattle, WA, 87319, 567 5th Ave, Seattle, WA, 87321 in my CSV along with several other columns that I need.
My question is, is there a way to import or set up a CREATE TABLE statement that will take this data and split it into two rows:
0: Fred, Flinstone ID#12345678, 123 Street, APT 6, Seattle, WA, 87319
1: Fred, Flinstone ID#12345678, 567 5th Ave, Seattle, WA, 87312
I know that there's Array_arg for splitting an array into distinct rows, but I don't know about populating a new table from it. Basically my problem right now is that we set up this database with only the primary/first listed address, and now that is causing significant issues when trying to join this table. I do not want to set up two tables where I can join to get both addresses. I just want to know if there's a way to do this on an import, as my searching hasn't turned up what I'm looking for.
There are two ways to approach this:
Use a foreign data wrapper
Use copy and staging table.
You can define a foreign table that can access the csv table as if it were a sql table with a foreign data wrapper. You can then use insert as select to insert the records in the target table. You would need to run the insert as select twice: first for the primary address and then for the secondary address.
Instead of using the foreign data wrapper approach you can first load the table into a staging table with the copy command. And then use the staging table to run insert as select into the target table.
I have to tables with identical columns. In both cases the first column is called id and type of SERIAL.
I want to copy the content of the second to the first table. I want to copy everything except the id. If I do this:
INSERT INTO first
SELECT *
FROM second;
It will complain, because it will duplicate the id.
If I do this:
INSERT INTO first
SELECT 1col, 2col, 3col .... (every column except the id column, which I dont want to be copied)
FROM second;
It will complain because it tries to insert the value of the '1col' into id column.
ERROR: column "id" is of type bigint but expression is of type date
So the bottom line is I want copy EVERYTHING except the SERIAL value, which needs to be calculated by the receiving table. Any hint?
You were half way there.
INSERT INTO first (1col, 2col, 3col ....)
SELECT 1col, 2col, 3col ....
FROM second
Yes, you have to repeat all the columns. No, there's no way to say "all except id".
I need to create a report that shows, for each master row, two sets of child (detail) rows. In database terms you can think of this as table 'A' having a 1:M relationship to table 'B' and also a 1:M relationship to table 'C'. So, for each row from table 'A', I want to display a list of child rows from 'B' (preferably in one section) and a list of child rows from 'C' (preferably in another section). I would also prefer to avoid the use of sub-reports, if possible. How can I best accomplish this?
Thanks.
I think I understand your question correctly, ie for a given row in Table A, you want the details section(s) to show all connected rows in Table B and then all connected rows in Table C (for any number of rows in B or C, including zero). I only know of two solutions to this, neither of which is straightforward.
The first is, as you've guessed, the disliked subreport option.
The second involves some additional work in the database; specifically, creating a view that combines the entries in Table B and Table C into one table, which can then be used in the main report as a linkable object to report on and you can group on Table A as desired. How straightforward this is will depend on how similar the structures of B and C are. If they were effectively identical, the view could contain something simple like
SELECT 'B' AS DetailType, Field1, Field2, FieldLinkedToTableA
FROM TableB
UNION ALL
SELECT 'C' AS DetailType, Field1, Field2, FieldLinkedToTableA
FROM TAbleC
However, neither option will scale well to reports with lots of results (unless your server supports indexing the view).
This is exactly what Crystal was made for :)
Make a blank .rpt and connect to your data sources as you normally would.
Go to Report->Group Expert and choose your grouping field (aka Index field).
You will now see a Group Header section in your design view. This is your "Master row". The Details sections will be your "Child rows".
In the example image below, this file is grouped by {Client}. For client "ZZZZ", there are 2 records, so there are 2 details sections while all the other clients only have 1 details section.
Edit
Based on your response, how about this:
In your datasource (or perhaps using some kind of intermediary like MS Access), start SQLing as follows.
Make a subquery left joining the primary key of TblA and the foreign key of TblB. Add a third column containing a constant, e.g. "TblB"
Make a subquery left joining the primary key of TblA and the foreign key of TblC. Add a third column containing a different constant, e.g. "TblC"
Union those 2 queries together. That'll be your "index table" of your crystal report.
In Crystal, you can have multiple grouping levels. So group first by your constant column, then by the TblA primary key, then by the foreign key.
This way, all the results from TblB will be displayed first, then TblC. And with a little work, tables B & C won't even have to have the same field definitions.
You can use or create columns that are used for grouping, then group on the table A column, then the table B column, then C. (Crystal's group is not the same as t-sql "group by". In Crystal, it's more of a sorting than a grouping.)