SQL--------------------------------------------
ALTER proc [dbo].[Month_compare2] @parmin varchar(7)
as
declare @year int, @month int
declare @result table(ivh_revtype1 varchar(10), [year] int, name varchar(100), sum_ivd_charge float)
set @year = 2009
set @month = 06
while @year <= left(@parmin, 4)
begin
insert into @result
select
h.ivh_revtype1,
@year,
case when left(i.cht_itemcode,2) like 'LH%' then 'Line Haul' else 'Fuel Surcharge' end,
(select sum(i.ivd_charge))
from invoicedetail as i inner join invoiceheader as h on i.ivh_hdrnumber = h.ivh_hdrnumber
where month(h.ivh_deliverydate) = right(@parmin,2) and
(i.cht_itemcode like 'LH%' or left(i.cht_itemcode,2) = 'FS') and
year(h.ivh_deliverydate) = @year
group by h.ivh_revtype1, left(i.cht_itemcode,2)
set @year = @year + 1
end
--Accessorials
set @year = 2009
set @month = 06
while @year <= left(@parmin, 4)
begin
insert into @result
select
h.ivh_revtype1,
@year,
'Accessorials',
(select sum(i.ivd_charge))
from invoicedetail as i inner join invoiceheader as h on i.ivh_hdrnumber = h.ivh_hdrnumber
where month(h.ivh_deliverydate) = right(@parmin,2) and
(i.cht_itemcode not like 'LH%' and i.cht_itemcode not like 'FS%') and
year(h.ivh_deliverydate) = @year
group by h.ivh_revtype1
set @year = @year + 1
end
--Totals
set @year = 2009
set @month = 06
while @year <= left(@parmin, 4)
begin
insert into @result
select
h.ivh_revtype1,
@year,
' Totals',
(select sum(i.ivd_charge))
from invoicedetail as i inner join invoiceheader as h on i.ivh_hdrnumber = h.ivh_hdrnumber
where month(h.ivh_deliverydate) = right(@parmin,2) and
year(h.ivh_deliverydate) = @year
group by h.ivh_revtype1
set @year = @year + 1
end
select * from @result order by 1,2,3 desc
VB-------------------
Sub Get_Revenue()
Call ClearForm
Dim command As String
Dim parm As String
parm = Sheet1.Range("G7").Value
' Create a connection object.
Set SuiteConn = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the database on remote server.
strConn = strConn & "Data Source=MyIp; Initial Catalog=MyDataBase;"
'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"
'Now open the connection.
SuiteConn.Open strConn
' Create a recordset object.
Dim rsCas As ADODB.Recordset
Set rsCas = New ADODB.Recordset
'MsgBox parm
With rsCas
' Assign the Connection object.
.ActiveConnection = SuiteConn
' Extract the required records.
.Open "Month_compare2 '" & parm & "'"
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A9").CopyFromRecordset rsCas
.Close
End With
SuiteConn.Close
Set rsCas = Nothing
Set RSuiteConn = Nothing
End Sub
|