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. Replace with integer (1,2,3...) Problem in SQL Query [modified]

Replace with integer (1,2,3...) Problem in SQL Query [modified]

Scheduled Pinned Locked Moved Database
databasehelpcsssql-serversysadmin
9 Posts 4 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
    Varun Sareen
    wrote on last edited by
    #1

    Dear Friends, I have a query in which there is a sub query that is fetching a no of rows (single column); it may be one,two..upto six (not more than that). It is using for xml path( '' ) feature of SQL server in order to display the values as comma separated values. The query is as follows:-

    select distinct (select top 1 CLAIMMASTERID from ClaimMaster where encounterid='87B6D11C-3680-44A5-B1DB-AB16B3907AA6') ,EVC.EncounterID,EVC.CPT,(select (select ICDCode + ',' as [text()] from VisitTypeICDCPT soi where soi.VisitTypeID = t.VisitTypeID and VisitTypeID=VTI.VisitTypeID and encounterid='87B6D11C-3680-44A5-B1DB-AB16B3907AA6' order by VisitTypeID for xml path( '' )) from (select distinct VisitTypeID from VisitTypeICDCPT where VisitTypeID=VTI.VisitTypeID and encounterid='87B6D11C-3680-44A5-B1DB-AB16B3907AA6') as t )DiagnosisCode,EVC.Fee,convert(varchar(10),'2010-12-14 00:00:00.000',101) from EncounterVisitCode EVC inner join VisitTypeICDCPT VTI on EVC.encounterid=VTI.encounterid where EVC.encounterid='87B6D11C-3680-44A5-B1DB-AB16B3907AA6'

    Here the sub query(starting from :-(select (select ICDCode...)) which is fetching the data from the database and the where "Diagnosis Code" is the column name. The data query is fetching is like this:- 004.12,001.20,...upto six values (may be less). Here i want the values to be replaced by integer values depending upon the no of values being fetched by sub-query i.e., 1,2, if values in no. are two, 1,2,3, if values in no. are three and so on... I am not able to find any way out to solve this problem. If someone could help me resolve this problem i would be very grateful to him/her. Thanks Varun Sareen

    modified on Friday, December 17, 2010 6:53 AM

    H 1 Reply Last reply
    0
    • V Varun Sareen

      Dear Friends, I have a query in which there is a sub query that is fetching a no of rows (single column); it may be one,two..upto six (not more than that). It is using for xml path( '' ) feature of SQL server in order to display the values as comma separated values. The query is as follows:-

      select distinct (select top 1 CLAIMMASTERID from ClaimMaster where encounterid='87B6D11C-3680-44A5-B1DB-AB16B3907AA6') ,EVC.EncounterID,EVC.CPT,(select (select ICDCode + ',' as [text()] from VisitTypeICDCPT soi where soi.VisitTypeID = t.VisitTypeID and VisitTypeID=VTI.VisitTypeID and encounterid='87B6D11C-3680-44A5-B1DB-AB16B3907AA6' order by VisitTypeID for xml path( '' )) from (select distinct VisitTypeID from VisitTypeICDCPT where VisitTypeID=VTI.VisitTypeID and encounterid='87B6D11C-3680-44A5-B1DB-AB16B3907AA6') as t )DiagnosisCode,EVC.Fee,convert(varchar(10),'2010-12-14 00:00:00.000',101) from EncounterVisitCode EVC inner join VisitTypeICDCPT VTI on EVC.encounterid=VTI.encounterid where EVC.encounterid='87B6D11C-3680-44A5-B1DB-AB16B3907AA6'

      Here the sub query(starting from :-(select (select ICDCode...)) which is fetching the data from the database and the where "Diagnosis Code" is the column name. The data query is fetching is like this:- 004.12,001.20,...upto six values (may be less). Here i want the values to be replaced by integer values depending upon the no of values being fetched by sub-query i.e., 1,2, if values in no. are two, 1,2,3, if values in no. are three and so on... I am not able to find any way out to solve this problem. If someone could help me resolve this problem i would be very grateful to him/her. Thanks Varun Sareen

      modified on Friday, December 17, 2010 6:53 AM

      H Offline
      H Offline
      Hiren solanki
      wrote on last edited by
      #2

      Please wrap your query in <pre lang="sql"><pre> TAG. so that we can see something from that mess.

      Regards, Hiren. Microsoft Dynamics CRM

      My Recent Article: - Way to know which control have raised PostBack[^]

      C 1 Reply Last reply
      0
      • H Hiren solanki

        Please wrap your query in <pre lang="sql"><pre> TAG. so that we can see something from that mess.

        Regards, Hiren. Microsoft Dynamics CRM

        My Recent Article: - Way to know which control have raised PostBack[^]

        C Offline
        C Offline
        Corporal Agarn
        wrote on last edited by
        #3

        I copied it out and reformatted, then decided I did not have enough time to decipher all the queries within queries.

        J V 2 Replies Last reply
        0
        • C Corporal Agarn

          I copied it out and reformatted, then decided I did not have enough time to decipher all the queries within queries.

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          djj55 wrote:

          I copied it out and reformatted, then decided I did not have enough time to decipher all the queries within queries.

          Agreed! But reformatting takes 10 seconds, so if someone else wants to give it a shot:

          SELECT DISTINCT (SELECT TOP 1 claimmasterid
          FROM claimmaster
          WHERE encounterid = '87B6D11C-3680-44A5-B1DB-AB16B3907AA6'),
          evc.encounterid,
          evc.cpt,
          (SELECT (SELECT icdcode + ',' AS [text()]
          FROM visittypeicdcpt soi
          WHERE soi.visittypeid = t.visittypeid
          AND visittypeid = vti.visittypeid
          AND encounterid =
          '87B6D11C-3680-44A5-B1DB-AB16B3907AA6'
          ORDER BY visittypeid
          FOR XML PATH( '' ))
          FROM (SELECT DISTINCT visittypeid
          FROM visittypeicdcpt
          WHERE visittypeid = vti.visittypeid
          AND encounterid =
          '87B6D11C-3680-44A5-B1DB-AB16B3907AA6') AS t
          )
          diagnosiscode,
          evc.fee,
          CONVERT(VARCHAR(10), '2010-12-14 00:00:00.000', 101)
          FROM encountervisitcode evc
          INNER JOIN visittypeicdcpt vti
          ON evc.encounterid = vti.encounterid
          WHERE evc.encounterid = '87B6D11C-3680-44A5-B1DB-AB16B3907AA6'

          "When did ignorance become a point of view" - Dilbert

          1 Reply Last reply
          0
          • C Corporal Agarn

            I copied it out and reformatted, then decided I did not have enough time to decipher all the queries within queries.

            V Offline
            V Offline
            Varun Sareen
            wrote on last edited by
            #5

            Dear djj55, I have formatted the query. Please go through it and if you have any answer to it then kindly let me know. Thanks & Regards Varun Sareen

            H C 2 Replies Last reply
            0
            • V Varun Sareen

              Dear djj55, I have formatted the query. Please go through it and if you have any answer to it then kindly let me know. Thanks & Regards Varun Sareen

              H Offline
              H Offline
              Hiren solanki
              wrote on last edited by
              #6

              I think you haven't applied lang="sql" Yet. It's not indented.

              Regards, Hiren.

              My Recent Article: - Way to know which control have raised PostBack
              My Recent Tip/Trick: - Remove HTML Tag, get plain Text

              V 1 Reply Last reply
              0
              • H Hiren solanki

                I think you haven't applied lang="sql" Yet. It's not indented.

                Regards, Hiren.

                My Recent Article: - Way to know which control have raised PostBack
                My Recent Tip/Trick: - Remove HTML Tag, get plain Text

                V Offline
                V Offline
                Varun Sareen
                wrote on last edited by
                #7

                Dear Hiren, I have made the changes as required. Kindly provide me with some answer, if you have. Thanks Varun Sareen

                H 1 Reply Last reply
                0
                • V Varun Sareen

                  Dear Hiren, I have made the changes as required. Kindly provide me with some answer, if you have. Thanks Varun Sareen

                  H Offline
                  H Offline
                  Hiren solanki
                  wrote on last edited by
                  #8

                  You can use ROW_NUMBER a Inbuilt ranking function in SQL to get only row numbers. See MSDN[^].

                  Regards, Hiren.

                  My Recent Article: - Way to know which control have raised PostBack
                  My Recent Tip/Trick: - Remove HTML Tag, get plain Text

                  1 Reply Last reply
                  0
                  • V Varun Sareen

                    Dear djj55, I have formatted the query. Please go through it and if you have any answer to it then kindly let me know. Thanks & Regards Varun Sareen

                    C Offline
                    C Offline
                    Corporal Agarn
                    wrote on last edited by
                    #9

                    As stated before: "I copied it out and reformatted, then decided I did not have enough time to decipher all the queries within queries." Sorry

                    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