Unpivot multiple grouped columns in MySQL - mysql

I am trying to unpivot multiple groups of columns with the same attribute but multiple value columns.
Say there are 2 products and 3 customers.
I am trying to get a transformed table with 1 attribute(customer: customer1, customer2 and customer3) and 2 values (product1 and product2)
I tried spliting it into 2 tables and then unpivot each table and finally join both the tables. I believe this is an unreasonable approach.
I have also done using multiple selects with union all. The place where I am getting stuck is how will I fill the customer column as all the fields are numerical values and the customer column which is to be formed is categorical.
SELECT ID-1, ID-2, ID-3, product1_customer1 AS customer1, product1_customer2 AS customer2, product1_customer3 AS customer3
FROM table
UNION ALL
SELECT ID-1, ID-2, ID-3, product2_customer1, product2_customer2, product2_customer3
FROM table
How can I get the product column?
Can you please point out in the direction where I'm going wrong?

Related

MySQL - Finding discrepancies in a common value for 2 different tables based on another common value

I have 2 tables, let's call them Table1 and Table2.
Table 1 and Table 2 both have 2 columns of interest (each table has several irrelevant columns as well): tID, xID
Some of the xID values are different between the 2 tables and I need to be able to visualise them in a table, preferably with 3 columns (tID, xID1, xID2) and displaying only values that have a discrepancy.
As the original tables have the same columns of interest, I am unsure if JOIN will just merge the values together or how it will treat the discrepancies.
This is what I have got at the moment, but I am not sure if it will work:
SELECT Table1.tID, Table1.xID, Table2.tID, Table2.xID,
FROM Table1, Table2
WHERE
table1.xID <> Table2.xID
GROUP BY tID
;
Could someone please advice me if this will work or what would be the best way to achieve this?
Thank you in advance for your help,

Multiple tables SQL with 1 same column

I have 2 tables:
acco_info
acco_revenue_2016
Both tables have different columns except for 1, acco_id.
This column is what connects both tables.
I want to write a query that combines important data from both tables and links them to the acco_id.
So from the acco_info table I need the following columns:
acco_id, acco_name, region_name, country_name
From the acco_revenue_2016 table I need:
acco_id, sales, revenue_per_item, revenue
The output should look like this:
acco_id, acco_name, region_name, country_name, sales, revenue_per_item, revenue
What's the best way to write this query?
I am stuck on
SELECT acco_id FROM acco_info UNION SELECT acco_id FROM acco_revenue_2016
This joins the ID's together but I can't find a way to also show the other data.
You'll be looking for something like this;
SELECT
ai.acco_id
,ai.acco_name
,ai.region_name
,ai.country_name
,ar.sales
,ar.revenue_per_item
,ar.revenue
FROM acco_info ai
INNER JOIN acco_revenue_2016 ar
ON ai.acco_id = ar.acco_id
This is assuming that both tables contain the same acco_id. Notice the table aliases to see which field is coming from each table

Create table in MS Access with all data and columns from Table 1 and add rows from Table 2

I'm trying to combine two tables together in MS Access (I'm using the Office 365 version). The tables both look something like this:
Headword Spelling Frequency
Word1 Sp1a x
Word1 Sp1b y
Word2 Sp2a z
So I've got a series of headwords and a few different spellings in each, along with their frequencies. These are in two tables.
I want a combined table with all the contents from Table 1, and some of Table 2. From Table 2, I only want the rows where the headword matches a headword in Table 1.
I've been trying to make this work using a join code:
SELECT Table1.Headword, Table2.Headword, Table1.Spelling,
Table2.Spelling, Table1.Frequency, Table2.Frequency,
FROM Table2 RIGHT JOIN Table1 ON Table2.Headword = Table1.Headword;
This has added new columns with the data from Table 2, which isn't what I wanted. (Though the data it added was the right data, at least!)
How do I go about joining the tables together in such a way that I have the three original columns from Table 1, and just add the relevant rows from Table 2 to the end?
You need a UNIONQuery:
SELECT Headword as HW, Spelling as SP, Frequency as Fr
FROM Table1
UNION
SELECT Table2.Headword as HW, Table2.Spelling as SP, Table2.Frequency as Fr
FROM Table2 RIGHT JOIN Table1 ON Table2.Headword = Table1.Headword;

sql join 2 tables on column x and merge field z

lets take an example - i have 2 data tables, table "books" with columns "shelfId" and "text", and table "shelves" with column "Id". I want to join these two tables on books.shelfId == shelves.Id, and as a result, i want to see a new table with 2 columns - column 1 has unique values of Ids, and column 2 has merged values of books.text with same books.shelfId values and separated by comma or something else, i.e. :
Is it possible to write such sql select to get what i need ?
Here is fiddle http://sqlfiddle.com/#!2/c96dfa/1
SELECT shelfid as id, GROUP_CONCAT(text) AS text
FROM books
GROUP BY shelfid

Show two fields from two tables as one field in mysql LIKE

I want to implement like search for autocomplete.I have two tables,Location and SubLocation.
I want to return only one field for this like.Here is my query
SELECT l.loc_name,sl.sub_loc FROM Location l,SubLocation sl
where l.loc_name LIKE '$term%' OR sl.sub_loc='$term%'
I want to show matching result from both tables as one return.EG,if i type D so i can view Dubai from first table and DubaiMarina from second table as one coloumn
You can use UNION
SELECT l.loc_name FROM Location l
where l.loc_name LIKE '$term%'
UNION
SELECT sl.sub_loc FROM SubLocation sl
where sl.sub_loc='$term%'
If the tables do not have duplicates, you can replace UNION with UNION ALL as the union all option will not look for duplicates, it might be a little faster depending on the amount of data in returned by the queries.