473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL Query

77
I have 3 tables in Access namely;

Recd_Items
Item_code - FK
Item_Recd - Numeric

Issue_Items
Item_Code - FK
Issue_Qty- Numeric


Item_Master
Item_Code - PK
Item_Desc - Text
Item_Rol - Numeric


My requirement is I need to write a SQL query which would display the following records for a particular Item_Code selected by the user. the user would select the itemcode and the records would be displayed in the subform.

Expand|Select|Wrap|Line Numbers
  1. Item_Desc    Sum of Stk recd     Sum of Stk Issued   StkInHand  Re-Order Level 
Thanking you in advance
Sajit
May 28 '08 #1
12 1343
beacon
579 512MB
What have you tried so far?
May 28 '08 #2
sajitk
77
What have you tried so far?
Hi beacon,

this is wat i have tried.

SELECT DISTINCT Item_Master.Item_Code, Item_Master.ROL, Sum(Recd_Stk.Recd_Stock) AS SumOfRecd_Stock, Sum(Issue_Mas.Issued_Qty) AS SumOfIssued_Qty
FROM Item_Master, Recd_Stk, Issue_Mas
WHERE Item_Master.Item_Code=[Recd_Stk].Item_Code And Item_Master.Item_Code=Issue_Mas.Item_Code
GROUP BY Item_Master.Item_Code, Item_Master.ROL;

but the result of this query is not showing correct.

Suppose I have the following data in the table

Item_Mas

Item_Code ROL
WS-002 10

Recd_Stock

Item_Code Recd_Stk
WS-002 5
WS-002 5

Issue_Stock

Item_Code Issue_Stk
WS-002 2

the result which i want is this:

Item_Code ROL Recd_Stk Issue_Stk
WS-002 10 10 2


But the result which my query retrieves is this

Item_Code ROL Recd_Stk Issue_Stk
WS-002 10 10 4


The value in the issue stock gets doubled. I know why this is getting doubled, but i am not able to write the SQL for this.

Sajit
May 29 '08 #3
sajitk
77
Hi beacon,

looking for an answer to my mail.

I am actually stuck on this. would be great if you could respond

sajit

Hi beacon,

this is wat i have tried.

SELECT DISTINCT Item_Master.Item_Code, Item_Master.ROL, Sum(Recd_Stk.Recd_Stock) AS SumOfRecd_Stock, Sum(Issue_Mas.Issued_Qty) AS SumOfIssued_Qty
FROM Item_Master, Recd_Stk, Issue_Mas
WHERE Item_Master.Item_Code=[Recd_Stk].Item_Code And Item_Master.Item_Code=Issue_Mas.Item_Code
GROUP BY Item_Master.Item_Code, Item_Master.ROL;

but the result of this query is not showing correct.

Suppose I have the following data in the table

Item_Mas

Item_Code ROL
WS-002 10

Recd_Stock

Item_Code Recd_Stk
WS-002 5
WS-002 5

Issue_Stock

Item_Code Issue_Stk
WS-002 2

the result which i want is this:

Item_Code ROL Recd_Stk Issue_Stk
WS-002 10 10 2


But the result which my query retrieves is this

Item_Code ROL Recd_Stk Issue_Stk
WS-002 10 10 4


The value in the issue stock gets doubled. I know why this is getting doubled, but i am not able to write the SQL for this.

Sajit
Jun 2 '08 #4
FishVal
2,653 Expert 2GB
Hello, sajitk.

I think this thread will be helpful for you.

Regards,
Fish
Jun 2 '08 #5
sajitk
77
Hi Fish,

That particular thread was helpful. But there is again a problem.If I execute an UNION query the results comes in the following format

Item Code Received Items
WS-002

Hello, sajitk.

I think this thread will be helpful for you.

Regards,
Fish
Jun 3 '08 #6
sajitk
77
Hi Fish,

That particular thread was helpful. But there is again a problem.If I execute an UNION query the results comes in the following format

Item Code Received Items
WS-002 20
WS-002 10

both the issue and received values comes under the same column. I need the result in the following format

Item Code Received Issued

WS-002 20 10


The query which i have written is given below:
SELECT Recd_Stk.Item_Code, NZ(Sum(Recd_Stk.Recd_Stock),0) AS [Received Quantity]
From Recd_Stk
where Recd_Stk.Item_Code = forms!Recd_Stk1.Item_Code.text
Group By Recd_Stk.Item_Code
UNION
Select Issue_Mas.Item_Code, NZ(Sum(Issue_Mas.Issued_Qty),0) As [Issued Quantity]
where Issue_Mas.Item_Code = forms!RecdStk1.Item_Code.text
Group By Issue_mas.Item_Code


The query basically takes an input from the user in the form Recd_Stk1

sajit

Hello, sajitk.

I think this thread will be helpful for you.

Regards,
Fish
Jun 3 '08 #7
FishVal
2,653 Expert 2GB
Hi, Sajit.

Try the following:
  • Join [Item_Master] and let us say [Issue_Items] with records grouping. Certainly it should be outer join.
  • Join thus obtained dataset with the rest table ([Recd_Items]) with records grouping.

Regards,
Fish
Jun 3 '08 #8
sajitk
77
Hi Fish,

Thanks for the prompt response.

Can u help me in writing the SQL. I am a bit confused.

Saji t

Hi, Sajit.

Try the following:
  • Join [Item_Master] and let us say [Issue_Items] with records grouping. Certainly it should be outer join.
  • Join thus obtained dataset with the rest table ([Recd_Items]) with records grouping.

Regards,
Fish
Jun 3 '08 #9
FishVal
2,653 Expert 2GB
Hi Fish,

Thanks for the prompt response.

Can u help me in writing the SQL. I am a bit confused.

Saji t
Ok, Sajit.

The problem of redundant records combinations happens because database engine groups records after performing all joins. It is evident that performing records grouping after each join in sequential queries or in subquery will be a red pill.

Regards,
Fish
Jun 3 '08 #10
sajitk
77
Dear FishVal,

I really need to appreciate your way of explaination....this is was just too good.
After reading wat u said, i got the real picture. I have written the SQL and its working fine.

SELECT T1.Item_Code AS [Item Code], NZ(SUM(T1.ITEM_RECD),0) AS [Stock Received], NZ(SUM(T2.ITEM_QTY),0) AS [Stock Issued]
FROM [SELECT ITEM_CODE,SUM(Recd_Stock) AS ITEM_RECD
FROM Recd_Stk GROUP BY ITEM_CODE]. AS T1, [SELECT ITEM_CODE,
SUM(Issued_QTY) AS ITEM_QTY
FROM
ISSUE_Mas
GROUP BY ITEM_CODE]. AS T2
WHERE T1.ITEM_CODE=T2.ITEM_CODE
GROUP BY T1.ITEM_CODE;

But life is not easy as of now. One problem solved and there comes another.

In the Receive_Inventory Form, I have a combo box with Vendor Name and a text box which shows the Vendor Code automatically from the Vendor Master table.

In the form I have a subform, where i will enter all the the receipt details of a particular item. This form will be viewed in Datasheetview. The form will have an Item description, Item code, Received_Qty, Date and Vendor_Code.

The problem is when there are many items received from a particular vendor. I need the Vendor code to be replicated for each row i enter. As of now the vendor code comes only for the first entry. In the second row, the vendor code doesnt comes. I need the vendor code there also.

rite now, i am using the following code in the click event of the combo box.

[Child82].Form![Vendor] = DLookup("Vendor_Code", "Vendor_Master", "Vendor_Name='" & Me.Vendor & "'")

How do i solve this problem. Please suggest.


Ok, Sajit.

The problem of redundant records combinations happens because database engine groups records after performing all joins. It is evident that performing records grouping after each join in sequential queries or in subquery will be a red pill.

Regards,
Fish
Jun 4 '08 #11
FishVal
2,653 Expert 2GB
Ok.

There are at least two ways to cure it:
  • Instead of setting Value property of [Vendor] control set DefaultValue property.
    Expand|Select|Wrap|Line Numbers
    1. [Child82].Form![Vendor].DefaultValue = DLookup("Vendor_Code", "Vendor_Master", "Vendor_Name='" & Me.Vendor & "'")
  • You may use conventianal master/child binding. For that you need to redesign your combobox.
    • RowSource = "Select Vendor_Code, Vendor_Name ......"
    • ColumnsCount = 2
    • BoundColumn = 1
    • ColumnWidths = "0;1" or any non-zero value instead of "1"
      Thus the combobox will display vendor names, but its Value property will return vendor codes

Regards,
Fish
Jun 4 '08 #12
sajitk
77
Hi Fish,

Sorry for the late reply.I was on travel

Thanks for the advise.

I have another problem now. In the Stock Receiving Form (Main form), I have the followng controls:

1) Vendor_Name - combo-box which shows the list of available vendors
2) A child form which is viewded as Datasheet
- Item_Desc - combo-box which shows the Item_Desc
- Item_Code - corresponding Item_code appears for the selected Item_Desc
- Stock_Recd - No of stock received
- Vendor_Code corresponsing vendor code for the vendor selected in 1.

A user can enter 'n' number of items received from the selected vendor.The data entered in this form is saved in Recd_Stk table.

3) This is where the problem is. I am asked to create a subform (this should also be viewed in Datasheet) which will show the current status of the particular item_code selected in 2. Each time a new item is selected in 2, the status of it should be shown in this subform.

I have the query, but i am not able to trap the item_code in 2.

SELECT T1.Item_Code AS [Item Code], NZ(SUM(T1.ITEM_RECD),0) AS [Stock Received], NZ(SUM(T2.ITEM_QTY),0) AS [Stock Issued]
FROM [SELECT ITEM_CODE,SUM(Recd_Stock) AS ITEM_RECD
FROM Recd_Stk GROUP BY ITEM_CODE]. AS T1, [SELECT ITEM_CODE,
SUM(Issued_QTY) AS ITEM_QTY
FROM
ISSUE_Mas
GROUP BY ITEM_CODE]. AS T2
WHERE T1.ITEM_CODE=T2.ITEM_CODE
GROUP BY T1.ITEM_CODE;


Hope I am able to make you understand.

each time I click on the sub-form icon and drag it in the form, the form opens are Childform and is unbound. the wizard for the sub-forms doent comes. Can you tell me why....????

Looking forward to hear from you

Sajit


Ok.

There are at least two ways to cure it:
  • Instead of setting Value property of [Vendor] control set DefaultValue property.
    Expand|Select|Wrap|Line Numbers
    1. [Child82].Form![Vendor].DefaultValue = DLookup("Vendor_Code", "Vendor_Master", "Vendor_Name='" & Me.Vendor & "'")
  • You may use conventianal master/child binding. For that you need to redesign your combobox.
    • RowSource = "Select Vendor_Code, Vendor_Name ......"
    • ColumnsCount = 2
    • BoundColumn = 1
    • ColumnWidths = "0;1" or any non-zero value instead of "1"
      Thus the combobox will display vendor names, but its Value property will return vendor codes

Regards,
Fish
Jun 10 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
14
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. ...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.