Difficult Query

  • Hi All,

    Just wondering if this were possible I have a table which contains Information on Electrical Components and when it was assembled similar to the following.

    ComponentCode, ComponentType, AssembledDate etc

    First of all I needed to find the number of components assembled within a particular time frame so I

    used a Select ComponenType, Count(ComponentCode) From Comp Where Assembeld date between XXX and XXX.

    This seems to work fine however now I am supposed to try and cross correlate with a table that has information on shiftTeams which has a structre like

    Date, DayShiftTeam, NightShiftTeam

    So for Each Record above there will be a single date with a dayshift team (which work fom 8 to 5) and a NightShift (who work 5 to 2)

    Is there any way with a query to find out how many components each team made in a prticular shift, assembled over a given time period.

    Results like

    Team Shift Component (Count of Components)

    A D CompA 20

    A N CompA 23

    B D CompC

  • You havent shown how these columns are related. If these tables have no relation then effectivly you arent storing which team assembled which component. Are there more columns or other tables which link these tables. i.e a componentAssembled table

    www.sql-library.com[/url]

  • That should do it the query you need is the last query of the batch... all the rest is setup tables and feed sample data :

    USE SSC

    GO

    IF EXISTS (SELECT FROM dbo.SysObjects WHERE Name 'Shifts' AND XType 'U' AND USER_NAME(uid'dbo')

           DROP TABLE dbo.Shifts

    GO

    CREATE TABLE dbo.Shifts 

    (

             StartShiftDate  SMALLDATETIME NOT NULL

           , EndShiftDate           SMALLDATETIME NOT NULL

           , ShiftType              CHAR(1) NOT NULL 

                   CONSTRAINT CK_Shifts_ShiftType CHECK ([ShiftType] IN ('D''N'))

           , CONSTRAINT PK_Shifts PRIMARY KEY CLUSTERED

           (

                     StartShiftDate

                   EndShiftDate

           ON [PRIMARY]

    )

    GO

    --Populate the table

    DECLARE @StartDate AS SMALLDATETIME

    DECLARE @EndDate AS SMALLDATETIME

    --set the startdate to whenever you started recording this data, or whenever you need to start reporting from.

    --I'd go for the first one if at all possible

    SET @StartDate '1995/01/01 08:00:00'

    SET @EndDate '1995/01/01 17:00:00'

    --populate a working table

    IF OBJECT_ID('Tempdb..#Work') > 0

           DROP TABLE #Work

    SELECT 

                   TOP 16437 --45 years worth of days

                   IDENTITY(INT01AS 

    INTO           #Work

    FROM           master.dbo.SysColumns C1

                   CROSS JOIN master.dbo.SysColumns C2

    INSERT INTO dbo.Shifts (StartShiftDateEndShiftDateShiftType)

    SELECT                 

                     DATEADD(DDw.ndtShifts.StartShiftDate)         AS StartShiftDate

                   DATEADD(DDw.ndtShifts.EndShiftDate)  AS EndShiftDate

                   dtShifts.ShiftType

    FROM           

                   (

                   SELECT @StartDate AS StartShiftDate@EndDate AS EndShiftDate'D' AS ShiftType

                   UNION ALL

                   SELECT @EndDate AS StartShiftDateDATEADD(HH9@EndDateAS EndShiftDate'N' AS ShiftType

                   dtShifts

                   CROSS JOIN #Work w

    --cleanup

    IF OBJECT_ID('Tempdb..#Work') > 0

           DROP TABLE #Work

    --You run the previous setup code only once.  Then this is how you use it : 

    IF OBJECT_ID('Tempdb..#Demo') > 0

           DROP TABLE #Demo

    CREATE TABLE #Demo 

    (

             Team VARCHAR(10) NOT NULL

           , ComponentCode VARCHAR(10) NOT NULL

           , AssemblyDate DATETIME NOT NULL

    )

    GO

    CREATE CLUSTERED INDEX IX_Demo ON #Demo (AssemblyDate)

    GO

    INSERT INTO #Demo (TeamComponentCodeAssemblyDate)

    SELECT 'A''Comp1''2007/02/02 08:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 09:00:00'

    UNION ALL

    SELECT 'B''Comp1''2007/02/02 10:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 11:00:00'

    UNION ALL

    SELECT 'B''Comp1''2007/02/02 12:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 13:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 14:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 15:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 16:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 16:00:00'

    UNION ALL

    SELECT 'B''Comp1''2007/02/02 17:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 18:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 19:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 20:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 21:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 22:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 23:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/02 00:00:00'

    UNION ALL

    SELECT 'A''Comp1''2007/02/03 01:59:00'

    UNION ALL

    SELECT 'A''Comp22''2007/02/02 15:00:00'

    UNION ALL

    SELECT 'A''Comp22''2007/02/02 16:00:00'

    UNION ALL

    SELECT 'A''Comp22''2007/02/02 16:00:00'

    UNION ALL

    SELECT 'B''Comp22''2007/02/02 17:00:00'

    UNION ALL

    SELECT 'A''Comp22''2007/02/02 18:00:00'

    UNION ALL

    SELECT 'A''Comp22''2007/02/02 19:00:00'

    UNION ALL

    SELECT 'A''Comp22''2007/02/02 20:00:00'

    UNION ALL

    SELECT 'A''Comp22''2007/02/02 21:00:00'

    INSERT INTO #Demo (TeamComponentCodeAssemblyDate)

    SELECT TOP 75 PERCENT TeamComponentCodeDATEADD(D1AssemblyDateFROM #Demo ORDER BY NEWID()

    INSERT INTO #Demo (TeamComponentCodeAssemblyDate)

    SELECT TOP 75 PERCENT TeamComponentCodeDATEADD(D2AssemblyDateFROM #Demo ORDER BY NEWID()

    INSERT INTO #Demo (TeamComponentCodeAssemblyDate)

    SELECT TOP 75 PERCENT TeamComponentCodeDATEADD(D3AssemblyDateFROM #Demo ORDER BY NEWID()

    INSERT INTO #Demo (TeamComponentCodeAssemblyDate)

    SELECT TOP 75 PERCENT TeamComponentCodeDATEADD(D4AssemblyDateFROM #Demo ORDER BY NEWID()

    INSERT INTO #Demo (TeamComponentCodeAssemblyDate)

    SELECT TOP 75 PERCENT TeamComponentCodeDATEADD(D5AssemblyDateFROM #Demo ORDER BY NEWID()

    INSERT INTO #Demo (TeamComponentCodeAssemblyDate)

    SELECT TOP 75 PERCENT TeamComponentCodeDATEADD(D6AssemblyDateFROM #Demo ORDER BY NEWID()

    INSERT INTO #Demo (TeamComponentCodeAssemblyDate)

    SELECT TOP 75 PERCENT TeamComponentCodeDATEADD(D7AssemblyDateFROM #Demo ORDER BY NEWID()

    SELECT 

                     D.Team

                   D.ComponentCode

                   S.ShiftType

                   COUNT(*) AS Total

    FROM           #Demo D

                   INNER JOIN dbo.Shifts S

                           ON D.AssemblyDate >= S.StartShiftDate

                           AND D.AssemblyDate EndShiftDate

    GROUP BY                        

                     D.Team

                   D.ComponentCode

                   S.ShiftType

    ORDER BY        

                     D.ComponentCode

                   S.ShiftType

                   D.Team

    IF OBJECT_ID('Tempdb..#Demo') > 0

           DROP TABLE #Demo

  • Thanks Guys muchly apreciated

     

    The Tables that I have are as such

     

    Shift Roster Table

                    Day Shift   Day Shift   Night Shift     Night Shift

    Date            Team        Man Hrs     Team            Man Hrs

    26/01/06 A     24  C         36 

    27/01/06 A     36  C         36 

    28/01/06 B     36  C         36 

    29/01/06 B     36  C         36 

    30/01/06 B     24  A         48 

     

    Component Table

                    Assemplby   Component   Assembled

    Job             code  

    JB1234          00091234    ComponentA  10/01/2007 04:24

    JB1234          00091235    ComponentA  10/01/2007 16:37

    JB1235          00091236    ComponentA  10/01/2007 16:24

    JB1235          00091237    ComponentB  10/01/2007 18:24

    JB1235          00091238    ComponentC  10/01/2007 16:24

    Which dont link really at all (the component table was an existing table that cannot be modified) the roster table is a new table. The request came to correlate given the assembled date try to determine the Team and number of components they put together knowing.

  • Sorry forgot to mentio the desired output would be as such

     

    Sorry Forgot to Mention the output should look as such

    TeamShiftComponent#Assembled
    ANComponentA34
    ANComponentB13
    ANComponentC23
    ANTIO212
    ADXRF34
    ADXRFPRP13
    ADAAS123
    ADTIO212
    BNXRF34
    BNXRFPRP13
    BNAAS123
    BNTIO212
    BDXRF34
    BDXRFPRP13
    BDAAS123
    BDTIO212
    CNXRF34
    CNXRFPRP13
    CNAAS123
    CNTIO212
  • So try adapting my last query to accomodate your actual tables.  Tell us if you need further help after you tried that.

  • Cheers Ninja's RGR'us did that after I posted this

    Works

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply