Results 1 to 23 of 23

Thread: SQL Statement Count Total Quantity

  1. #1

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    SQL Statement Count Total Quantity

    Anyone has any idea on how to calculate the transaction date by model and then count out the total quantity for that transaction date for that day?
    For example:-
    I want to set a target 2th March 2005 starting to count for the transaction date and time.Then,it will pick out only the transaction date(trans_date) and time which i set in the database for me by following the certain model.Anyone can tell me how to write for the coding?Thank you.
    Human Knowledge Belongs To The World

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SQL Statement Count Total Quantity

    Something like...
    VB Code:
    1. SELECT Count(trans_date) As Transactions FROM Table1 GROUP BY trans_date HAVING trans_date = '03/02/2005'
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    Thanks lot,Robdog
    Human Knowledge Belongs To The World

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: SQL Statement Count Total Quantity

    No prob. Glad to help

    Ps, dont forget to Resolve your thread
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    May I know what's wrong with my coding below:-

    sSQL = "SELECT StartDate FROM DateTime"
    Set oRs = oSQL.Execute(sSQL)

    sSQL = "insert into PackQty Select * from Model_Defined_View where trans_date >= '" & StartDate & "'"
    Set oRs2 = oShip.Execute(sSQL)

    Can the Set oRs in two diff SQL Statement be the same without setting it to oRs and oRs1?When,it debug,it couldnt get my StartDate.So,it pick up the irrelevant trans_date from Model_Defined_View also.
    Human Knowledge Belongs To The World

  6. #6
    Addicted Member
    Join Date
    Aug 2004
    Posts
    176

    Re: SQL Statement Count Total Quantity

    It doesn't recognise StartDate without a reference to the recordset
    Use:
    VB Code:
    1. sSQL = "SELECT StartDate FROM DateTime"
    2. Set oRs = oSQL.Execute(sSQL)
    3.  
    4. sSQL = "insert into PackQty Select * from Model_Defined_View where trans_date >= '" &[B] oRs!StartDate [/B] & "'"
    5. Set oRs2 = oShip.Execute(sSQL)

  7. #7

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    Thanks,Flair.

    However,I encounter another problems then.I will display out all the model and total quantity in the datagrid by two columns.When the total quantity is less than the set TargetQty,it will display out vbRed and if the total quantity meet the TargetQty,it will display vbGreen.To get the Total quantity,Qty should add the Exceed Packout for that day as shown in the code below so that it can compared with the TargetQty.But,dunno why some of the data should be green,but it turns to Red because it doesn't add up my Qty.

    Maybe my explanation is not good enough.Below attached is the coding that I do.Hope anyone here can solve the problem for me.Thank You.

    VB Code:
    1. Private Sub DisplayGrid ()
    2. .....
    3.  
    4. fg.Clear
    5. sSQL = "SELECT * FROM PackQty_View"
    6. Set oRs1 = oShip.Execute(sSQL)
    7. 'Run FROM PackQty_View and get ModelDefined(Ex:- I730 and I90) and Qty by 2 and 11
    8.  
    9.   Do Until oRs1.EOF
    10.    
    11.         fg.Rows = oRs1.RecordCount + 1
    12.         fg.TextMatrix(Row, 1) = oRs1!Qty
    13.         Qty = oRs1!Qty
    14.         ModelDefined = oRs1!ModelDefined
    15.         oRs1.MoveNext
    16.     Loop
    17.  
    18. DisplaySQL = "SELECT Model,TargetQty,Exceed FROM DataGrid"
    19. Set rst = oSQL.Execute(DisplaySQL)
    20.  
    21.     fg.ColWidth(0) = 2900
    22.     fg.ColWidth(1) = 1600
    23.    
    24.     fg.ColAlignment(0) = flexAlignLeftCenter
    25.     fg.ColAlignment(1) = flexAlignLeftCenter
    26.  
    27.     Row = 0
    28.    
    29.     fg.TextMatrix(Row, 0) = "Model"
    30.     fg.TextMatrix(Row, 1) = "Total Quantity"
    31.    
    32.     Row = Row + 1
    33.    
    34. Do Until rst.EOF
    35.    
    36.         fg.Rows = rst.RecordCount + 1
    37.         fg.TextMatrix(Row, 0) = rst!Model
    38.  
    39.         DisplaySQL = "SELECT * FROM DataGrid"
    40.         Set rst = oSQL.Execute(DisplaySQL)
    41.        
    42.         If fg.TextMatrix(Row, 0) <> oRs1!ModelDefined Then  'Izit i compare like this make it compare only with the Model in the first row in PackQty_View instead of comparing with the second row?
    43.                 Qty = 0
    44.             Else
    45.                 fg.TextMatrix(Row, 1) = oRs1!Qty
    46.                 Qty = oRs1!Qty
    47.                 oRs1.MoveNext
    48.             End If
    49.  
    50.         TargetQty = rst!TargetQty
    51.         Exceed = rst!Exceed
    52.         sTotal = Qty + Exceed
    53.    
    54.         fg.TextMatrix(Row, 1) = sTotal
    55.  
    56.         If fg.TextMatrix(Row, 1) < TargetQty Then
    57.          fg.Row = Row
    58.          fg.Col = 1
    59.          fg.CellBackColor = vbRed
    60.  
    61.         ElseIf fg.TextMatrix(Row, 1) >= TargetQty Then
    62.          fg.Row = Row
    63.          fg.Col = 1
    64.          fg.CellBackColor = vbGreen
    65.         End If
    66.  
    67.         Row = Row + 1
    68.         rst.MoveNext
    69.  
    70.  
    71.     Loop
    72.  
    73. Exit Sub
    74.  
    75. End Sub
    Human Knowledge Belongs To The World

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Statement Count Total Quantity

    Why use multiple SQL statements at all?

    Code:
    insert into PackQty
       Select * from Model_Defined_View
       where trans_date >= (SELECT StartDate FROM DateTime)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    Yah,i've put this statement at the beginning of the statement.However,m still facing some problems for the count quantity in the statement which make the color(green,red) turns differently because of the wrong count
    Human Knowledge Belongs To The World

  10. #10

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    anyone can help?Thanks
    Human Knowledge Belongs To The World

  11. #11

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    How to correct this part?It only keep on comparing with the first ModelDefined.So,when the second ModelDefined is actually run the Qty= oRs1!Qty part,but it goes to Qty = 0.Please help.

    If fg.TextMatrix(Row, 0) <> oRs1!ModelDefined Then
    Qty = 0
    Else
    Qty = oRs1!Qty
    End If
    Human Knowledge Belongs To The World

  12. #12
    Addicted Member
    Join Date
    Aug 2004
    Posts
    176

    Re: SQL Statement Count Total Quantity

    You are doing a
    VB Code:
    1. Do Until rst.EOF
    Then inside this you are again defining
    VB Code:
    1. DisplaySQL = "SELECT * FROM DataGrid"
    2.         Set rst = oSQL.Execute(DisplaySQL)
    This would reset the recordset. The .Movenext would no more be effective. Don't set it again.

  13. #13

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    How about this?

    VB Code:
    1. Private Sub DisplayGrid ()
    2. i = 0
    3. fg.Clear
    4.     sSQL = "SELECT * FROM PackQty_View "
    5.     Set oRs1 = oShip.Execute(sSQL)
    6.     Qty = oRs1!Qty
    7.     ModelDefined = oRs1!ModelDefined
    8.  
    9.    DisplaySQL = "SELECT Model,TargetQty,Exceed FROM DataGrid"
    10.    Set rst = oSQL.Execute(DisplaySQL)
    11.  
    12.     fg.ColWidth(0) = 2900
    13.     fg.ColWidth(1) = 1600
    14.    
    15.     fg.ColAlignment(0) = flexAlignLeftCenter
    16.     fg.ColAlignment(1) = flexAlignLeftCenter
    17.  
    18.     Row = 0
    19.    
    20.     fg.TextMatrix(Row, 0) = "Model"
    21.     fg.TextMatrix(Row, 1) = "Total Quantity"
    22.    
    23.     Row = Row + 1
    24.    
    25. Do Until rst.EOF
    26.  
    27.     fg.Rows = rst.RecordCount + 1
    28.     fg.TextMatrix(Row, 0) = rst!Model
    29.  
    30.  
    31.     If fg.TextMatrix(Row, 0) <> oRs1!ModelDefined Then
    32.                 Qty = 0
    33.             Else
    34.                 Qty = oRs1!Qty
    35.             End If
    36.            
    37.  
    38.         TargetQty = rst!TargetQty
    39.         Exceed = rst!Exceed
    40.         sTotal = Qty + Exceed
    41.  
    42.         fg.TextMatrix(Row, 1) = sTotal
    43.  
    44.         If fg.TextMatrix(Row, 1) < TargetQty Then
    45.          fg.Row = Row
    46.          fg.Col = 1
    47.          fg.CellBackColor = vbRed
    48.  
    49.         ElseIf fg.TextMatrix(Row, 1) >= TargetQty Then
    50.          fg.Row = Row
    51.          fg.Col = 1
    52.          fg.CellBackColor = vbGreen
    53.         End If
    54.  
    55.         Row = Row + 1
    56.         rst.MoveNext
    57.    
    58. Loop
    59. Exit Sub
    60. End Sub
    Human Knowledge Belongs To The World

  14. #14

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    May I know where is my mistakes?
    Human Knowledge Belongs To The World

  15. #15

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    Do Until rst.EOF

    fg.Rows = rst.RecordCount + 1
    fg.TextMatrix(Row, 0) = rst!Model
    fg.TextMatrix(Row, 1) = rst!TargetQty
    ' Row = Row + 1

    If fg.TextMatrix(Row, 0) <> ModelDefined Then 'How to write the coding to compare with the two row of ModelDefined instead of comparing only with the first row of ModelDefined here? Qty = 0
    Else
    Qty = oRs1!Qty
    End If

    TargetQty = rst!TargetQty
    OnHand = rst!OnHand
    sTotal = Qty + OnHand

    fg.TextMatrix(Row, 2) = sTotal

    If fg.TextMatrix(Row, 2) < TargetQty Then
    fg.Row = Row
    fg.Col = 2
    fg.CellBackColor = vbRed

    ElseIf fg.TextMatrix(Row, 2) >= TargetQty Then
    fg.Row = Row
    fg.Col = 2
    fg.CellBackColor = vbGreen
    End If

    Row = Row + 1
    rst.MoveNext

    Loop
    Human Knowledge Belongs To The World

  16. #16

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Exclamation Re: SQL Statement Count Total Quantity

    Sorrry for asking again and again here,but it is urgent!
    Human Knowledge Belongs To The World

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: SQL Statement Count Total Quantity

    You want to compare to the first two rows of the flexgrid? You cannot compare to a record after you move to the next record.

    VB Code:
    1. if  fg.TextMatrix(Row, 0) <> fg.TextMatrix(1, 0) and   fg.TextMatrix(Row, 0) <> fg.TextMatrix(2, 0) then

    if that's where they are, and you want to add to the quantity if it doesn't match either.

    If you want something else, be specific about how it should work.

  18. #18

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    No,I mean I get my ModelDefined,TotalQty from the PackQty_View table in database.Then fg.TextMatrix(Row, 0) = Model which means this Model will compare with PackQty_View table to see if this Model exist in that or not,if not ,then set the Qty Packout is 0.

    ...
    If fg.TextMatrix(Row, 0) <> ModelDefined Then
    Qty = 0
    Else
    Qty = oRs1!Qty
    End If

    TargetQty = rst!TargetQty
    OnHand = rst!OnHand
    sTotal = Qty + OnHand

    fg.TextMatrix(Row, 2) = sTotal

    If fg.TextMatrix(Row, 2) < TargetQty Then
    fg.Row = Row
    fg.Col = 2
    fg.CellBackColor = vbRed

    ElseIf fg.TextMatrix(Row, 2) >= TargetQty Then
    fg.Row = Row
    fg.Col = 2
    fg.CellBackColor = vbGreen
    End If
    Human Knowledge Belongs To The World

  19. #19
    Addicted Member
    Join Date
    Aug 2004
    Posts
    176

    Re: SQL Statement Count Total Quantity

    You must exit the loop once a match is found
    VB Code:
    1. Do Until rst.EOF
    2. If fg.TextMatrix(Row, 0) <> ModelDefined Then
    3. Qty = 0
    4. Else
    5. Qty = oRs1!Qty
    6. Exit Do
    7. End If
    8. Row = Row + 1
    9. rst.MoveNext
    10.  
    11. Loop

  20. #20

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    It works when i tried the following code,but the problems i faced now is it can't turn to green although the quantity is more than the TargetQty.
    For example:- sTotal(20000) > TargetQty(20),but it still run the first line and get red instead of green.


    ....
    Do Until rst.EOF
    sSQL = "Select * from PackQty_View where ModelDefined='" & rst!Model & "'"
    Set oRs1 = oShip.Execute(sSQL)

    If oRs1.RecordCount <> 0 Then
    sTotal = oRs1!Qty + rst!OnHand
    Else
    sTotal = rst!OnHand
    End If

    oRs1.Close


    fg.Rows = rst.RecordCount + 1
    fg.TextMatrix(Row, 0) = rst!Model 'Run Model column
    fg.TextMatrix(Row, 1) = rst!TargetQty 'Run Target Quantity
    fg.TextMatrix(Row, 2) = sTotal 'Run Total Quantity


    If fg.TextMatrix(Row, 2) < rst!TargetQty Then 'fg.TextMatrix(Row, 2) TargetQty fg.TextMatrix(Row, 1)
    fg.Row = Row
    fg.Col = 2
    fg.CellBackColor = vbRed

    ElseIf fg.TextMatrix(Row, 2) >= rst!TargetQty Then
    fg.Row = Row
    fg.Col = 2
    fg.CellBackColor = vbGreen
    End If

    Row = Row + 1
    rst.MoveNext

    Loop
    Human Knowledge Belongs To The World

  21. #21

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    Izit because i declare it as

    Dim sTotal As Integer?So,it can't take it a large quantity?So,what should I declare it as?Pls help.Thanks
    Human Knowledge Belongs To The World

  22. #22
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: SQL Statement Count Total Quantity

    integers are limited to 32768.

    Long is the biggest, but you could also use currency, which holds larger numbers
    I didn't realize the numbers were that big.

  23. #23

    Thread Starter
    Lively Member carlovfrimily's Avatar
    Join Date
    Jan 2005
    Location
    Malaysia
    Posts
    123

    Re: SQL Statement Count Total Quantity

    Oh,thanks lots lots to all of you.My program could finally work out.Juz that i need to make my flexgrid more attractive now.
    Human Knowledge Belongs To The World

Posting Permissions

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



Click Here to Expand Forum to Full Width