Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Should I use a nested subquery? Expand / Collapse
Author
Message
Posted Thursday, October 11, 2012 7:53 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 20, 2013 8:35 AM
Points: 40, Visits: 154
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

RepairSheetNr unitnr groupid brandId modelId version Type_Fleet Closed_date TotalModels
5000003 81 EDMN FIAT GRANDE PUNTO 1.2 8V DYNAMIC 5DR OF 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?


































Post #1371502
Posted Thursday, October 11, 2012 8:15 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 1,072, Visits: 6,336
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






Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1371525
Posted Thursday, October 11, 2012 8:38 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 20, 2013 8:35 AM
Points: 40, Visits: 154
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
Post #1371547
Posted Thursday, October 11, 2012 8:41 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 1,072, Visits: 6,336
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?



Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1371551
Posted Thursday, October 11, 2012 9:08 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 20, 2013 8:35 AM
Points: 40, Visits: 154
RepairSheetNr unitnr groupid brandId modelId version Type_Fleet close_date TotalModels
5000003 84 EDMN FIAT GRANDE PUNTO 1.2 8V DYNAMIC 5DR OF 2012-07-31 00:00:00.000 1

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
Post #1371571
Posted Thursday, October 11, 2012 9:17 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 1,072, Visits: 6,336
thomasrichardson2000 (10/11/2012)
RepairSheetNr unitnr groupid brandId modelId version Type_Fleet close_date TotalModels
5000003 84 EDMN FIAT GRANDE PUNTO 1.2 8V DYNAMIC 5DR OF 2012-07-31 00:00:00.000 1

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.



Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1371577
Posted Thursday, October 11, 2012 9:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 20, 2013 8:35 AM
Points: 40, Visits: 154
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.3CDTI SC', '95PS 5DR', 'BB', '2012-03-06 00:00:00.000', '1'
Post #1371628
Posted Thursday, October 11, 2012 10:04 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 1,072, Visits: 6,336
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.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1371637
Posted Thursday, October 11, 2012 10:21 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 20, 2013 8:35 AM
Points: 40, Visits: 154
"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

RepairSheetNr unitnr groupid brandId modelId version Type_Fleet close_date TotalModels
5000076 51 EDMN PEUGEOT 207 S 1.4 8V OF 2012-03-06 00:00:00.000 47
Post #1371659
Posted Thursday, October 11, 2012 10:24 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 1,072, Visits: 6,336
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

RepairSheetNr unitnr groupid brandId modelId version Type_Fleet close_date TotalModels
5000076 51 EDMN PEUGEOT 207 S 1.4 8V OF 2012-03-06 00:00:00.000 47


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



Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1371663
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse