Unix Technical Forum

Sub-Query / Cross Join / or something else?

This is a discussion on Sub-Query / Cross Join / or something else? within the SQL Server forums, part of the Microsoft SQL Server category; --> I have the following 2 tables: (BATCHES) BatchID [int] KEY ID [int] OrderID [int] Action1DateTime [datetime] Action2DateTime [datetime] Action3DateTime ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:43 PM
Henrik Juul
 
Posts: n/a
Default 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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:43 PM
Ed Murphy
 
Posts: n/a
Default Re: Sub-Query / Cross Join / or something else?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:44 PM
Henrik Juul
 
Posts: n/a
Default Re: Sub-Query / Cross Join / or something else?

Thanx very much Ed.

I do believe you really fixed my problem here.

Regards
Henrik


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 02:45 PM
--CELKO--
 
Posts: n/a
Default Re: Sub-Query / Cross Join / or something else?

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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:11 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com