SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Should I use a nested subquery?


Should I use a nested subquery?

Author
Message
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 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?
ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5192 Visits: 10607
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
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 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
ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5192 Visits: 10607
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
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 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

ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5192 Visits: 10607
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
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 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'

ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5192 Visits: 10607
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
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 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

ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5192 Visits: 10607
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search