Results 1 to 14 of 14

Thread: Need Help With SQL Query

  1. #1
    Member
    Join Date
    Jun 12
    Posts
    41

    Need Help With SQL Query

    I really need some help. I have two different tables, but I wont to join the two tables together all in one select statement. I have provided in attachment, on how the table look now, in how I'm trying to get the tables.



    SQL Table Example.txt


    Table 1:
    SELECT [SerialNumber]
    ,[In Stock]
    ,SerialNumber - [In Stock] as [Short Of]
    FROM [phabsproduction].[dbo].[Stock_Inventory]
    where
    (SerialNumber - [In Stock])<0
    order by
    [Short Of] desc



    Table 2:
    SELECT PartNumber,Count(PartNumber) AS Total FROM Stock_Inventory WHERE [PartNumber] like '82480-%' or [PartNumber] like '83480-%' GROUP BY PartNumber


    WHAT I WILL NEED FROM THE TABLE 1 ARE THE FOLLOWING; I WILL NEED THIS PART ADDED IN THE TABLE 2 QUERY;

    ,SerialNumber - [In Stock] as [Short Of]
    FROM [phabsproduction].[dbo].[Stock_Inventory]
    where
    (SerialNumber - [In Stock])<0
    order by
    [Short Of] desc

  2. #2
    PowerPoster
    Join Date
    Feb 12
    Location
    West Virginia
    Posts
    4,978

    Re: Need Help With SQL Query

    Sounds like you need an Inner Join statement

    Code:
    Select table1.Field1, Table2.Field1 From Table1 Inner Join Table2 on Table2.KeyField=Table1.KeyField Where ......

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,624

    Re: Need Help With SQL Query

    Thread moved to the 'Database Development' forum - which is where you should always post SQL questions (while SQL can be used in VB, it is certainly not specific to VB)

  4. #4
    Member
    Join Date
    Jun 12
    Posts
    41

    Re: Need Help With SQL Query

    Is there a way, I dont have to have two tables. Becasue what im trying to prevent is making two tables. But have all this in a select statement. To prevent confusing, all is on one table so I just need one select query that can do all what i provided above.

  5. #5
    Member
    Join Date
    Jun 12
    Posts
    41

    Re: Need Help With SQL Query

    I have those two query for example what I need in one query, basically there's not 2 tables. Just one table, which i have a select statement to get things I need. but I nned both select statement in one select statement to get me the same results two select query would give me. If that make sense.

  6. #6
    Lively Member
    Join Date
    Oct 08
    Location
    Califorina
    Posts
    127

    Re: Need Help With SQL Query

    Quote Originally Posted by Focus12 View Post
    Is there a way, I dont have to have two tables. Becasue what im trying to prevent is making two tables. But have all this in a select statement. To prevent confusing, all is on one table so I just need one select query that can do all what i provided above.
    After reading your question again I think you're only query data from ONE data but you have two queries that you would like to rewrite as one? Is this correct?

  7. #7
    Member
    Join Date
    Jun 12
    Posts
    41

    Re: Need Help With SQL Query

    Both of theses query that are on the same table, but im trying to combine both query together. In get the same resulta on one query, as I was running in two query.

    Query 1:
    SELECT [SerialNumber]
    ,[In Stock]
    ,SerialNumber - [In Stock] as [Short Of]
    FROM [production].[dbo].[Stock_Inventory]
    where
    (SerialNumber - [In Stock])<0
    order by
    [Short Of] desc



    Query2:
    SELECT PartNumber,Count(PartNumber) AS Total FROM Stock_Inventory WHERE [PartNumber] like '81-%' or [PartNumber] like '82-%' GROUP BY PartNumber;

  8. #8
    Lively Member
    Join Date
    Oct 08
    Location
    Califorina
    Posts
    127

    Re: Need Help With SQL Query

    Off the top of my head id try this.


    Code:
    SELECT PartNumber, Count(PartNumber) AS Total
           , SerialNumber - [In Stock] as [Short Of]
     WHERE [PartNumber] like '82480-%' or [PartNumber] like '83480-%'
         AND (SerialNumber - [In Stock]) < 0
    GROUP BY PartNumber
    Last edited by smendoza; Aug 1st, 2012 at 01:12 PM. Reason: don't group by serial number.

  9. #9
    Lively Member
    Join Date
    Oct 08
    Location
    Califorina
    Posts
    127

    Re: Need Help With SQL Query

    if that doesn't work show us the schema of the table with two records or so.

  10. #10
    Member
    Join Date
    Jun 12
    Posts
    41

    Re: Need Help With SQL Query

    That works but it doest count the part number like it was; SELECT PartNumber, Count(PartNumber) AS Total
    On that part I will need to collect all data that are the same on the table, it give the total. Just like I have below;

    PartNumber Total
    82480-EM 2
    83480-EM 2

  11. #11
    Lively Member
    Join Date
    Oct 08
    Location
    Califorina
    Posts
    127

    Re: Need Help With SQL Query

    Can you show us the table structure. It's hard to help (well for me) when you don't know what the table structure looks like. I think you will need to use both queries and a inner join.

  12. #12
    Member
    Join Date
    Jun 12
    Posts
    41

    Re: Need Help With SQL Query

    QUERY NEED HELP.doc





    I need help, but I provided a attachment, that explain what i need help in.

    EXAMPLE: PartNumber Total Limit Shorten
    83480-EM 2 213 211
    82470-EM 2 192 190
    Last edited by Focus12; Aug 1st, 2012 at 08:45 PM.

  13. #13
    Lively Member
    Join Date
    Oct 08
    Location
    Califorina
    Posts
    127

    Re: Need Help With SQL Query

    Well in your doc you show you're query data from TWO different tables. So yes, like you said you need to use a JOIN, a INNER JOIN is what you want to use. I say give it a try it sounds like you know what you need to do.

  14. #14
    New Member
    Join Date
    Jan 13
    Posts
    1

    Re: Need Help With SQL Query

    For SQL Server 2008:

    What is the procedure to delete some data from Lock Table?
    We can't use NOLOCK for Update, delete & Insert for that.

    A table Name like Business_History which is in Transaction Lock.
    Want to delete data where Proposal_Form_No='ABC'. If I Execute Select Operation With NOLOCK it shows 5 rows of data. But Can't delete them.

    Please Help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •