jordanking
Programmer
- Sep 8, 2005
- 351
Hello,
I have two queries that I want to combine the results from. They are both based on the same SQL View but one invloves grouping and the other is a flat result set:
QUERY # 1 ************
RESULTS IN
some fields were removed from following result set to make it easier to view)
QUERY # 2 **************
RESULTS IN
I want some way to include the second query's resluts in the first so it would look like:
where chrCode and intCount from the second query are somehow grouped with the chrDrivername from the first query. I don't care if the result from the second query is displayed more than once (like for each line of the respective "chrDriveName").
.....
I'd rather be surfing
I have two queries that I want to combine the results from. They are both based on the same SQL View but one invloves grouping and the other is a flat result set:
QUERY # 1 ************
Code:
SELECT
(ROW_NUMBER() OVER (PARTITION BY intDriverID ORDER BY intDriverID, intREGENT)) AS intStopNum
, intResidential
, dtServiceDate
, sintSigns
, chrProdInstruc
, chrCompany
, chrCustomer
, chrCustomerDetail
, intPhoneCode
, chrAddress
, intDriverID
, chrServiceCode
, intMLS
, intREGENT
, chrRegion
, chrName
, chrPostCode
, chrQuadrant
, chrDriverName
, ISNULL(intRemoveService,0)
FROM dbo.vw_Residential_Driver_Lists
WHERE dtServiceDate >= '2007-03-07' AND dtServiceDate < '2007-03-08' AND intRemoveService > 0
RESULTS IN
Code:
intStopNum intResidential sintSigns chrName chrPostCode chrDriverName intRemoveService
-------------------- -------------- --------- -------------------- ------------------ ------------------------ ----------------
1 65298 1 Residential WT Scott 8
2 65336 1 Residential BPP Scott 8
3 65342 1 Residential YPP Scott 8
4 65300 1 Residential WT Scott 8
5 65280 1 Residential WT Scott 8
6 65368 1 Residential WPP Scott 8
7 65356 1 Residential WT Scott 8
8 65359 1 Residential BS Scott 8
9 65309 1 Residential WT Scott 8
10 65253 1 Residential WPP Scott 8
11 65246 1 Residential WT Scott 8
...(records removed to shorten representaion)
1 65313 1 Residential WPP Brian Seals 44
2 65297 1 Residential WT Brian Seals 8
3 65393 1 Residential WS Brian Seals 8
4 65366 1 Residential WT Brian Seals 8
5 65350 1 Residential WPP Brian Seals 8
...
1 65291 1 Residential BS Grant 8
2 65289 1 Residential BS Grant 8
3 65290 1 Residential BS Grant 8
4 65278 1 Residential BS Grant 8
21 64562 1 Residential BS Grant 44
...
1 65324 1 Residential YPP Dale 8
2 65314 1 Residential BS Dale 8
3 65276 1 Residential WT Dale 8
4 65272 1 Residential WT Dale 8
5 65365 1 Residential BS Dale 8
6 65371 1 Residential WPP Dale 8
QUERY # 2 **************
Code:
SELECT chrDriverName, chrPostCode AS chrCode, COUNT(chrPostCode) AS intCount
FROM vw_Residential_Driver_Lists
WHERE (dtServiceDate >= '2007-03-07') AND (dtServiceDate < '2007-03-08') AND intRemoveService > 0
GROUP BY chrPostCode, chrDriverName
ORDER BY chrDriverName, chrCode
RESULTS IN
Code:
chrDriverName chrCode intCount
-------------------------------------------------- -------------------------------------------------- -----------
Brian Seals WPP 2
Brian Seals WS 1
Brian Seals WT 2
Dale BS 7
Dale WPP 5
Dale WT 7
Dale YPP 2
Grant BS 13
Grant WPP 5
Grant WT 3
Scott BPP 1
Scott BS 4
Scott WPP 8
Scott WT 8
Scott YPP 1
I want some way to include the second query's resluts in the first so it would look like:
Code:
intNumStop intResidential ...(all other fields from first query)... WPP WT YPP BS WS
------------- ---------------- ------------ ---------- ------------ --------- -------- ---------- ------- ---------
1 1234 scott 1 2 4 5 0
1 1689 scott 1 2 4 5 0
1 1899 scott 1 2 4 5 0
1 1534 grant 0 3 4 0 0
1 2357 grant 0 3 4 0 0
1 8794 dale 2 1 0 2 1
where chrCode and intCount from the second query are somehow grouped with the chrDrivername from the first query. I don't care if the result from the second query is displayed more than once (like for each line of the respective "chrDriveName").
.....
I'd rather be surfing