SQL query to create joins

I am trying to solve the following problem.

It seems to me that this is possible, but I do not seem to understand.

Here's the script:

Table 1 (Assets)
1 Asset-A
2 Asset-B
3 Asset-C
4 Asset-D

Table 2 (Attributes)
1 Asset-A Red
2 Asset-A Hard
3 Asset-B Red
4 Asset-B Hard
5 Asset-B Heavy
6 Asset-C Blue
7 Asset-C Hard

If I am looking for something that has the same attributes as Asset-A, then it should identify Asset-B, since Asset-B has all the same attributes as Asset-A (it should drop heavy because Asset -A didn’t indicate something else or similar). Also, if I wanted only the attributes for Asset-A AND Asset-B to be distributed, how would I get this?

It seems simple, but I can not nail it ...

The actual table I'm using is almost exactly Table2, just the association of AssetId and AttributeId like this: PK: Id
int: AssetId
int: AttributeId

I just included the idea of ​​an asset table to simplify the issue.

+5
9

, .

WITH Atts AS 
(
    SELECT
    DISTINCT
        at1.[Attribute]
    FROM
        Attribute at1
    WHERE
        at1.[Asset] = 'Asset-A'
)

SELECT 
    DISTINCT
    Asset,
    (
        SELECT 
            COUNT(ta2.[Attribute]) 
        FROM 
            Attribute ta2 
        INNER JOIN
            Atts b 
            ON
                b.[Attribute] = ta2.[attribute]
        WHERE 
            ta2.[Asset] = ta.Asset
    ) 
    AS [Count]
FROM 
    Atts a
INNER JOIN
    Attribute ta
    ON
    a.[Attribute] = ta.[Attribute]
0
SELECT  ato.id, ato.value
FROM    (
        SELECT  id
        FROM    assets a
        WHERE   NOT EXISTS
                (
                SELECT  NULL
                FROM    attributes ata
                LEFT  JOIN
                        attributes ato
                ON      ato.id = ata.id
                        AND ato.value = ata.value
                WHERE   ata.id = 1
                        AND ato.id IS NULL
                )
        ) ao
JOIN    attributes ato
ON      ato.id = ao.id
JOIN    attributes ata
ON      ata.id = 1
        AND ata.value = ato.value

SQL Server 2005 ( ):

WITH    assets AS 
        (
        SELECT 1 AS id, 'A' AS name
        UNION ALL
        SELECT 2 AS id, 'B' AS name
        UNION ALL
        SELECT 3 AS id, 'C' AS name
        UNION ALL
        SELECT 4 AS id, 'D' AS name
        ),
        attributes AS
        (
        SELECT 1 AS id, 'Red' AS value
        UNION ALL
        SELECT 1 AS id, 'Hard' AS value
        UNION ALL
        SELECT 2 AS id, 'Red' AS value
        UNION ALL
        SELECT 2 AS id, 'Hard' AS value
        UNION ALL
        SELECT 2 AS id, 'Heavy' AS value
        UNION ALL
        SELECT 3 AS id, 'Blue' AS value
        UNION ALL
        SELECT 3 AS id, 'Hard' AS value
        )
SELECT  ato.id, ato.value
FROM    (
        SELECT  id
        FROM    assets a
        WHERE   a.id <> 1
                AND NOT EXISTS
                (
                SELECT  ata.value
                FROM    attributes ata
                WHERE   ata.id = 1
                EXCEPT
                SELECT  ato.value
                FROM    attributes ato
                WHERE   ato.id = a.id
                )
        ) ao
JOIN    attributes ato
ON      ato.id = ao.id
JOIN    attributes ata
ON      ata.id = 1
        AND ata.value = ato.value
+4

, .

, Asset-A Asset-B:

SELECT [Table 2].Name
FROM [Table 2]
JOIN [Table 1] a ON a.ID = [Table 2].AssetID AND a.Name = 'Asset-A'
JOIN [Table 1] b ON b.ID = [Table 2].AssetID AND b.Name = 'Asset-B'
GROUP BY [Table 2].Name
0
 Select * From Assets A
    Where Exists 
      (Select * From Assets
       Where AssetId <> A.AssetID
          And (Select Count(*)
               From Attributes At1 Join Attributes At2
                  On At1.AssetId <> At2.AssetId
                      And At1.attribute <> At2.Attribute
               Where At1.AssetId = A.AssetId Asset) = 0 )
    And AssetId = 'Asset-A'
0
select at2.asset, count(*)
from       attribute at1
inner join attribute at2 on at1.value = at2.value
where at1.asset =  "Asset-A"
and   at2.asset != "Asset-A"
group by at2.asset
having count(*) = (select count(*) from attribute where asset = "Asset-A");
0

, , "A" ( ):

SELECT Other.ID
FROM Assets Other
WHERE
  Other.AssetID <> 'Asset-A'   -- do not return Asset A as a match to itself
  AND NOT EXISTS (SELECT NULL FROM Attributes AttA WHERE
    AttA.AssetID='Asset-A' 
    AND NOT EXISTS (SELECT NULL FROM Attributes AttOther WHERE
      AttOther.AssetID=Other.ID AND AttOther.AttributeID = AttA.AttributeID
      )
    ) 

I.e., " , A, ".

, , "A":

SELECT Other.ID
FROM Assets Other
WHERE
  Other.AssetID <> 'Asset-A'   -- do not return Asset A as a match to itself
  AND NOT EXISTS (SELECT NULL FROM Attributes AttA WHERE
    AttA.AssetID='Asset-A' 
    AND NOT EXISTS (SELECT NULL FROM Attributes AttOther WHERE
      AttOther.AssetID=Other.ID 
      AND AttOther.AttributeID = AttA.AttributeID
      )
    ) 
  AND NOT EXISTS (SELECT NULL FROM Attributes AttaOther WHERE
    AttaOther.AssetID=Other.ID 
    AND NOT EXISTS (SELECT NULL FROM Attributes AttaA WHERE
      AttaA.AssetID='Asset-A' 
      AND AttaA.AttributeID = AttaOther.AttributeID
      )
   )

I.e., " , A, , , A."

0

, , -a:

select att2.Asset from attribute att1
inner join attribute att2 on att2.Attribute = att1.Attribute and att1.Asset <> att2.Asset
where att1.Asset = 'Asset-A'
group by att2.Asset, att1.Asset
having COUNT(*) = (select COUNT(*) from attribute where Asset=att1.Asset)
0

, , , LINQ, :

var result = from productsNotA in DevProducts
             where  productsNotA.Product != "A" && 
            (
                from productsA in DevProducts
                where productsA.Product == "A"
                select productsA.Attribute
            ).Except
            (
                from productOther in DevProducts
                where productOther.Product == productsNotA.Product
                select productOther.Attribute
            ).Single() == null
            select new {productsNotA.Product};

result.Distinct()

, SQL LinqPad SQL-. :). DevProducts - Product Attribute. , LINQ , , LINQ.

LINQ , ( SQL;))

0

I am using the following DDL

CREATE TABLE Attributes (
    Asset      VARCHAR(100)
    , Name     VARCHAR(100)
    , UNIQUE(Asset, Name)
    )

Second question is simple

SELECT   Name
FROM     Attributes
WHERE    Name IN (SELECT Name FROM Attributes WHERE Asset = 'A')
         AND Asset = 'B'

The first question is no harder

SELECT   Asset
FROM     Attributes
WHERE    Name IN (SELECT Name FROM Attributes WHERE Asset = 'A')
GROUP BY Asset
HAVING   COUNT(*) = (SELECT COUNT(*) FROM FROM Attributes WHERE Asset = 'A')

Edit:

I left the second fragment AND Asset != 'A'from the sentence WHEREfor brevity

0
source

All Articles