/ Forside / Teknologi / Udvikling / SQL / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
SQL
#NavnPoint
pmbruun 1704
niller 962
fehaar 730
Interkril.. 701
ellebye 510
pawel 510
rpje 405
pete 350
gibson 320
10  smorch 260
Sub-Query / Cross Join / eller noget helt ~
Fra : Henrik Juul


Dato : 21-03-07 10:08

Jeg har følgende 2 tabeller:

(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 til Batches.Action8DateTime kan have flere entries
hver dag.
Jeg behøver en query til at tælle alle Batches.Action1DateTime til alle
Batches.Action8DateTime for hver dag i en specifik periode.
Jeg skal også kunne sætte betingelser for Orders.OrderID og/eller
Orders.RecipeID.

Jeg skal bruge data til at tegne en graf for hver ActionXDateTime som en
funktion af dato.

På forhånd tak for hjælpen.

/Henrik



 
 
Martin (21-03-2007)
Kommentar
Fra : Martin


Dato : 21-03-07 13:38

Henrik Juul wrote:
> Jeg har følgende 2 tabeller:
>
> (BATCHES)
> BatchID [int] KEY
> ID [int]
> OrderID [int]
> Action1DateTime [datetime]
> Action2DateTime [datetime]
> Action3DateTime [datetime]
> Action4DateTime [datetime]
> Action5DateTime [datetime]
> Action6DateTime [datetime]
> Action7DateTime [datetime]
> Action8DateTime [datetime]


Ville det ikk være smart med endnu en tabel med kun action times, med
reference til BatchID :)

Henrik Juul (21-03-2007)
Kommentar
Fra : Henrik Juul


Dato : 21-03-07 13:56

"Martin" <news@natten-i.dk> wrote in message
news:460125c5$0$90270$14726298@news.sunsite.dk...
> Henrik Juul wrote:
>> Jeg har følgende 2 tabeller:
>>
>> (BATCHES)
>> BatchID [int] KEY
>> ID [int]
>> OrderID [int]
>> Action1DateTime [datetime]
>> Action2DateTime [datetime]
>> Action3DateTime [datetime]
>> Action4DateTime [datetime]
>> Action5DateTime [datetime]
>> Action6DateTime [datetime]
>> Action7DateTime [datetime]
>> Action8DateTime [datetime]
>
>
> Ville det ikk være smart med endnu en tabel med kun action times, med
> reference til BatchID :)

Måske, men det løser ikke mit problem.

/Henrik



Henrik Juul (23-03-2007)
Kommentar
Fra : Henrik Juul


Dato : 23-03-07 07:23


"Henrik Juul" <h1e2n3r4i5k6@juul-janning.dk (swap janning and juul)> wrote
in message news:4600f5f8$0$90272$14726298@news.sunsite.dk...
> Jeg har følgende 2 tabeller:
>
> (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 til Batches.Action8DateTime kan have flere entries
> hver dag.
> Jeg behøver en query til at tælle alle Batches.Action1DateTime til alle
> Batches.Action8DateTime for hver dag i en specifik periode.
> Jeg skal også kunne sætte betingelser for Orders.OrderID og/eller
> Orders.RecipeID.
>
> Jeg skal bruge data til at tegne en graf for hver ActionXDateTime som en
> funktion af dato.
>
Til interesserede.
Fra en udenlandsk gruppe fik jeg dette svar, som løste mit problem:

> 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



Søg
Reklame
Statistik
Spørgsmål : 177579
Tips : 31968
Nyheder : 719565
Indlæg : 6409073
Brugere : 218888

Månedens bedste
Årets bedste
Sidste års bedste