Merge multiple records in the same table in access - ms-access

Hello I am dealing with some unfriendly import files which import as:
timestamp position name
001 2 Jon
001 3 Bob
001 1 Ann
001 4 Mike
002 1 Joe
002 2 Sue
003 1 Jeff
004 5 James
004 1 Andy
004 2 Beth
004 4 Mitch
004 3 Chris
And would like to create a new table that displays thusly:
timestamp position1 position2 position3 position4 position5
001 Ann Jon Bob Mike
002 Joe Sue
003 Jeff
004 Andy Beth Chris Mitch James
By browsing this forum the closest I have come to a solution is:
SELECT pos1.timestamp, pos1.name AS position1, pos2.name AS position2
FROM table1 AS pos1 INNER JOIN table1 AS pos2
ON pos1.timestamp = pos2.timestamp
WHERE (((pos1.position)=1) AND ((pos2.position)=2))
I cannot figure out how to expand this to my specs, any help is much appreciated.

Try something like this
TRANSFORM First(Table.[name]) AS FirstOfname
SELECT Table.[timestamp]
FROM [Table]
GROUP BY Table.[timestamp]
PIVOT Table.[position];
I created this using the MS Access Cross Tab Query Wizard.
Also have a look at
What is a CrossTab Query?
Some better explenation.
Click the Create tab.
Click the Query Wizard.
Select Second Option (Crosstab Query
Wizard) and hit OK.
Select the table for your input, and
hit next.
Select Timestamp and click the arrow
(single) pointing fromleft to right,
and hit next.
Select position and hit next.
name will be the remaining field,
Select First (default is count), and
hit next.
Hit finish.
It should have saved a query called Table_Crosstab (or something similar).
Right click this and select design view.
On the view button, select Sql View.
You should see something similar to
TRANSFORM First(Table.name) AS FirstOfname
SELECT Table.timestamp, First(Table.name) AS [Total Of name]
FROM [Table]
GROUP BY Table.timestamp
PIVOT Table.position;
From the second line remove
, First(Table.name) AS [Total Of name]
so that you end up with
TRANSFORM First(Table.name) AS FirstOfname
SELECT Table.timestamp
FROM [Table]
GROUP BY Table.timestamp
PIVOT Table.position;
And that should be it. Save and you are ready.

Your new table is a bad design. What happens when there is a sixth and a seventh person in the incoming data? It will also be difficult to work with when you need to pull out all the data by a specific person as you will then need to query five columns.

Related

Mysql stored procedure Pass Guid list to parameter and use for in clause [duplicate]

I have a table like:
id name children
1 Roberto Michael,Dia
2 Maria John,Alex
3 Mary Alexandre,Diana
My problem is;
I want to find who has a child named Alex.
I can't use "where children = 'Alex'" in SQL because I have more than one names in same cells.
So I use "where children LIKE '%Alex%'" - that looks smart but
in the same time i get all start like Alex :( Alexandre
or i want to get dia but result is dia and diana :(
how can I get single Alex in that data type?
I hope I can explain my problem with my terrible english :D
The best solution would be to normalize your schema. You should have a separate table with one row for each child, instead of a comma-delimited list. Then you can join with this table to find parent with a specific child. See #themite's answer for an example of this.
But if you can't do that for some reason, you can use FIND_IN_SET:
WHERE FIND_IN_SET('Alex', children)
You should split the data into two tables.
the first would look like this
ID Name
1 Roberto
2 Maria
3 Mary
And the second like this
ParentId child
1 Michael
1 Dia
2 John
2 Alex
and so on.
then you could do the query you want without having to worry about like and your data is much more useable
That's why you'd want to have two tables here.
parents:
id name
1 Roberto
2 Maria
3 Mary
children:
id parentid name
1 1 Michael
2 1 Dia
3 2 John
4 2 Alex
5 3 Alexandre
6 3 Diana
And now you can query this much more effectively with a join or an exists:
SELECT *
FROM Parents
WHERE EXISTS(
SELECT *
FROM Children
WHERE parentid=Parents.id
AND Children.name='Alex'
)
I would rather make different tables for children and parents something like this.
Table for parents
parent_id name
1 Roberto
2 Maria
3 Mary
Table for children
children_id parent_id name
1 1 Michael
2 1 Dia
3 2 John
.... and so on

MySQL values to be in one column just separated by a comma

So I have a query that is pulling alot of data together and I would like it to only have one row vs x amount, and it just be one but the column Product ID, which is the last one, to just be all on one line, example data:
ID FN LN MEM REP EMAIL PID
001 Test User 1001 1001 testemail#gmail.com 001
001 Test User 1001 1001 testemail#gmail.com 002
001 Test User 1001 1001 testemail#gmail.com 003
001 Test User 1001 1001 testemail#gmail.com 004
001 Test User 1001 1001 testemail#gmail.com 005
001 Test User 1001 1001 testemail#gmail.com 006
001 Test User 1001 1001 testemail#gmail.com 007
But would like the output to be:
001 Test User 1001 1001 testemail#gmail.com 001,002,003,004,005,006,007
My SQL knowledge is not super strong so im kinda lost, any help would be awesome. I tried GROUP BY but some of the data as different values in the fourth column so it wont always work.
You're looking for GROUP_CONCAT along with GROUP BY.
SELECT ID, LN, EMAIL, GROUP_CONCAT(PID) as products
FROM tableName
GROUP BY ID
Or something like that. GROUP_CONCAT(productID) will combine them into one row and GROUP BY tells it how to combine the rows together.
If you remove the GROUP BY, you will get one row with all the results found. If you add it, it tells it how to combine the rows, which field to match.
GROUP_CONCAT is probably what you're looking for. So you can group and concatenate the Product ID and group it by a common attribute of your data records.
Try GROUP_CONCAT()
It's the same thing as Stuff For XML in TSQL

Distinct is not working in crystal reports and in mysql

id_no doc_id item_no product customer
123 2 1 A Daisy
123 2 9 A Ben
123 4 3 A Daisy
123 4 4 A Ben
123 6 11 B Daisy
123 6 13 B Ben
when I put it in my report it results to
Daisy Daisy
Ben
And it is also the result in mysql
select distinct customer from receipt where id_no like '123'
result:
Daisy
Daisy
Ben
Another query that I tried:
select distinct id_no, customer, product from receipt where id_no like '123'
result:
123 Daisy A
123 Daisy B
123 Daisy A
123 Ben A
123 Ben B
desired result:
Daisy
Ben
Please help me please.
Thank you guys for the help I found out why the other one keeps on showing. It is because the other Daisy is spelled as Daissy that's why.
Most likely your Customer name contains additional characters between the two records. Depending on how the datatype is implemented, spaces could matter and have contributed to the difference.
Try concatenating a character before and after customer.
I am unfamiliar with the concepts in Crystal Reports, but from what I understand, you would have to create a formula like so:
"XXX" & {Receipt.Customer} & "XXX"
If you run it again, you might recognize there is additional space like so:
XXXDaisyXXX
XXXDaisy XXX
^____ Additional Space
There is no chance of error while you using distinct ..it should return distinct value ...any way you can try another way
SELECT customer FROM receipt WHERE id_no like '123' GROUP BY customer
I don't see why you are fetching three records. I tried implementing your database and ran your query. It returned the result as expected.
See the above pic. There may be some issue with the data type you used. You may try grouping via customer, but I don't think it should affect your result anyway.
Also Check if the data types match.
The selection you made from customer id and id_no is unique and with distinct it should return only two rows
plase try this code
i get solution
select distinct `customer` from receipt where `id_no`='123'
this is right
i tryied this is my past project
best of luck

Merging and converting rows to columns

I have a list of data that happens to have been set up like this:
entryID fieldID Value
100 1 John
100 2 Smith
100 3 USA
101 1 Jane
101 2 Doe
101 3 USA
The way it works is, when a "ticket" is created, it assigns an entryID for all the data in that ticket, but each field also has it's own fieldID. So first name is always a fieldID of 1, last name is 2, etc.
What I need to be able to do is create a view that will look like this:
First Name Last Name Country
John Smith USA
Jane Doe USA
I need to be able to do this in MySQL, and not SQL or excel, as some other similar problems have addressed.
I just found the answer to my own question, so I will post it here for future users.
This website explains how to do exactly what I was asking for: http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/

MySQL table comparison between two tables

I have two tables, follow and followed. I want to get all the rows in the follow table such that follow.screen != followed.following_screen_name.
follow table
ID screen_name
-----------------
1 eddie
2 jason
3 omar
4 jonathan
5 jack
followed table
ID my_screen_name following_screen_name
-------------------------------------------
1 john eddie
2 kenny eddie
3 kenny omar
4 john jason
5 john omar
Query I tried which didn't work
SELECT follow.screen_name from follow, followed where followed.my_screen_name='john'
AND follow.screen_name != followed.following_screen_name
Expected results
ID screen_name
-----------------
1 jonathan
2 jack
you can get this by doing a LEFT JOIN
SELECT F.screen_name FROM follow F
LEFT JOIN followed FD
on F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
WHERE FD.my_screen_name IS NULL
and FD.following_screen_name IS NULL
Another way is to use NOT EXISTS, get all rows that exists in followed and do NOT EXISTS clause to get desired result.
SELECT F.screen_name FROM follow F
WHERE NOT EXISTS
(
SELECT 1 FROM followed FD
WHERE F.screen_name = FD.my_screen_name
OR F.screen_name = FD.following_screen_name
)
There are plenty of ways to solve this, but common to all is that you need to compare the follow.screen_name to both followed.my_screen_name and followed.following_screen__name.
One way is to use NOT IN with a UNION:
select screen_name
from follow
where screen_name not in (
select following_screen_name
from followed
where following_screen_name is not null
union all
select my_screen_name
from followed
where my_screen_name is not null
)
While this approach is nice for clarity, it may not be as good for performance as using a left join or not exists.
A nice place to pick up mysql syntax and logic is here.
But try this code, it selects every row where the screen_name is not identical to anything produced in the next two queries:
SELECT * from follow WHERE screen_name
not in (select screen_name from followed)
AND not in (select followed_screen_name from followed);
The last two queries would look this and the WHERE filters all of the rows out with screen names identical to the fields below.
my_screen_name following_screen_name
-------------------------------------
john eddie
kenny eddie
kenny omar
john jason
john omar