Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Get records having different data in 2 rows

Get records having different data in 2 rows

Scheduled Pinned Locked Moved Database
databasequestion
13 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vipul Mehta
    wrote on last edited by
    #1

    I have 2 columns (Account Number & State) with follwing set of records in my table Acc No State 1001 WA 1002 NY 1002 NY 1002 NY 1003 CA 1003 CA 1001 CA 1002 NY I want to pull such Account Numbers that has multiple states associated with that account. Looking at the existing records, output should be as shown below 1001 1002 What query should I write to get this result set?

    Regards, Vipul Mehta

    V J A B N 6 Replies Last reply
    0
    • V Vipul Mehta

      I have 2 columns (Account Number & State) with follwing set of records in my table Acc No State 1001 WA 1002 NY 1002 NY 1002 NY 1003 CA 1003 CA 1001 CA 1002 NY I want to pull such Account Numbers that has multiple states associated with that account. Looking at the existing records, output should be as shown below 1001 1002 What query should I write to get this result set?

      Regards, Vipul Mehta

      V Offline
      V Offline
      Vipul Mehta
      wrote on last edited by
      #2

      Got this resolved

      Create Table T1 (Col1 int, Col2 varchar(50))
      Insert into T1 Select 1001,'WA'
      Insert into T1 Select 1001,'CA'
      Insert into T1 Select 1002,'WA'
      Insert into T1 Select 1001,'WA'
      Select Col1 From
      (
      Select *,ROW_NUMBER()Over(partition by Col1 Order by Col1) RN
      From (Select Col1,Col2 From T1
      Group by Col1,Col2)T1
      )A Where RN>1

      Regards, Vipul Mehta

      1 Reply Last reply
      0
      • V Vipul Mehta

        I have 2 columns (Account Number & State) with follwing set of records in my table Acc No State 1001 WA 1002 NY 1002 NY 1002 NY 1003 CA 1003 CA 1001 CA 1002 NY I want to pull such Account Numbers that has multiple states associated with that account. Looking at the existing records, output should be as shown below 1001 1002 What query should I write to get this result set?

        Regards, Vipul Mehta

        J Offline
        J Offline
        JammoD87
        wrote on last edited by
        #3

        Hi, An alternative way to retrieve the data would be...

        SELECT [Acc No] FROM table
        GROUP BY State
        HAVING COUNT(State) > 1

        Thanks,

        B 1 Reply Last reply
        0
        • V Vipul Mehta

          I have 2 columns (Account Number & State) with follwing set of records in my table Acc No State 1001 WA 1002 NY 1002 NY 1002 NY 1003 CA 1003 CA 1001 CA 1002 NY I want to pull such Account Numbers that has multiple states associated with that account. Looking at the existing records, output should be as shown below 1001 1002 What query should I write to get this result set?

          Regards, Vipul Mehta

          A Offline
          A Offline
          Arun Vasu
          wrote on last edited by
          #4

          Create Table T1 (Col1 int, Col2 varchar(50)) Insert into T1 Select 1001,'WA' Insert into T1 Select 1002,'NY' Insert into T1 Select 1002,'NY' Insert into T1 Select 1002,'NY' Insert into T1 Select 1003,'CA' Insert into T1 Select 1003,'CA' Insert into T1 Select 1001,'CA' Insert into T1 Select 1002,'NY' select distinct(col1) from t1 group by col1,col2 having count(col1)<2 output will come one record. because state only one record have multi state

          M S 2 Replies Last reply
          0
          • A Arun Vasu

            Create Table T1 (Col1 int, Col2 varchar(50)) Insert into T1 Select 1001,'WA' Insert into T1 Select 1002,'NY' Insert into T1 Select 1002,'NY' Insert into T1 Select 1002,'NY' Insert into T1 Select 1003,'CA' Insert into T1 Select 1003,'CA' Insert into T1 Select 1001,'CA' Insert into T1 Select 1002,'NY' select distinct(col1) from t1 group by col1,col2 having count(col1)<2 output will come one record. because state only one record have multi state

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Jammo gave this exact same answer 2 hours earlier and it was a lot more understandable. Try using the formatting tools for placing code, your answer will be much more readable!

            Never underestimate the power of human stupidity RAH

            B 1 Reply Last reply
            0
            • V Vipul Mehta

              I have 2 columns (Account Number & State) with follwing set of records in my table Acc No State 1001 WA 1002 NY 1002 NY 1002 NY 1003 CA 1003 CA 1001 CA 1002 NY I want to pull such Account Numbers that has multiple states associated with that account. Looking at the existing records, output should be as shown below 1001 1002 What query should I write to get this result set?

              Regards, Vipul Mehta

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              Why do you expect "1002" to be among the output? Only "NY" is associated with "1002", though 4 times. But you said "multiple states".

              1 Reply Last reply
              0
              • J JammoD87

                Hi, An alternative way to retrieve the data would be...

                SELECT [Acc No] FROM table
                GROUP BY State
                HAVING COUNT(State) > 1

                Thanks,

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #7

                That causes error message 8120 in Microsoft SQL server (similar message in MS Access):

                Column [Acc No] is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

                J 1 Reply Last reply
                0
                • M Mycroft Holmes

                  Jammo gave this exact same answer 2 hours earlier and it was a lot more understandable. Try using the formatting tools for placing code, your answer will be much more readable!

                  Never underestimate the power of human stupidity RAH

                  B Offline
                  B Offline
                  Bernhard Hiller
                  wrote on last edited by
                  #8

                  Arun's answer is clearly different from Jammo's. And Jammo's SQL statement fails with an error message.

                  M 1 Reply Last reply
                  0
                  • B Bernhard Hiller

                    That causes error message 8120 in Microsoft SQL server (similar message in MS Access):

                    Column [Acc No] is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

                    J Offline
                    J Offline
                    JammoD87
                    wrote on last edited by
                    #9

                    Yeah, slight typo there did it on the quick, will have to read it properly next time!

                    SELECT [Acc No] FROM table
                    GROUP BY [Acc No], State
                    HAVING COUNT(State) > 1

                    Sorry about that :doh:

                    1 Reply Last reply
                    0
                    • A Arun Vasu

                      Create Table T1 (Col1 int, Col2 varchar(50)) Insert into T1 Select 1001,'WA' Insert into T1 Select 1002,'NY' Insert into T1 Select 1002,'NY' Insert into T1 Select 1002,'NY' Insert into T1 Select 1003,'CA' Insert into T1 Select 1003,'CA' Insert into T1 Select 1001,'CA' Insert into T1 Select 1002,'NY' select distinct(col1) from t1 group by col1,col2 having count(col1)<2 output will come one record. because state only one record have multi state

                      S Offline
                      S Offline
                      Simon_Whale
                      wrote on last edited by
                      #10

                      Nice answer. +5 can I suggest that you try to format your code so that it is easier to read? i.e.

                      SELECT DISTINCT(col1)
                      FROM t1
                      GROUP BY col1,col2
                      HAVING COUNT(col1) < 2

                      Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

                      1 Reply Last reply
                      0
                      • B Bernhard Hiller

                        Arun's answer is clearly different from Jammo's. And Jammo's SQL statement fails with an error message.

                        M Offline
                        M Offline
                        Mycroft Holmes
                        wrote on last edited by
                        #11

                        My bad - I did not look close enough at the answer.

                        Never underestimate the power of human stupidity RAH

                        1 Reply Last reply
                        0
                        • V Vipul Mehta

                          I have 2 columns (Account Number & State) with follwing set of records in my table Acc No State 1001 WA 1002 NY 1002 NY 1002 NY 1003 CA 1003 CA 1001 CA 1002 NY I want to pull such Account Numbers that has multiple states associated with that account. Looking at the existing records, output should be as shown below 1001 1002 What query should I write to get this result set?

                          Regards, Vipul Mehta

                          N Offline
                          N Offline
                          Niju1
                          wrote on last edited by
                          #12

                          select [Acc No],count(*) from table group by [Acc No] having count(*)>1

                          1 Reply Last reply
                          0
                          • V Vipul Mehta

                            I have 2 columns (Account Number & State) with follwing set of records in my table Acc No State 1001 WA 1002 NY 1002 NY 1002 NY 1003 CA 1003 CA 1001 CA 1002 NY I want to pull such Account Numbers that has multiple states associated with that account. Looking at the existing records, output should be as shown below 1001 1002 What query should I write to get this result set?

                            Regards, Vipul Mehta

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            SELECT DISTINCT A.Acc_No,count(*)
                            FROM [Test].[dbo].[Table_2] A
                            JOIN [Test].[dbo].[Table_2] B
                            on A.Emp_state = B.Emp_state
                            group by A.Acc_No having COUNT(*)>1

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups