Results 1 to 26 of 26

Thread: [RESOLVED] [Help] SQL for Datareport Complex Query

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Resolved [RESOLVED] [Help] SQL for Datareport Complex Query

    Hi to all.

    I need some help with my project.
    I have a database that contain 4 tables: Items, Purchased, Sold, Stocks
    I want to make a recordset from these database.
    The Items database contains Item Information: BarCode, Name, Size, Price, Category
    The Purchased database records Purchase ordered Items: BarCode, Units, Date
    The Sold database records Sold out Items: Barcode, Units, Date
    The Stocks database records available Items in stock: Barcode, Units, Date

    I wanted to query the Database to get a result like this:
    Code:
    Items.Barcode - Items.Name - Items.Size - Sold.Units
    But Sold database contains records with the same Barcode but different Date and Units. So I am looking for a way to automatically add Unit from records with same Barcode in a span of days.
    I will need the result to display in a datareport.
    I also want it in order by category.

    I hope someone can help.

  2. #2
    Addicted Member
    Join Date
    Apr 2007
    Posts
    197

    Re: [Help] SQL for Datareport Complex Query

    Quote Originally Posted by ayankent View Post
    Hi to all.

    I need some help with my project.
    I have a database that contain 4 tables: Items, Purchased, Sold, Stocks
    I want to make a recordset from these database.
    The Items database contains Item Information: BarCode, Name, Size, Price, Category
    The Purchased database records Purchase ordered Items: BarCode, Units, Date
    The Sold database records Sold out Items: Barcode, Units, Date
    The Stocks database records available Items in stock: Barcode, Units, Date

    I wanted to query the Database to get a result like this:
    Code:
    Items.Barcode - Items.Name - Items.Size - Sold.Units
    But Sold database contains records with the same Barcode but different Date and Units. So I am looking for a way to automatically add Unit from records with same Barcode in a span of days.
    I will need the result to display in a datareport.
    I also want it in order by category.

    I hope someone can help.
    don't understand your problem - the bit in bold - what actually do you mean?

    G

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    What is the database it makes a difference in what kind of SQL you will run. It sounds like you are mixing terms here also is Stocks a table or a database?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    oh... sorry.. its all a table in a single database...

    But Sold database contains records with the same Barcode but different Date and Units. So I am looking for a way to automatically add Unit from records with same Barcode in a span of days.
    Sold table cantains records with same Barcode but in different date and units.
    I am looking for a query that will automatically add the Units with same Barcode in a specified span of date...

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [Help] SQL for Datareport Complex 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)

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    sorry sir... and thanks...

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    Once again what is the backend datadase? MS Access? SQL Server? Oracle? MySQL?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    MS Access...

    I am using DataEnvironment to populate the Datareport (VB6)

    Example on the table Sold there are these data:
    Code:
    8851717200014 - 5 - 10/14/2011
    8851717200015 - 5 - 10/14/2011
    8851717200018 - 5 - 10/14/2011
    8851717200014 - 3 - 10/15/2011
    8851717200015 - 4 - 10/15/2011
    8851717200018 - 3 - 10/15/2011
    8851717200014 - 2 - 10/16/2011
    the Items table contains some info about the 3 products.
    Code:
         Barcode      -      Item Name      -  Size - Price  - Category
    8851717200014 - Strawberry Yogurt - 90ml - 18.00 - Beverages
    8851717200015 - Chocolate Waffer  - 1.5g - 5.00- Biscuits
    8851717200018 - Supreme Seafoods - 65g - 23.00 - Noodles
    I wanted my query to provide this kind of result when I query the table of Sold along with Items base on date Sold.Date from 10/14/2011 to 10/16/2011:
    Code:
         Barcode      -      Item Name      -  Size - Units  
    8851717200014 - Strawberry Yogurt - 90ml - 10
    8851717200015 - Chocolate Waffer  - 1.5g - 9
    8851717200018 - Supreme Seafoods - 65g - 8

  9. #9
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    Try something like this:
    Code:
    Select 
    	Items.BarCode ,
    	Items.Name ,
    	Items.Size
    	SUM(Sold.Units)
    From 
    	Items
    INNER JOIN Sold
        ON Items.BarCode = Sold.BarCode
    Where Sold.[Date] BETWEEN "Date1# AND #Date2#
    (This is a bad field name (DATE) to use by the way as it is a Reserved word)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    Quote Originally Posted by GaryMazzone View Post
    Try something like this:
    Code:
    Select 
    	Items.BarCode ,
    	Items.Name ,
    	Items.Size
    	SUM(Sold.Units)
    From 
    	Items
    INNER JOIN Sold
        ON Items.BarCode = Sold.BarCode
    Where Sold.[Date] BETWEEN "Date1# AND #Date2#
    (This is a bad field name (DATE) to use by the way as it is a Reserved word)
    thanks... i will try it right away... and thanks for the advise about Date being a reserved word... I totally forgot about it... I will surely change it right aways...

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    I tried your code but it wont show any result on my DataEnvironment and thus, wont populate the Datareport... this is the actual code I used...

    Code:
    select Items.ICode, Items.IName, Items.USize, sum(Sold.Units) from Items inner join Sold on Items.ICode = Sold.ICode
    I tried this query on Visual Data Manager but all I get is errors...

  12. #12
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    This was written directly in to the webpage. I do not have your DB so can not test on it. It is just SQL what are the errors?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    in dataenvironment... it doest show an error...

  14. #14
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    Run it in Access then an see what it tells you..... I can't guess as to what is wrong as I can't see your computer screen from here.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  15. #15
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    OK Try like this (I missed the Group By clause)
    Code:
    Select 
    	Items.BarCode ,
    	Items.Name ,
    	Items.Size ,
    	SUM(Sold.Units)
    From 
    	Items
    INNER JOIN Sold
        ON Items.BarCode = Sold.BarCode
    Where Sold.[Date] BETWEEN #Date1# AND #Date2#
    GROUP BY Items.barcode , items.NAME  , items.SIZE
    Leave out the Where clause if you don't care about the date range
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    Quote Originally Posted by GaryMazzone View Post
    OK Try like this (I missed the Group By clause)
    Code:
    Select 
    	Items.BarCode ,
    	Items.Name ,
    	Items.Size ,
    	SUM(Sold.Units)
    From 
    	Items
    INNER JOIN Sold
        ON Items.BarCode = Sold.BarCode
    Where Sold.[Date] BETWEEN #Date1# AND #Date2#
    GROUP BY Items.barcode , items.NAME  , items.SIZE
    Leave out the Where clause if you don't care about the date range
    Thanks... It worked...

  17. #17
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    HP-- If this solves the problem please use the Thread Tools to mark this as Resolved.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  18. #18

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    Another questions...

    1. How can I make it that DataEnvironment take date value from a textbox and append it in a query dynamically at runtime?

    2. How can I make my datareport dynamically? My "Items" table contain a "Category" column, therefor I made 4 DataEnvironment Command for each category that I have which are "Biscuit", "Beverage", "Noodle" and "New Product".

  19. #19
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    Don't know for either of these questions. I code everything by hand and don't use the DataEnviorment.

    But if you can pass a completed SQL statement to that then I would code the SQL in the code and pass to the DataEnvirronment as shown in my example and also adding a second where condition to include the Category.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  20. #20

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    I also wanted to code all by hand but I do not know how... This is my first time using DataReport... Could you please help me with this? I am not also familiar with ADODC... Only done code in DAO since I started programming...

    This is what I Want my report to show:

    Barcode Driven Inventory System

    Barcode Beverages Size Units
    8851717200014 DM Blueberry Yogurt 90mlx48 98
    8851717200015 DM Strawberry Yogurt 90mlx48 98
    8851717200016 DM Mix Fruit Yogurt 90mlx48 98
    8851717200017 DM Blueberry Yogurt 180mlx48 98
    8851717200018 DM Strawberry Yogurt 180mlx48 98
    8851717200019 DM Mix Fruit Yogurt 180mlx48 98
    Barcode Biscuit Size Units
    8851717200020 Yummy Wafer 20x20x1.5g 98
    8851717200021 Chocolate Wafer 20x20x1.5g 98
    8851717200022 Vanilla Wafer 20x20x1.5g 98
    8851717200023 Yummy Wafer 20x20x3.5g 98
    8851717200024 Chocolate Wafer 20x20x3.5g 98
    8851717200025 Vanilla Wafer 20x20x3.5g 98


    and so on...

  21. #21
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    As I said I do not use ADODC or data reports.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  22. #22

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    how do you do it? using DAO?

  23. #23
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    I use ADODB. Coded directly
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  24. #24

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    can you help me with it... I am not familiar with ADODB...

  25. #25
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [Help] SQL for Datareport Complex Query

    There are example in the FAQ section of the database area
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  26. #26

    Thread Starter
    Member
    Join Date
    Oct 2009
    Posts
    49

    Re: [Help] SQL for Datareport Complex Query

    I solved my problem on this... thanks to you GaryMazzone....

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