Share via

select only same id and different group data

Lily June 101 Reputation points
2021-09-17T05:04:05.037+00:00

I would like to get same transfer id with different group as the following script

GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TransferDetail](
[Account] [float] NULL,
[TransferId] nvarchar NULL,
[TransferType] nvarchar NULL,
[TransferDate] [datetime] NULL,
[Description] nvarchar NULL,
[Currency] [float] NULL,
[Amount] [float] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[TransferDetail] ([Account], [TransferId], [TransferType], [TransferDate], [Description], [Currency], [Amount]) VALUES (11111, N'ddd', N'From', CAST(0x0000AD9A00000000 AS DateTime), N'Transfer 11111', 702, -300)
INSERT [dbo].[TransferDetail] ([Account], [TransferId], [TransferType], [TransferDate], [Description], [Currency], [Amount]) VALUES (11111, N'aaa', N'From', CAST(0x0000AD9A00000000 AS DateTime), N'Transfer from 11111', 702, -100)
INSERT [dbo].[TransferDetail] ([Account], [TransferId], [TransferType], [TransferDate], [Description], [Currency], [Amount]) VALUES (11111, N'aaa', N'To', CAST(0x0000AD9A00000000 AS DateTime), N'Transfer To 11111', 840, 74.33)
INSERT [dbo].[TransferDetail] ([Account], [TransferId], [TransferType], [TransferDate], [Description], [Currency], [Amount]) VALUES (11111, N'bbb', N'From', CAST(0x0000AD9A00000000 AS DateTime), N'Transfer from 11111', 702, -200)
INSERT [dbo].[TransferDetail] ([Account], [TransferId], [TransferType], [TransferDate], [Description], [Currency], [Amount]) VALUES (11111, N'bbb', N'To', CAST(0x0000AD9A00000000 AS DateTime), N'Transfer To 11111', 840, 148.65)
INSERT [dbo].[TransferDetail] ([Account], [TransferId], [TransferType], [TransferDate], [Description], [Currency], [Amount]) VALUES (22222, N'ccc', N'From', CAST(0x0000AD9A00000000 AS DateTime), N'Transfer From 22222', 840, -200)

Here're the table data

I would like to get highlighted color records

Currently,I extract like that
Query 1: not got need result

select * from Transferdetail where TransferType ='From' and CURRENCY ='702' Or TransferType ='To' and CURRENCY ='840'

Query 2: got need result but I would like know more ways

select * from Transferdetail where TransferType IN ('From','To') and CURRENCY IN ('702','840')
and TransferId In (select TransferId from Transferdetail where TransferType ='To' and CURRENCY ='840' )

pls any way suggest.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


Answer accepted by question author

EchoLiu-MSFT 14,626 Reputation points
2021-09-20T03:00:05.527+00:00

Hi @Lily June ,

Does your FROM-TO only have the four data pairs you mentioned above? If yes, please refer to:

 SELECT * FROM Transferdetail   
 WHERE TransferId IN(  
 SELECT TransferId  FROM Transferdetail  
 WHERE TransferType='From' AND CURRENCY ='702'  
 INTERSECT  
 SELECT TransferId  FROM Transferdetail  
 WHERE TransferType='To'AND CURRENCY IN ('840','978')  
 UNION ALL  
 SELECT TransferId  FROM Transferdetail  
 WHERE TransferType='From' AND CURRENCY IN ('840','978')  
 INTERSECT  
 SELECT TransferId  FROM Transferdetail  
 WHERE TransferType='To'AND CURRENCY ='702')  

Output:
133220-image.png

If you have any question, please feel free to let me know.

Regards
Echo


If the answer is helpful, please click "Accept Answer" and upvote it.

Was this answer helpful?


2 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,621 Reputation points
    2021-09-17T06:29:48.957+00:00

    Not very clear, but I guess you mean something like this:

    select *
    from #Transferdetail AS tdFROM
         INNER JOIN
         #Transferdetail AS tdTO
             on tdFROM.TransferId = tdTO.TransferId
    where tdFROM.TransferType ='From' and tdFROM.CURRENCY ='702'
          AND tdTO.TransferType ='To' and tdTO.CURRENCY ='840'
    

    Was this answer helpful?


  2. EchoLiu-MSFT 14,626 Reputation points
    2021-09-17T06:24:25.803+00:00

    Hi @Lily June ,

    Please check:

    SELECT [Account],[TransferId],[TransferType],  
    [TransferDate],[Description],[Currency],  
    [Amount] FROM (SELECT *,COUNT(TransferId)   
                   OVER(PARTITION BY TransferId) num  
                   FROM Transferdetail) t   
    WHERE num>1  
    

    Or:

    SELECT * FROM Transferdetail   
    WHERE TransferId IN(  
    SELECT TransferId  FROM Transferdetail  
    WHERE TransferType='From'  
    INTERSECT  
    SELECT TransferId  FROM Transferdetail  
    WHERE TransferType='To')  
    

    Or:

    SELECT * FROM Transferdetail   
    WHERE TransferId IN(  
    SELECT TransferId FROM Transferdetail  
    WHERE CURRENCY=702)  
    AND   
    TransferId IN(  
    SELECT TransferId FROM Transferdetail  
    WHERE CURRENCY=840)  
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.