MS Access convert rows to columns using pivot - ms-access

I have a table with ~6500 products and images in which each product can have several jpg files:
ID_product product product_photo ordering
1 Product A a1234.jpg 1
2 Product B x5678.jpg 0
3 Product A b1234.jpg 0
4 Product B y5678.jpg 1
5 Product B z5678.jpg 2
6 Product C e4455.jpg 1
7 Product C f4455.jpg 0
8 Product C g4455.jpg 2
So I created a query in MS ACCESS:
TRANSFORM First([table1].product_photo) AS product
SELECT [table1].ID_product, First([table1].product) AS products
FROM [table1]
GROUP BY [table1].ID_product
PIVOT [table1].product_photo;
The result of the query:
ID_product product a1234_jpg b1234_jpg e4455_jpg f4455_jpg g4455_jpg x5678_jpg y5678_jpg z5678_jpg
1 Product A a1234.jpg b1234.jpg
2 Product B x5678.jpg y5678.jpg z5678.jpg
3 Product C e4455.jpg f4455.jpg g4455.jpg
I would like to change the table in such a way that the images are in columns:
ID_product product image_1 image_2 image_3
1 Product A a1234.jpg b1234.jpg
2 Product B x5678.jpg y5678.jpg z5678.jpg
3 Product C e4455.jpg f4455.jpg g4455.jpg
How to expand the query to get the desired result?

Because the Pivot works on the value of the column rather than its name, you are out of luck. This is the problem with pivoting - it's fairly limited in its flexibility.
At this point, you usually revert to code. For example, you could build the result set in VBA in an array of arrays of variant.
Or, you could use VBA to count the maximum number of columns for any product, create a temp table with appropriately named columns, and insert the values into the appropriate rows.
Either way, this is really a UI issue of sexy display to the user, rather than handing off data from one query to the next, which is what SQL is designed for.

Related

How can I get all products which have selected attributes in Prestashop

I am working on module development for Prestashop. Now I have situation in which I have to fetch all products which have selected attributes. There is a interface, where dropdown list of all active attributes are showing. And user does selects attributes as per need. Now, I want to find all the products on the basis of selected attributes.
Below are the table structure:
Product Table:
id_product id_shop ean upc quantity price
1 1 abc 50 16.99
2 1 def 25 25.99
Product Combination Table
id_attribute id_product
1 1
13 1
5 1
1 2
10 2
Can anyone please help on how can I fetch products on the basis of selected attributes??
Isn't this really basic My SQL ?
select * from product
where
(
select count(*) from
product_combination
where product_combination.id_attribute in (X,Y,Z)
and product.product_id = product_combination.product_id
) = 3
where X,Y,Z are the attributes the user selected, and 3 is the count of attributes selected.
If this is more involved, I think you need to edit your question to provide some more details as to exactly what the technical issue is. If its just that you dont know SQL then this isn't really the place to post your query.

exact search and similar search combining two tables with multiple keywords mysql

Say I've two tables products and brands having below data.
tbl_products
ID Name BrandID Price
1 Keyboard 1 100
2 Keyboard 2 120
3 Keyboard wireless 1 130
4 Keyboard wireless 2 150
tbl_brands
ID Name
1 Microsoft
2 Dell
3 HP
What I want is when I type 'Microsoft Keyboard' or 'Keyboard Microsoft' then it should list me product ID 1 and 3 not 2 or 4 even 2 or 4 has keyboard. I may search for more keywords but it should give me only the items matching itself.
SELECT p.*, b.Name BrandName FROM tbl_products p INNER JOIN tbl_brands b ON b.ID = p.BrandID WHERE p.Name LIKE '%Microsoft%' OR b.Name LIKE '%Microsoft%' OR p.Name LIKE '%Keyboard%' OR b.Name LIKE '%Keyboard%'
Please help me to write proper MySQL query or any schema change with query..
Appreciate the question. Though I don't have exact answer but surely can discuss one approach to solve the problem.
STEP 1 Get BRAND NAME+NAME as single string.
STEP 2 Tokenise the entered STRING. Example Microsoft Keyboard = Mincrosoft,Keyboard Following Link for spliting the entered data. How do I split a string so I can access item x?
STEP 3 DO a like query on the string obtained in step 1.

Efficient MySQL query method for multiple joins

I am asking this question in the hope there is a more efficient (faster) way to pull and insert data in the the tables I am working with.
The basic structure of the data table is
ID Doc_ID Field Value
1 10 Title abc
2 10 Abstract xyz
3 10 Author Bob
4 11 Publisher Bookworms
5 11 Title zzz
6 11 Abstract bbb
7 12 Title aaa
8 12 Sale No
In other words the data tables are row based, each row contain a document id and the corresponding field value. Not all documents have the same number of fields defined. Indeed books may differ radically from magazines.
The data table is 10,000,000 rows typically a document has 100 fields associated with it.
So the performance problem I am finding is pulling a report with reference to 50+ different fields, for example if I have a query list in an order_table the query could be like
select ord.number as 'Order ID', d1.value as 'Title', d2.value as 'Author' .......
from order_table ord
LEFT JOIN data_table as d1 on d1.Doc_ID=ord.Doc_ID and d1.Field='Title'
LEFT JOIN data_table as d2 on d2.Doc_ID=ord.Doc_ID and d2.Field='Author'
........
LEFT JOIN data_table as d50 on d50.Doc_ID=ord.Doc_ID and d50.Field='Qty'
Using LEFT JOINS as there is no guarantee that the field is defined for that document.
Given there may be some WHERE parameters to limit the list to items (in stock for example or below a price) it is a slow query. Indexes don't really much.
Without being able to change the data model, what is the best way to pull volumes of information out?

MySQL - Search for record within nested tables

I have a MySQL database with two tables. The first is a hierachy of departments, whereby each record has a parent record from within the same table, and the top level has a parent_id of 0.
The second is a table of products. Each product can live anywhere in the tree of departments.
I need to be able to let people search for a product by specifying a department to search within, but for the search to look within all the sub-departments of the specified department. Can this be done in a single query?
My "Plan B" is to store a list of parent ids with each product in a new field, and search that field using a LIKE query, but that seems nasty somehow.
Dummy data:
id|parent_id|name
1|0|Main 1
2|0|Main 2
3|0|Main 3
4|1|Sub 1-1
5|1|Sub 1-2
6|1|Sub 1-3
7|4|Sub 1-1-1
8|4|Sub 1-1-2
9|4|Sub 1-1-3
id|department|product
1|1|test product 1
2|4|test product 2
3|7|test product 3
SELECT * FROM products WHERE department = 1 ; want to receive all three products
SELECT * FROM products WHERE department = 4 ; want to receive products 2 and 3
SELECT * FROM products WHERE department = 7 ; want to receive only product 3

Using mysql i need to retrieve which are inserted last in every category

My prob in brief:
I have two tables namely category and product.
table: category
id category
1 cat1
2 cat2
3 cat3
4 cat4
table: product
id productName category
1 test1 1
2 test2 2
3 test3 3
4 test4 4
5 test5 2
6 test6 2
My prob is:
I need products which are inserted last in every category.
How to solve this.
thanks in advance
You could add a create_time timestamp when a new a product has been added, and retrieve the newest by category be something like:
select max(create_time),category from product group by category
This is a variation of one of the most-common SQL questions asked here, the per-group maximum. See eg. this question for a variety of approaches.
The one I often use is a null-self-left-join, to select the row which has no value above it:
SELECT p0.*
FROM product AS p0
LEFT JOIN product AS p1 ON p1.category=p0.category AND p1.id>p0.id
WHERE p1.id IS NULL
This is assuming that id​s are allocated in order so the highest is the most recent. Normally it wouldn't be a good idea to rely on identity as an ordering mechanism; you'd typically add an added timestamp to each row to work on instead.
(Note this and many other per-group maximum functions can return more than one row when two rows have identical order columns. If this is a problem it could be avoided by using a UNIQUE ordering column; as a primary key id is already that. You can get a single row even when there are two maxima using SQL:2003's useful but rather ugly ROW_NUMBER() OVER windowing functions, but this is not supported by MySQL.)