1
votes

Comment obtenir ces enregistrements contenant tous les éléments mentionnés dans la clause where

J'ai une table (mentionnée ci-dessous):

where <some_other_conditions> and (Tags = 'Online' or Tags = 'InActive')

Je veux ces enregistrements qui ont à la fois les balises "Online" et "InActive".

La sortie qui est attendu est:

where <some_other_conditions> and (Tags = 'Online' and Tags = 'InActive')

Les requêtes que j'essayais sont: La requête 1 ne donne aucun résultat et la requête 2 donne tous les autres enregistrements où seul "En ligne" est disponible.

Requête 1:

CityId  StoreId DeviceId    Tags
1234    101     5           Online
1234    101     5           InActive
1234    101     6           Online
1234    101     6           InActive

Requête 2:

CityId  StoreId DeviceId    Tags
1234    101     0           Online
1234    101     2           Online
1234    101     3           Online
1234    101     3           Error
1234    101     4           Online
1234    101     5           Online
1234    101     5           InActive
1234    101     6           Online
1234    101     6           InActive


3 commentaires

Merci @Zhorov pour le formatage correct


Si vous avez une ligne 1234 101 6 Error , quelle sera la sortie attendue?


Merci à vous tous pour vos aimables contributions et le temps consacré à ma demande. J'ai marqué la réponse de @ forpas comme acceptée car ma requête donne le résultat attendu avec des changements très minimes dans ma requête existante.


4 Réponses :


0
votes

Essayez ceci peut être utile.

    SELECT * FROM city WHERE CONCAT(CityId,StoreId ,DeviceId) in
    (
        SELECT CONCAT(CityId,StoreId ,DeviceId) 
            from (
                    SELECT CityId,  StoreId , DeviceId   , Tags, ROW_NUMBER()over(partition by CityId,  StoreId , DeviceId order by CityId,  StoreId,   DeviceId ) rnk
                        FROM city)a
            WHERE (tags ='online' or tags ='InActive')
    and rnk>=2)


0 commentaires

1
votes

Une approche possible consiste à utiliser SUM () fenêtré et COUNT () , sans ORDER BY caluse, puis à utiliser le code> WHERE clause:

Données:

--------------------------------
CityId  StoreId DeviceId    Tags
--------------------------------
1234    101     5           Online
1234    101     5           InActive
1234    101     6           Online
1234    101     6           InActive

Déclaration:

SELECT CityId, StoreId, DeviceId, Tags
FROM (
    SELECT 
        *,
        SUM(CASE WHEN Tags = 'Online' THEN 1 ELSE 0 END) OVER (PARTITION BY CityId, StoreId, DeviceId) AS OnlineCount,
        SUM(CASE WHEN Tags = 'InActive' THEN 1 ELSE 0 END) OVER (PARTITION BY CityId, StoreId, DeviceId) AS InActiveCount,
        COUNT(*) OVER (PARTITION BY CityId, StoreId, DeviceId) AS TotalCount
    FROm #Data
) t
WHERE
    (OnlineCount = 1) AND (InActiveCount = 1) AND (Tags = 'Online' OR Tags = 'InActive')

Résultat: p>

CREATE TABLE #Data (
    CityId int,
    StoreId int,
    DeviceId int,    
    Tags varchar(10)
)
INSERT INTO #Data 
   (CityId, StoreId, DeviceId, Tags)
VALUES
    (1234, 101, 0, 'Online'),
    (1234, 101, 2, 'Online'),
    (1234, 101, 3, 'Online'),
    (1234, 101, 3, 'Error'),
    (1234, 101, 4, 'Online'),
    (1234, 101, 5, 'Online'),
    (1234, 101, 5, 'InActive'),
    (1234, 101, 6, 'Error'),
    (1234, 101, 6, 'Online'),
    (1234, 101, 6, 'InActive')


0 commentaires

1
votes

Avec EXISTS:

> cityid | storeid | deviceid | tags    
> -----: | ------: | -------: | :-------
>   1234 |     101 |        5 | Online  
>   1234 |     101 |        5 | InActive
>   1234 |     101 |        6 | Online  
>   1234 |     101 |        6 | InActive

Voir la démo a >.
Résultats:

select t.* from tablename t
where t.tags in ('Online', 'Inactive')
and exists (
  select 1 from tablename
  where tags in ('Online', 'Inactive') and
  cityid = t.cityid and storeid = t.storeid and deviceid = t.deviceid and tags <> t.tags
)


0 commentaires

0
votes

Hope peut vous aider

Select * Into #DATA From (
    Select '1234' [CityID], '101' [StoreID],  '0' [DeviceID], 'Online' [Tags] Union All
    Select '1234' [CityID], '101' [StoreID],  '2' [DeviceID], 'Online' [Tags] Union All
    Select '1234' [CityID], '101' [StoreID],  '3' [DeviceID], 'Online' [Tags] Union All
    Select '1234' [CityID], '101' [StoreID],  '3' [DeviceID], 'Error' [Tags] Union All
    Select '1234' [CityID], '101' [StoreID],  '4' [DeviceID], 'Online' [Tags] Union All
    Select '1234' [CityID], '101' [StoreID],  '5' [DeviceID], 'Online' [Tags] Union All
    Select '1234' [CityID], '101' [StoreID],  '5' [DeviceID], 'InActive' [Tags] Union All
    Select '1234' [CityID], '101' [StoreID],  '6' [DeviceID], 'Online' [Tags] Union All
    Select '1234' [CityID], '101' [StoreID],  '6' [DeviceID], 'InActive' [Tags] 
) A

Select * From #DATA D
Left Join (
    Select 
        CityID, StoreID, DeviceID, Count(*) [Total] 
    From #DATA
    Where Tags In ('OnLine','InActive')
    Group By CityID, StoreID, DeviceID
    Having Count(*) = 2
) T On D.CityID = T.CityID And D. StoreID = T. StoreID And D. DeviceID = T. DeviceID 
Where T.CityID Is Not Null

Résultat:

entrez la description de l'image ici


0 commentaires