I'm having a hard time getting pointed in the right direction for how to write a SQL query. Using the data example, I would like to find devices where the elements are a perfect match, so in this case device 1 and 2. Can someone help me on keywords to help search for something that would work? In my actual table, Device is an Id, but I showed text for readability.
What does the actual table look like? Is the data in the Element column also an id? It sounds like you're looking for a where clause in your SQL query.
Does this work?
SELECT device, element
FROM yourTableHere
WHERE device=element
This won't work as you think. Grouping by all fields is effectively grouping by no fields
@jlandwerlen I don't quite understand what you want to return. If elements exactly match, I.e. if device 1 and 2 both have elements 1-6, devices 1 and 2 are returned. But what if devices 6 and 10 both have elements 1-4, return those as well?
What's your criteria though for filtering which elements you want?
Maybe it would be more obvious if you used actual (or actual but modified if it's private) table data, and a larger array of table rows, and then show a couple examples of the result you'd like to see for different criteria
Here is another example. The basic reason is we have a (long) list of tags given to us and I want to see which ones I can group together because they match (think UDT). So, in this example Motor 1 and 3 are a (full) match. Motor 2 is a one-off (it has one more than the others). There isn't any filtering.
I think I will just do a hybrid, since this doesn't need to be a production query, I will be lazy and simply loop through counts, up to the max count for a group. It's ugly but I'm just going tor functional at this point. Thanks for the help all.
I do this kind of thing in Excel with a PIVOT table. There's a PIVOT function in MS SQL as well - what DB are you using? I know postgres doesn't have that function (at least from posts on this forum)
Then you can either visually look for patterns (sorting by some columns helps), or alternatively and more accurately, binary encode each property and then sum them together if they have a 1. Then all the matching sums will have exactly the same properties
SELECT GROUP_CONCAT(device ORDER BY device SEPARATOR ', ') AS identical_devices,
device_elements
FROM
(
SELECT
device,
GROUP_CONCAT(element ORDER BY element SEPARATOR ', ') AS device_elements
FROM
your_table
GROUP BY
device
) grouped_devices
GROUP BY device_elements;
Note: Written for MySQL. SQL Server (>=2017) would use STRING_AGG instead of GROUP_CONCAT.