473,385 Members | 1,888 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,385 software developers and data experts.

Sub-Query / Cross Join / or something else?

I have the following 2 tables:

(BATCHES)
BatchID [int] KEY
ID [int]
OrderID [int]
Action1DateTime [datetime]
Action2DateTime [datetime]
Action3DateTime [datetime]
Action4DateTime [datetime]
Action5DateTime [datetime]
Action6DateTime [datetime]
Action7DateTime [datetime]
Action8DateTime [datetime]

(ORDERS)
OrderID [int] KEY
ProductionLineID [int]
RecipeID [int]
OrderAmount [int]

Batches.Action1DateTime to Batches.Action8DateTime can have several entries
each day.
I need a query to count all Batches.Action1DateTime to all
Batches.Action8DateTime for each day in a specified period.
I also need to specifically use where clauses for Orders.OrderID and/or
Orders.RecipeID.

I need the data to draw a graph for each ActionXDateTime as a function of
date.

Any help appreciated.

/Henrik
Mar 21 '07 #1
3 3537
Henrik Juul wrote:
I have the following 2 tables:

(BATCHES)
BatchID [int] KEY
ID [int]
The 'ID' column should be renamed to indicate what it's an ID for.
OrderID [int]
Action1DateTime [datetime]
Action2DateTime [datetime]
Action3DateTime [datetime]
Action4DateTime [datetime]
Action5DateTime [datetime]
Action6DateTime [datetime]
Action7DateTime [datetime]
Action8DateTime [datetime]
This is a classic case of bad design. Here's how to fix the design:

create view BatchesNormalized as
select BatchID, ID, OrderID,
Action1DateTime as ActionDateTime,
1 as ActionNumber -- if order is important
from Batches
where Action1DateTime is not null
union
select BatchID, ID, OrderID,
Action2DateTime as ActionDateTime,
2 as ActionNumber
from Batches
where Action2DateTime is not null
-- similar for 3 through 8

Ideally, you should fix the original table:

1) Create the view shown above
2) Copy its contents to a second table
3) Drop the view
4) Drop the Batches table and re-create it with the same columns
as the view
5) Copy the contents of the second table to the new Batches table

If you already have a lot of code referencing the non-normalized table:

1) Create the view
2) Change SELECTs one at a time to use the view
3) Create stored procedures that wrap around INSERT, UPDATE, and DELETE
4) Change INSERTs/UPDATEs/DELETEs one at a time to use the stored
procedures
5) Fix the table as described above, and at the same time, change the
stored procedure wrappers to use the fixed table

If you can't get rid of the non-normalized table (e.g. you're working
with a third-party software package), then at least create the view and
use it in your own stuff.
(ORDERS)
OrderID [int] KEY
ProductionLineID [int]
RecipeID [int]
OrderAmount [int]

Batches.Action1DateTime to Batches.Action8DateTime can have several entries
each day.
I need a query to count all Batches.Action1DateTime to all
Batches.Action8DateTime for each day in a specified period.
I also need to specifically use where clauses for Orders.OrderID and/or
Orders.RecipeID.
Once the data is normalized, it becomes simple:

select b.ActionDateTime, count(*)
from BatchesNormalized b
join Orders o on b.OrderID = o.OrderID
where b.OrderID = @OrderID and o.RecipeID = @RecipeID
group by b.ActionDateTime
Mar 21 '07 #2
Thanx very much Ed.

I do believe you really fixed my problem here.

Regards
Henrik
Mar 22 '07 #3
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

Next, get **any** book on RDBMS. Read the chapter about getting rid
of repeated groups in a table -- it is called First Normal Form (1NF)
and it is the foundation of RDBMS.

There is no such thng as a magical universal "id" -- it hs to be the
identifer of a particular kind of entity in your data model.
Mar 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Dewbert | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Hello, I have trouble passing a folder object (from a FileSystemObject) to a sub procedure. Consider the following code: ...
2
by: Gianluca_Venezia | last post by:
Scenary: a main form contains two sub-form unbounded. Sub-form "one" contains a list of products. Sub-form "two" contains the items of the selected product. Event: when, from sub-form "two",...
10
by: tmaster | last post by:
When I try to dynamically add a second sub menu item to this ContextMenu item, I get an error 'Specified argument was out of the range of valid values'. Private Sub mnuTopics_Show_Select(ByVal...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
16
by: Adam Honek | last post by:
This is probably the most silly question asked here but I just can't figure it out. In VB6 letting Sub Main() be the app's startup directory was real easy under project properties. In VB.Net...
1
by: dBNovice | last post by:
Please help! I have 3 forms: Task, Subtask, Elements. Elements is a subform of Subtask and Subtask is a subform of Task. I am able to navigate from Task to Subform to Element and from Element to...
7
by: ILCSP | last post by:
Hi, I'm using VB.Net (2003) and I have a question. Does anyone knows how to call a procedure using a variable? The variable will be equal to the name of the procedure. for example, if I have...
5
by: Sharon | last post by:
Hi all. To prevent access to a sub system internal types, is it necessary to create the sub system in a different project, and use the internal access level? Or is there another way that will...
6
by: Bob | last post by:
Hi, I found this code here below (about cartitems and shoppingcart) and I have two questions about sub New(). In the first class CartItem, there is two times sub New(): Public Sub New() End...
1
by: bemadragon | last post by:
Hello, I have been struggling with this for a while. I am working on a rather simple database for calculation purposes in Access 2003. I have a table called Order that has a primary key field...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.