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. SQL Query - write all in one query

SQL Query - write all in one query

Scheduled Pinned Locked Moved Database
databaseagentic-ai
20 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.
  • R Offline
    R Offline
    Raman samineni
    wrote on last edited by
    #1

    Hello Friends I have 3 select statments I need to write all in one query How, is it works in MS SQL in Management Studio Select E.FirstName +' ' + E.LastName as Supervisor INNER JOIN [cgs].dbo.tems h ON(h.Supervisor = E.EmploId) where h.temId = 336 Select E.FirstName +' ' + E.LastName as Agent From [cgs].dbo.Employ E with (NoLock) where E.EmploId = 2305 Select count(*) As Tras From CGCSLF Where Dispo = 'Tras' Select count(*) As Comp From CGCSLF Where Dispo = 'Comp' SELECT AgeID Sum(Hours) As HOURS FromCGCSLF Group By AgeID Raman Thank you in advance

    P M L 3 Replies Last reply
    0
    • R Raman samineni

      Hello Friends I have 3 select statments I need to write all in one query How, is it works in MS SQL in Management Studio Select E.FirstName +' ' + E.LastName as Supervisor INNER JOIN [cgs].dbo.tems h ON(h.Supervisor = E.EmploId) where h.temId = 336 Select E.FirstName +' ' + E.LastName as Agent From [cgs].dbo.Employ E with (NoLock) where E.EmploId = 2305 Select count(*) As Tras From CGCSLF Where Dispo = 'Tras' Select count(*) As Comp From CGCSLF Where Dispo = 'Comp' SELECT AgeID Sum(Hours) As HOURS FromCGCSLF Group By AgeID Raman Thank you in advance

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I'm unclear what you require. If you are using the ADO.net classes in SqlClient then you can put all those in one Command -- just separate them with semi-colons (;). You can then use ExecuteReader to get a DataReader to read the results -- use the NextResult method to advance to the results from the next statement. I'm fairly sure that DataAdapters will handle it as well, but I haven't used one for several years. If you need to do something else, then please clarify your question.

      R 1 Reply Last reply
      0
      • R Raman samineni

        Hello Friends I have 3 select statments I need to write all in one query How, is it works in MS SQL in Management Studio Select E.FirstName +' ' + E.LastName as Supervisor INNER JOIN [cgs].dbo.tems h ON(h.Supervisor = E.EmploId) where h.temId = 336 Select E.FirstName +' ' + E.LastName as Agent From [cgs].dbo.Employ E with (NoLock) where E.EmploId = 2305 Select count(*) As Tras From CGCSLF Where Dispo = 'Tras' Select count(*) As Comp From CGCSLF Where Dispo = 'Comp' SELECT AgeID Sum(Hours) As HOURS FromCGCSLF Group By AgeID Raman Thank you in advance

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

        Raman samineni wrote:

        I have 3 select statments

        Funny I count 5, the ability to count is reasonably fundamental to stating your problem. You also need to be much clearer what your goal is. Do some reading on UNION and UNION ALL, these will help. Your queries must have the same fields and formats so your first query should be something like

        Select E.FirstName +' ' + E.LastName as FullName, 'Supervisor' as TypeName
        INNER JOIN [cgs].dbo.tems h ON(h.Supervisor = E.EmploId)
        where h.temId = 336

        Never underestimate the power of human stupidity RAH

        R 1 Reply Last reply
        0
        • M Mycroft Holmes

          Raman samineni wrote:

          I have 3 select statments

          Funny I count 5, the ability to count is reasonably fundamental to stating your problem. You also need to be much clearer what your goal is. Do some reading on UNION and UNION ALL, these will help. Your queries must have the same fields and formats so your first query should be something like

          Select E.FirstName +' ' + E.LastName as FullName, 'Supervisor' as TypeName
          INNER JOIN [cgs].dbo.tems h ON(h.Supervisor = E.EmploId)
          where h.temId = 336

          Never underestimate the power of human stupidity RAH

          R Offline
          R Offline
          Raman samineni
          wrote on last edited by
          #4

          I put Union and Union all, it does not work. Thanks Regards Raman

          M 1 Reply Last reply
          0
          • P PIEBALDconsult

            I'm unclear what you require. If you are using the ADO.net classes in SqlClient then you can put all those in one Command -- just separate them with semi-colons (;). You can then use ExecuteReader to get a DataReader to read the results -- use the NextResult method to advance to the results from the next statement. I'm fairly sure that DataAdapters will handle it as well, but I haven't used one for several years. If you need to do something else, then please clarify your question.

            R Offline
            R Offline
            Raman samineni
            wrote on last edited by
            #5

            hello sir, I call this query in the program, before I do, I am testing query in the SQL management studio. In program Code is like that String query = @"SELECT t.firstName + '' + t.LastName Regards Raman

            P 1 Reply Last reply
            0
            • R Raman samineni

              I put Union and Union all, it does not work. Thanks Regards Raman

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

              Did you do some reading on how to use UNION or did you just stick it in between the select queries. RTFM

              Never underestimate the power of human stupidity RAH

              R 1 Reply Last reply
              0
              • R Raman samineni

                hello sir, I call this query in the program, before I do, I am testing query in the SQL management studio. In program Code is like that String query = @"SELECT t.firstName + '' + t.LastName Regards Raman

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I don't think you finished what you intended to write, but what you did write looks bad.

                1 Reply Last reply
                0
                • R Raman samineni

                  Hello Friends I have 3 select statments I need to write all in one query How, is it works in MS SQL in Management Studio Select E.FirstName +' ' + E.LastName as Supervisor INNER JOIN [cgs].dbo.tems h ON(h.Supervisor = E.EmploId) where h.temId = 336 Select E.FirstName +' ' + E.LastName as Agent From [cgs].dbo.Employ E with (NoLock) where E.EmploId = 2305 Select count(*) As Tras From CGCSLF Where Dispo = 'Tras' Select count(*) As Comp From CGCSLF Where Dispo = 'Comp' SELECT AgeID Sum(Hours) As HOURS FromCGCSLF Group By AgeID Raman Thank you in advance

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

                  Raman samineni wrote:

                  I need to write all in one query

                  What would you gain from having it in a single statement? As PIEBALD stated, you can put them in a single SqlCommand and execute them.

                  • The first Sql-statement will not work. Test it again, it lacks a FROM clause.

                  • you could embed the count as a sub-select, like this;

                    SELECT E.FirstName +' ' + E.LastName AS Agent
                          ,(SELECT COUNT(\*) FROM CGCSLF WHERE Dispo = 'Comp') AS \[Comp\]
                     FROM \[cgs\].dbo.Employ E with (NoLock)
                    WHERE E.EmploId = 2305
                    
                  • What the ELEPHANT is that NOLOCK doing there??

                  • You need to pay attention to your formatting

                  • EmploId is redundant; you're already in the "E" table, just name the damn thing "Id". That way all keys are named Id, saves some time typing and thinking, less chances on a typo, and a lot clearer when reading the code.

                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                  R 4 Replies Last reply
                  0
                  • L Lost User

                    Raman samineni wrote:

                    I need to write all in one query

                    What would you gain from having it in a single statement? As PIEBALD stated, you can put them in a single SqlCommand and execute them.

                    • The first Sql-statement will not work. Test it again, it lacks a FROM clause.

                    • you could embed the count as a sub-select, like this;

                      SELECT E.FirstName +' ' + E.LastName AS Agent
                            ,(SELECT COUNT(\*) FROM CGCSLF WHERE Dispo = 'Comp') AS \[Comp\]
                       FROM \[cgs\].dbo.Employ E with (NoLock)
                      WHERE E.EmploId = 2305
                      
                    • What the ELEPHANT is that NOLOCK doing there??

                    • You need to pay attention to your formatting

                    • EmploId is redundant; you're already in the "E" table, just name the damn thing "Id". That way all keys are named Id, saves some time typing and thinking, less chances on a typo, and a lot clearer when reading the code.

                    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                    R Offline
                    R Offline
                    Raman samineni
                    wrote on last edited by
                    #9

                    Hello sir Thank you. My out put was Supervisor- AgentName-Hours-Tran-Completed, when I ran the qry i need to get this report. Regards Raman

                    L 1 Reply Last reply
                    0
                    • M Mycroft Holmes

                      Did you do some reading on how to use UNION or did you just stick it in between the select queries. RTFM

                      Never underestimate the power of human stupidity RAH

                      R Offline
                      R Offline
                      Raman samineni
                      wrote on last edited by
                      #10

                      Hello sir, Just stick in between the select queries. I dont want use Union , Because this qry goes to in the program. Regards Raman

                      1 Reply Last reply
                      0
                      • R Raman samineni

                        Hello sir Thank you. My out put was Supervisor- AgentName-Hours-Tran-Completed, when I ran the qry i need to get this report. Regards Raman

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

                        Raman samineni wrote:

                        when I ran the qry i need to get this report.

                        You could put the other query in there too, as a subquery. The other queries might be combined, depending on "what" you are selecting from. I have not seen a corrected version of that code yet; I suggest you start writing and testing :)

                        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                        1 Reply Last reply
                        0
                        • L Lost User

                          Raman samineni wrote:

                          I need to write all in one query

                          What would you gain from having it in a single statement? As PIEBALD stated, you can put them in a single SqlCommand and execute them.

                          • The first Sql-statement will not work. Test it again, it lacks a FROM clause.

                          • you could embed the count as a sub-select, like this;

                            SELECT E.FirstName +' ' + E.LastName AS Agent
                                  ,(SELECT COUNT(\*) FROM CGCSLF WHERE Dispo = 'Comp') AS \[Comp\]
                             FROM \[cgs\].dbo.Employ E with (NoLock)
                            WHERE E.EmploId = 2305
                            
                          • What the ELEPHANT is that NOLOCK doing there??

                          • You need to pay attention to your formatting

                          • EmploId is redundant; you're already in the "E" table, just name the damn thing "Id". That way all keys are named Id, saves some time typing and thinking, less chances on a typo, and a lot clearer when reading the code.

                          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                          R Offline
                          R Offline
                          Raman samineni
                          wrote on last edited by
                          #12

                          hello sir, How all three statments be written as one query. Regards Raman

                          1 Reply Last reply
                          0
                          • L Lost User

                            Raman samineni wrote:

                            I need to write all in one query

                            What would you gain from having it in a single statement? As PIEBALD stated, you can put them in a single SqlCommand and execute them.

                            • The first Sql-statement will not work. Test it again, it lacks a FROM clause.

                            • you could embed the count as a sub-select, like this;

                              SELECT E.FirstName +' ' + E.LastName AS Agent
                                    ,(SELECT COUNT(\*) FROM CGCSLF WHERE Dispo = 'Comp') AS \[Comp\]
                               FROM \[cgs\].dbo.Employ E with (NoLock)
                              WHERE E.EmploId = 2305
                              
                            • What the ELEPHANT is that NOLOCK doing there??

                            • You need to pay attention to your formatting

                            • EmploId is redundant; you're already in the "E" table, just name the damn thing "Id". That way all keys are named Id, saves some time typing and thinking, less chances on a typo, and a lot clearer when reading the code.

                            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                            R Offline
                            R Offline
                            Raman samineni
                            wrote on last edited by
                            #13

                            how I will put in the sql Command all three query Thanks Raman

                            L 1 Reply Last reply
                            0
                            • R Raman samineni

                              how I will put in the sql Command all three query Thanks Raman

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

                              Raman samineni wrote:

                              how I will put in the sql Command all three query

                              Do you need them in a single "sql statement", or do you need them in a single "SqlCommand"? For either way you have been given a solution - did they not work, and if so, what went wrong?

                              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                              R 1 Reply Last reply
                              0
                              • L Lost User

                                Raman samineni wrote:

                                how I will put in the sql Command all three query

                                Do you need them in a single "sql statement", or do you need them in a single "SqlCommand"? For either way you have been given a solution - did they not work, and if so, what went wrong?

                                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                R Offline
                                R Offline
                                Raman samineni
                                wrote on last edited by
                                #15

                                send me both, No problem Thanks Raman

                                L 1 Reply Last reply
                                0
                                • R Raman samineni

                                  send me both, No problem Thanks Raman

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

                                  Raman samineni wrote:

                                  send me both, No problem

                                  AFAIK, we're volunteers here. Why should I provide two pieces of code?? The first query from your list is simply invalid, as it lacks a FROM statement. You can search on the internet what a "subquery" is and how to write one, and try to combine that code as suggested. Also reread PIEBALDs' suggestions, there's a good reason they get upvoted every time. Good luck, you'll need it X|

                                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    Raman samineni wrote:

                                    I need to write all in one query

                                    What would you gain from having it in a single statement? As PIEBALD stated, you can put them in a single SqlCommand and execute them.

                                    • The first Sql-statement will not work. Test it again, it lacks a FROM clause.

                                    • you could embed the count as a sub-select, like this;

                                      SELECT E.FirstName +' ' + E.LastName AS Agent
                                            ,(SELECT COUNT(\*) FROM CGCSLF WHERE Dispo = 'Comp') AS \[Comp\]
                                       FROM \[cgs\].dbo.Employ E with (NoLock)
                                      WHERE E.EmploId = 2305
                                      
                                    • What the ELEPHANT is that NOLOCK doing there??

                                    • You need to pay attention to your formatting

                                    • EmploId is redundant; you're already in the "E" table, just name the damn thing "Id". That way all keys are named Id, saves some time typing and thinking, less chances on a typo, and a lot clearer when reading the code.

                                    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                    R Offline
                                    R Offline
                                    Raman samineni
                                    wrote on last edited by
                                    #17

                                    hello sir, My OUT PUT Report was SuperName Agent Name Hours Trans Compl Thanks Raman

                                    L 1 Reply Last reply
                                    0
                                    • R Raman samineni

                                      hello sir, My OUT PUT Report was SuperName Agent Name Hours Trans Compl Thanks Raman

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

                                      I can't help you, sorry. We gave quite some answers and options, and your response was "write them both for me". Unless you can answer whether you need them in a single SqlCommand or as a single Sql-statement, there will not be any progress. Now, it doesn't add anything of value to have them in a single statement, but I'm willing to help. When I say "help", I mean that I can point out how some things are done, it does not mean that I am going to write YOUR code.

                                      R 1 Reply Last reply
                                      0
                                      • L Lost User

                                        I can't help you, sorry. We gave quite some answers and options, and your response was "write them both for me". Unless you can answer whether you need them in a single SqlCommand or as a single Sql-statement, there will not be any progress. Now, it doesn't add anything of value to have them in a single statement, but I'm willing to help. When I say "help", I mean that I can point out how some things are done, it does not mean that I am going to write YOUR code.

                                        R Offline
                                        R Offline
                                        Raman samineni
                                        wrote on last edited by
                                        #19

                                        yes, that is true, I need them in SQLCOMMAND or String query in the program I appreciated Raman

                                        L 1 Reply Last reply
                                        0
                                        • R Raman samineni

                                          yes, that is true, I need them in SQLCOMMAND or String query in the program I appreciated Raman

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

                                          You're welcome :)

                                          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                          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