Should I use a nested subquery?

  • SELECT DISTINCT RepairSheetH.RepairSheetNr, Vehicle.unitnr, Vehicle.groupid, Vehicle.brandId, Vehicle.modelId, Vehicle.version, Vehicle.Type_Fleet,

    RepairSheetH.close_date,

    CASE WHEN RepairSheetH.unitnr = Vehicle.unitnr THEN count(vehicle.modelId) ELSE 0 END as TotalModels

    FROM Vehicle INNER JOIN

    RepairSheetH ON Vehicle.unitnr = RepairSheetH.UnitNr INNER JOIN

    RepairSheetL ON RepairSheetH.RepairSheetNr = RepairSheetL.RepairSheetNr INNER JOIN

    CashFlow_Accounts ON RepairSheetH.Company = CashFlow_Accounts.Company AND RepairSheetL.cashflow = CashFlow_Accounts.CashFlow_AccID

    where RepairSheetL.cashflow='201708' and vehicle.Type_Fleet<>'FS'

    GROUP BY RepairSheetH.RepairSheetNr,CashFlow_Accounts.CashFlow_AccID,Vehicle.unitnr,Vehicle.groupid,Vehicle.brandId,Vehicle.modelId,Vehicle.version,Vehicle.Type_Fleet,RepairSheetH.Date,RepairSheetH.close_date,RepairSheetH.total_value,RepairSheetL.cashflow,RepairSheetH.UnitNr

    Ok So this query basically pulls back all the vehicles that have had a tyre fitted based on the "where RepairSheetL.cashflow='201708'" where '201708' is the code for tyres. Now I need to be able to pull back all the modelid in the fleet linked to the modelid of the Unitnr in question. Heres an example of a result row

    RepairSheetNrunitnrgroupidbrandIdmodelIdversion Type_Fleet Closed_date TotalModels

    500000381EDMNFIATGRANDE PUNTO 1.2 8V DYNAMIC 5DROF 2012-09-31 1

    I would need the column TotalModels to reflect the total number of that particular model of vehicle on the fleet at that time. When finished I am going to use this dataset in SSRS, the table that holds the data is Vehicle, does anybody have an example of how I could do this. I attempted a case statement to no avail, should I use a subquery?

  • Do you get different results with/without the DISTINCT?

    Here's a reformatted copy of the query. I've added table aliases to make it a little more readable and changed the order of the columns in the GROUP BY to see which ones are in the output list and which are not.

    SELECT DISTINCT

    h.RepairSheetNr,

    v.unitnr,

    v.groupid,

    v.brandId,

    v.modelId,

    v.version,

    v.Type_Fleet,

    h.close_date,

    -- you don't need this CASE because the condition already exists in a join: v.unitnr = h.UnitNr

    -- count(CASE WHEN h.unitnr = v.unitnr THEN v.modelId ELSE 0 END) AS TotalModels

    COUNT(v.modelId) AS TotalModels

    FROM Vehicle v

    INNER JOIN RepairSheetH h

    ON v.unitnr = h.UnitNr

    INNER JOIN RepairSheetL l

    ON h.RepairSheetNr = l.RepairSheetNr

    INNER JOIN CashFlow_Accounts cf

    ON h.Company = cf.Company

    AND l.cashflow = cf.CashFlow_AccID

    WHERE l.cashflow = '201708'

    AND v.Type_Fleet<>'FS'

    GROUP BY

    h.RepairSheetNr,

    v.unitnr,

    v.groupid,

    v.brandId,

    v.modelId,

    v.version,

    v.Type_Fleet,

    h.close_date,

    cf.CashFlow_AccID,

    h.Date,

    h.total_value,

    l.cashflow,

    h.UnitNr


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • Didn't work just pulled back the same result set, I need the query to be able to search the vehicle table for all the modelid on the fleet at that time linked to the particular Modelid of the row returned. So if the modelid of a row is GRANDE PUNTO 1.2, I need the TotalModels column to reflect the count of that particular model on the fleet at that time. Thanks for the effort

  • thomasrichardson2000 (10/11/2012)


    Didn't work just pulled back the same result set, I need the query to be able to search the vehicle table for all the modelid on the fleet at that time linked to the particular Modelid of the row returned. So if the modelid of a row is GRANDE PUNTO 1.2, I need the TotalModels column to reflect the count of that particular model on the fleet at that time. Thanks for the effort

    What didn't work?


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • RepairSheetNrunitnrgroupidbrandIdmodelIdversionType_Fleetclose_dateTotalModels

    500000384EDMNFIATGRANDE PUNTO 1.28V DYNAMIC 5DROF 2012-07-31 00:00:00.0001

    Sorry the query ran fine, it just returned the exact same result set as the query I posted.

    I need the total value to reflect the total number of that particular model on the fleet at present, for example in the case above i know there are 45 "GRANDE PUNTO 1.2 ", ineed the results to reflect this

  • thomasrichardson2000 (10/11/2012)


    RepairSheetNrunitnrgroupidbrandIdmodelIdversionType_Fleetclose_dateTotalModels

    500000384EDMNFIATGRANDE PUNTO 1.28V DYNAMIC 5DROF 2012-07-31 00:00:00.0001

    Sorry the query ran fine, it just returned the exact same result set as the query I posted.

    I need the total value to reflect the total number of that particular model on the fleet at present, for example in the case above i know there are 45 "GRANDE PUNTO 1.2 ", ineed the results to reflect this

    That was the intention, Thomas - I made the query more readable for others, that's all 🙂

    Are you only getting one row back with this query? If you are getting several, then it would be helpful if you could post a set of them embracing several models. Post in a readily-consumable format, like this:

    CREATE TABLE #Temp (RepairSheetNr ?, unitnr ?, groupid ?, brandId ?, modelId ?, version ?, Type_Fleet ?, close_date DATETIME, TotalModels INT)

    INSERT INTO #Temp

    SELECT 5000003, 84, 'EDMN', 'FIAT','GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF', '2012-07-31 00:00:00.000',1 UNION ALL

    Don't forget to include the datatypes in the CREATE TABLE - replace ? with the correct type.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • I am getting over 500 single results, heres some sample data to work with

    CREATE TABLE #Temp (RepairSheetNr int, unitnr int, groupid char(4), brandId varchar(40), modelId varchar(40), version varchar(40), Type_Fleet char(10), close_date DATETIME, TotalModels INT)

    INSERT INTO #Temp

    SELECT 5000003, 84, 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-07-31 00:00:00.000', 1

    UNION ALL

    select 5000016,671,'IDMD','TOYOTA','AVENSIS 2.0D-4D','TERRA DPF 4DR','OF','2012-05-04 00:00:00.000','1'

    UNION ALL

    select 5000024,472,'CDMD','FORD','FOCUS STYLE 1.6TDCI','STYLE 1.6 TDCI 109PS','OF', '2012-03-01 00:00:00.000','1'

    UNION ALL

    select 5000058,698,'IDMD','FORD','MONDEO 1.8 TDCI','ECONETIC 09 MY 4DR','OF','2012-05-09 00:00:00.000','1'

    UNION ALL

    select 5000078,430,'CDMD','OPEL','ASTRA 1.3CDTISC', '95PS 5DR','BB', '2012-03-06 00:00:00.000','1'

  • What do you see if you order the results by Vehicle.modelId? Do you get multiple rows per model? That's what I'm looking for.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • "What do you see if you order the results by Vehicle.modelId? Do you get multiple rows per model? That's what I'm looking for."

    Yes when I order them by Vehicle.modelId I get multiple rows per model, these are models that have had tyres changed etc, I need the total models column to give me the total amount of those models in the fleet, irrespective of anything

    RepairSheetNrunitnrgroupidbrandIdmodelIdversionType_Fleetclose_dateTotalModels

    500007651EDMNPEUGEOT207 S 1.48VOF 2012-03-06 00:00:00.00047

  • thomasrichardson2000 (10/11/2012)


    "What do you see if you order the results by Vehicle.modelId? Do you get multiple rows per model? That's what I'm looking for."

    Yes when I order them by Vehicle.modelId I get multiple rows per model, these are models that have had tyres changed etc, I need the total models column to give me the total amount of those models in the fleet, irrespective of anything

    RepairSheetNrunitnrgroupidbrandIdmodelIdversionType_Fleetclose_dateTotalModels

    500007651EDMNPEUGEOT207 S 1.48VOF 2012-03-06 00:00:00.00047

    Good. Pick a couple of models which only have about 5 or so rows, and post the data as I showed you earlier.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • CREATE TABLE #Temp (RepairSheetNr int, unitnr int, groupid char(4), brandId varchar(40), modelId varchar(40), version varchar(40), Type_Fleet char(10), close_date DATETIME, TotalModels INT)

    INSERT INTO #Temp

    SELECT '5000023', '83', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-04-08 00:00:00.000', 1

    UNION ALL

    select '5000094', '84', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-08-31 00:00:00.000', 1

    UNION ALL

    SELECT'5000085', '85', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-08-25 00:00:00.000', 1

    UNION ALL

    SELECT'5000056', '86', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-07-14 00:00:00.000', 1

    UNION ALL

    SELECT'5000077', '87', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-04-31 00:00:00.000', 1

  • thomasrichardson2000 (10/11/2012)


    CREATE TABLE #Temp (RepairSheetNr int, unitnr int, groupid char(4), brandId varchar(40), modelId varchar(40), version varchar(40), Type_Fleet char(10), close_date DATETIME, TotalModels INT)

    INSERT INTO #Temp

    SELECT '5000023', '83', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-04-08 00:00:00.000', 1

    UNION ALL

    select '5000094', '84', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-08-31 00:00:00.000', 1

    UNION ALL

    SELECT'5000085', '85', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-08-25 00:00:00.000', 1

    UNION ALL

    SELECT'5000056', '86', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-07-14 00:00:00.000', 1

    UNION ALL

    SELECT'5000077', '87', 'EDMN', 'FIAT', 'GRANDE PUNTO 1.2', '8V DYNAMIC 5DR', 'OF','2012-04-31 00:00:00.000', 1

    Thanks Thomas, that helped. Note that April only has 30 days.

    -- Using DISTINCT will probably screw up your counts

    -- If you get "dupes" in the output, deal with it properly

    SELECT --DISTINCT

    h.RepairSheetNr,

    v.unitnr,

    v.groupid,

    v.brandId,

    v.modelId,

    v.version,

    v.Type_Fleet,

    h.close_date,

    COUNT(*) OVER (PARTITION BY v.modelId, v.Type_Fleet) AS TotalModels

    FROM Vehicle v

    INNER JOIN RepairSheetH h

    ON v.unitnr = h.UnitNr

    INNER JOIN RepairSheetL l

    ON h.RepairSheetNr = l.RepairSheetNr

    INNER JOIN CashFlow_Accounts cf

    ON h.Company = cf.Company

    AND l.cashflow = cf.CashFlow_AccID

    WHERE l.cashflow = '201708'

    AND v.Type_Fleet<>'FS'

    ORDER BY v.modelId, v.Type_Fleet

    /* I don't think you need this GROUP BY, but TEST to be sure.

    GROUP BY

    h.RepairSheetNr,

    v.unitnr,

    v.groupid,

    v.brandId,

    v.modelId,

    v.version,

    v.Type_Fleet,

    h.close_date,

    cf.CashFlow_AccID,

    h.Date,

    h.total_value,

    l.cashflow,

    h.UnitNr

    */


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • Thanks Chris, I think I need to explain myself a little bit better,

    RepairSheetNr unitnr groupid brandId modelId version Type_Fleet close_date TotalModels TotalModelsInFleet

    5000076 51 EDMN PEUGEOT 207 S 1.4 8V OF 2012-03-06 00:00:00.000 47 143

    At the moment I have the total number of models that have had tyres replaced Which is the TotalModels Column. What I am looking for is to compare this against the absolute total number of that model on the fleet. I highlighted the Field TotalmodelsInFleet, This is the expected result I would want to achieve. Any Ideas?

  • thomasrichardson2000 (10/15/2012)


    Thanks Chris, I think I need to explain myself a little bit better,

    RepairSheetNr unitnr groupid brandId modelId version Type_Fleet close_date TotalModels TotalModelsInFleet

    5000076 51 EDMN PEUGEOT 207 S 1.4 8V OF 2012-03-06 00:00:00.000 47 143

    At the moment I have the total number of models that have had tyres replaced Which is the TotalModels Column. What I am looking for is to compare this against the absolute total number of that model on the fleet. I highlighted the Field TotalmodelsInFleet, This is the expected result I would want to achieve. Any Ideas?

    It might be as simple as this:

    SELECT

    h.RepairSheetNr,

    v.unitnr,

    v.groupid,

    v.brandId,

    v.modelId,

    v.version,

    v.Type_Fleet,

    h.close_date,

    COUNT(*) OVER (PARTITION BY v.modelId, v.Type_Fleet) AS TotalModelsInFleet,

    COUNT(*) OVER (PARTITION BY v.modelId) AS TotalModels

    FROM Vehicle v

    INNER JOIN RepairSheetH h

    ON v.unitnr = h.UnitNr

    INNER JOIN RepairSheetL l

    ON h.RepairSheetNr = l.RepairSheetNr

    INNER JOIN CashFlow_Accounts cf

    ON h.Company = cf.Company

    AND l.cashflow = cf.CashFlow_AccID

    WHERE l.cashflow = '201708'

    AND v.Type_Fleet<>'FS'

    ORDER BY v.modelId, v.Type_Fleet


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • Chris just tried that, These two columns were identical

    COUNT(*) OVER (PARTITION BY v.modelId, v.Type_Fleet) AS TotalModelsInFleet,

    COUNT(*) OVER (PARTITION BY v.modelId) AS TotalModels

    Thanks for your help

Viewing 15 posts - 1 through 14 (of 14 total)

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