Connecting Tech Pros Worldwide Forums | Help | Site Map

Joining to two queries to extract a result set

Newbie
 
Join Date: Oct 2008
Location: south africa
Posts: 1
#1: Oct 24 '08
Hi I am new to the IT enviro...although i've used QSL query for a while now but on simple or single queries.

Here is the problem...
I've got two queries looking at a set of tables to extract the Costed and Invoiced data from the same database. I've tried to use a JOIN but it keeps failing. I've got multiple fields that needs to be linked.
Any suggestions. I've attached the query to make it easier
Expand|Select|Wrap|Line Numbers
  1. Declare @IDateFrom  DateTime,
  2.             @IDateTo DateTime,
  3.             @CDateFrom DateTime,
  4.             @CDateTo DateTime
  5.  
  6.  
  7.  
  8.  
  9. Begin
  10.     ---------------------------------------------------------------------------------------------------
  11.     ---------------------------------------------------------------------------------------------------
  12.  
  13.         Select 
  14.             c.Description as CCompany,
  15.             lo.Description as CBranch,
  16.             u.Description as CService,
  17.             ((DateName(Month,w.Shipdate))+'-'+(DateName(Year,w.Shipdate)))CShpMonth,
  18.             Sum(w.NumberOfParcels)CostedNoPcls,
  19.             Round(Sum(w.ActualMass),2)CostedActMass,
  20.             Round(Sum(w.ChargeMass),2)CostedChgMass,
  21.             Round(Sum(l.Amount),2)CostedAmount
  22.         From
  23.             tblCompany c(Nolock)  Inner Join
  24.             tblCostingWaybill w(Nolock) On c.CompanyID = w.CompanyID Inner Join
  25.             tblCostingLines l(Nolock) On w.CostingWaybillID = l.CostingWaybillID Inner Join
  26.             tblWaybill wb(Nolock) On w.WaybillID = wb.WaybillID Inner Join
  27.             tblLookUp u(Nolock) On wb.WaybillClass = u.LookUpID Inner Join
  28.             tbllocation lo(Nolock) On wb.OriginID = lo.LocationID
  29.         Where 
  30.                  w.WaybillType = 'W'
  31.         and        w.Shipdate between @CDateFrom and @CDateTo
  32.         Group By 
  33.             c.Description,
  34.             lo.Description,
  35.             u.Description,
  36.             ((DateName(Month,w.Shipdate))+'-'+(DateName(Year,w.Shipdate)))
  37.         Order by
  38.             c.Description,
  39.             lo.Description,
  40.             u.Description
  41.  
  42.             --------------------------------------------------------------------------------------------------------
  43.             --------------------------------------------------------------------------------------------------------    
  44.  
  45.  
  46.                     Select 
  47.                         ic.Description as ICompany,
  48.                         lc.Description as IBranch,
  49.                         up.Description as IService,
  50.                         ((DateName(Month,wb.Shipdate))+'-'+(DateName(Year,wb.Shipdate)))IShpMonth,
  51.                         Sum(iw.NumberOfParcels)InvoicedNoPcls,
  52.                         Round(Sum(iw.ActualMass),2)InvoicedActMass,
  53.                         Round(Sum(iw.ChargeMass),2)InvoicedChgMass,
  54.                         Round(Sum(il.Amount),2)InvoicedAmount
  55.                     From
  56.                         tblCompany ic(Nolock)  Inner Join
  57.                         tblInvoiceHeader ih(Nolock) On ic.CompanyID = ih.CompanyID Inner Join
  58.                         tblInvoiceWaybill iw(Nolock) On ih.InvoiceHeaderID = iw.InvoiceHeaderID Inner Join
  59.                         tblInvoiceLines il(Nolock) On iw.InvoiceWaybillID = il.InvoiceWaybillID Inner Join    
  60.                         tblWaybill wb(Nolock) On iw.WaybillID = wb.WaybillID Inner Join
  61.                         tblLookUp up(Nolock) On wb.WaybillClass = up.LookUpID Inner Join
  62.                         tbllocation lc(Nolock) On wb.OriginID = lc.LocationID
  63.                     Where 
  64.                              wb.WaybillType = 'W'
  65.                     and        ih.InvoiceDate between @IDateFrom and @IDateTo
  66.                     Group By 
  67.                         ic.Description,
  68.                         lc.Description,
  69.                         up.Description,
  70.                         ((DateName(Month,wb.Shipdate))+'-'+(DateName(Year,wb.Shipdate)))
  71.                     Order by
  72.                         ic.Description,
  73.                         lc.Description,
  74.                         up.Description
  75.  
  76. End

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 24 '08

re: Joining to two queries to extract a result set


If you want to join the results of both queries into one resultset then have a look at UNION.
Reply