Thursday 19 January 2017

SQL Query to select the rows with same id but different value in another column.

select the rows with same id but different value in another column

Description:

In this example we explain that how to select the rows with same id but different value in another column (parent column) in SQL Server. Or how to get all rows that have same ID but different value in other column. Or select only those records that have different/multiple values for a particular column in sql server. Or find record in same table with different company id but some other some values.
Query:

For example suppose I have data like

+------+------+
|ARIDNR|LIEFNR|
+------+------+
|1     |A     |
+------+------+
|2     |A     |
+------+------+
|3     |A     |
+------+------+
|1     |B     |
+------+------+
|2     |B     |
+------+------+
And I want output like this
+------+------+
|ARIDNR|LIEFNR|
+------+------+
|1     |A     |
+------+------+
|1     |B     |
+------+------+
|2     |A     |
+------+------+
|2     |B     |
+------+------+

Below is the query to get the result like
SELECT * FROM Table WHERE ARIDNR IN (SELECT ARIDNR FROM Table GROUP BY ARIDNR HAVING COUNT (distinct LIEFNR) > 1)

In my case the query is
SELECT *
FROM ReinforcementRequest
WHERE ReinforcementOrderId IN (
    SELECT ReinforcementOrderId
    FROM ReinforcementRequest
    GROUP BY ReinforcementOrderId
    HAVING COUNT(distinct CreditorId) > 1
)

SELECT * FROM ReinforcementRequest WHERE ReinforcementOrderId IN (
    SELECT a.ReinforcementOrderId FROM ReinforcementRequest a
    JOIN ReinforcementRequest b on b.ReinforcementOrderId = a.ReinforcementOrderId AND b.CreditorId <> a.CreditorId


This entry was posted in :

9 comments:

  1. Your blog is really very well interesting and i am looking forward more things from your blog.


    Tooth Braces In Chennai | Dental Hospital in chennai | Best Root Canal Treatment In Chennai

    ReplyDelete
    Replies
    1. thanks. also keep visiting updated post on blog.

      Delete
  2. thanks. also keep visiting updated post on blog.

    ReplyDelete
  3. thanks. also keep visiting updated post on blog.

    ReplyDelete
  4. thanks. also keep visiting updated post on blog.

    ReplyDelete
  5. thanks. also keep visiting updated post on blog.

    ReplyDelete
  6. thanks. also keep visiting updated post on blog.

    ReplyDelete
  7. I think great site for these post and i am read the most of contents have useful for my Carrier.Thanks for these useful information.Any information are commands like to share him.

    http://www.iperidigi.com/in/chennai/seo-company-in-chennai/
    http://www.iperidigi.com/seo-company-in-india/
    http://www.iperidigi.com/in/chennai/digital-marketing-company-in-chennai/
    http://www.iperidigi.com/digital-marketing-company-in-india/
    http://www.iperidigi.com/web-development-company-in-india/
    http://www.iperidigi.com/in/chennai/web-design-company-in-chennai/
    http://www.iperidigi.com/android-app-development-company-in-chennai/
    http://www.iperidigi.com/mobile-app-development-companies-in-chennai/
    http://www.iperidigi.com/ios-app-development-company-in-chennai

    ReplyDelete