Results 1 to 5 of 5

Thread: [RESOLVED] help with mssql query to return top five selling products by date

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Resolved [RESOLVED] help with mssql query to return top five selling products by date

    so far i've managed the code to load the results to gridview. Can anyone help me by grouping the products by productname and qty sold for each product. my goal is to see top 100 products and the qty's sold within 2 dates. here is my current code:

    Code:
    Try
                Cursor = Cursors.WaitCursor
                Timer1.Enabled = True
                con = New SqlConnection(cs)
                con.Open()
                cmd = New SqlCommand("Select RTRIM(InvoiceNo),InvoiceDate,RTRIM(ProductName),RTRIM(Invoice_Product.Barcode),Invoice_Product.Qty,Invoice_Product.SalesRate,Invoice_Product.PurchaseRate, Invoice_Product.DiscountPer, Invoice_Product.Discount, Invoice_Product.VATPer, Invoice_Product.VAT, Invoice_Product.TotalAmount,Invoice_Product.Margin,Temp_stock_Company.Qty from InvoiceInfo,Invoice_Product,Product,Temp_Stock_Company where InvoiceInfo.Inv_ID=Invoice_product.InvoiceID and product.PID=Invoice_Product.ProductID and Product.PID=temp_Stock_Company.ProductID and InvoiceDate >=@d1 and InvoiceDate < @d2 Order by InvoiceDate", con)
                cmd.Parameters.Add("@d1", SqlDbType.DateTime, 30, "Date").Value = dtpDateFrom.Value.Date
                cmd.Parameters.Add("@d2", SqlDbType.DateTime, 30, "Date").Value = dtpDateTo.Value.Date.AddDays(1)
                cmd.CommandTimeout = 0
                rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                dgw.Rows.Clear()
                While (rdr.Read() = True)
                    dgw.Rows.Add(rdr(0), rdr(1), rdr(2), rdr(3), rdr(4), rdr(5), rdr(6), rdr(7), rdr(8), rdr(9), rdr(10), rdr(11), rdr(12), rdr(13))
                End While
                con.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: help with mssql query to return top five selling products by date

    Moved.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] help with mssql query to return top five selling products by date

    If the issue has been resolved then it would be nice for you to provide your solution, which may help others with similar issues. It also means that we can provide improvements if your solution is suboptimal.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2021
    Posts
    14

    Re: [RESOLVED] help with mssql query to return top five selling products by date

    no the issue is still not resolved. its loading the gridview but with each not merging the rows. example receipt 1: sold bmx bike for $99, receipt 2: sold bmx helmet for $40, receipt 3: sold bmx bike for $99 etc.. in gridview its showing every receipt as a row.. instead of **row-1: 2 bmx bikes for $198, **row-2: 1 bmx helmet $40

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: [RESOLVED] help with mssql query to return top five selling products by date

    Your Problem is that you're not aggregating the fields and you're using unique-per-row fields in your Output (e.g. InvoiceNo)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Tags for this Thread

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