fork download
  1. declare
  2. @DateBegin datetime,
  3. @DateEnd datetime,
  4. @GLCode varchar(250),
  5. @Service varchar(250),
  6. @Location varchar(250),
  7. @Employee int;
  8.  
  9. set @DateBegin = :DateBegin;
  10. set @DateEnd = :DateEnd;
  11. set @Location = :Location;
  12. set @Service = :Service;
  13. set @GLCode = :GLCode;
  14. set @Employee = :Employee;
  15.  
  16. declare @header table (sort int, description varchar(50));
  17. insert into @header (sort, description)
  18. select 100, 'Location'
  19. union all
  20. select 200, 'Date'
  21. union all
  22. select 300, 'Sub-Total Sales'
  23. union all
  24. select 400, '*Services'
  25. union all
  26. select 500, '*GL Codes (Sales)'
  27. union all
  28. select 600, '*GL Codes (Discounts)'
  29. union all
  30. select 700, 'Discounts'
  31. union all
  32. select 800, 'Taxes'
  33. union all
  34. select 999, 'Total Sales';
  35.  
  36. print '@dates';
  37. declare @dates table (date datetime, sort int);
  38. insert into @dates
  39. select dateadd(day, number, @DateBegin), number + 1
  40. from master..spt_values
  41. where type = 'P'
  42. and number between 0 and datediff(day, @DateBegin, @DateEnd)
  43. union all
  44. select null, 99999;
  45.  
  46. print '@services';
  47. declare @services table (code varchar(50), sort int);
  48. insert into @services (code, sort)
  49. select
  50. s.HistoryCode,
  51. row_number() over (order by min(s.HistorySorting), s.HistoryCode)
  52. from (
  53. select distinct
  54. s.HistoryCode,
  55. s.HistorySorting
  56. from dbo.fnSplit(@Service) f
  57. inner join Service s on s.HistoryCode = ltrim(f.value)
  58. ) s
  59. group by s.HistoryCode
  60. ;
  61.  
  62. print '@glcodes';
  63. declare @glcodes table (id int, code varchar(50), name varchar(50), coef int, sort int);
  64. insert into @glcodes
  65. select
  66. p.ID, p.Code, p.Name,
  67. case p.PaidOut when 1 then -1 else 1 end,
  68. row_number() over (order by p.Code)
  69. from dbo.fnSplit(@GLCode) s
  70. inner join PaidOutCode p on ltrim(s.value) = p.Code;
  71.  
  72. print '@locations';
  73. declare @locations table (id int, code varchar(10), name varchar(50), sort int);
  74. insert into @locations
  75. select
  76. ID,
  77. code,
  78. Name,
  79. row_number() over (order by Type, code)
  80. from (
  81. select distinct
  82. l.ID,
  83. l.Type,
  84. case l.Type when 0 then 'O-' else 'F-' end + right('0000' + l.Code, 4) as code,
  85. l.Name
  86. from dbo.fnSplit(@Location) s
  87. inner join Location l
  88. on s.Value = 'o' and l.Type = 0 and l.Active = 1 and cast(l.Code as int) < 9000
  89. or s.Value = 'f' and l.Type = 1 and l.Active = 1 and cast(l.Code as int) < 9000
  90. or s.Value not in ('o', 'f') and cast(l.ID as varchar) = ltrim(s.Value)
  91. inner join (
  92. select [Object Id2] as Location
  93. from Availability
  94. where [Object Identification1] = 4
  95. and [Object Identification2] = 8
  96. and [Object Id1] = @Employee
  97. ) a on a.Location = l.ID
  98. ) s
  99. union all
  100. select null, null, null, 0
  101. union all
  102. select null, null, null, 99999;
  103.  
  104. print '@orders';
  105. declare @orders table (
  106. ID int,
  107. Location int,
  108. Sale money,
  109. Discount money,
  110. Tax money,
  111. Date datetime
  112. );
  113. insert into @orders (ID, Location, Sale, Discount, Tax, Date)
  114. select o.ID, o.Location, o.Sale, o.Discount, o.Tax, dateadd(day, datediff(day, 0, DateTimeBegin), 0)
  115. from [Order] o
  116. inner join @locations l on o.Location = l.ID
  117. /*inner join @dates d on o.DateTimeBegin >= d.Date and o.DateTimeBegin < d.Date + 1*/
  118. where o.Status = 0
  119. and o.DateTimeBegin >= @DateBegin
  120. and o.DateTimeBegin < @DateEnd + 1;
  121.  
  122. print 'TempList';
  123. delete from TempList where spid = @@spid;
  124. insert into TempList (spid, val)
  125. select @@spid, o.ID
  126. from @orders o
  127. inner join [Order Item] oi on o.ID = oi.[Order]
  128. inner join Discount d on oi.[Object Identification] = 2 and oi.[Object ID] = d.ID
  129. inner join @glcodes g on d.GLCode = g.ID and d.ReportAsPaidOut = 1;
  130.  
  131. print '#ReportSalesAnalysis_CalculatedOrders';
  132. if object_id('tempdb..#ReportSalesAnalysis_CalculatedOrders') is not null drop table #ReportSalesAnalysis_CalculatedOrders;
  133. select *
  134. into #ReportSalesAnalysis_CalculatedOrders
  135. from dbo.fnCalculateMoneyMultiple(0)
  136.  
  137. print '@glcodesdiscounts';
  138. declare @glcodesdiscounts table (glcodesort int, locationsort int, datesort int, Amount money);
  139. insert into @glcodesdiscounts (glcodesort, locationsort, datesort, Amount)
  140. select
  141. s.glcodesort,
  142. s.locationsort,
  143. d.sort,
  144. sum(s.Amount)
  145. from (
  146. select
  147. glcodesort = g.sort,
  148. locationsort = l.sort,
  149. Date = dateadd(day, datediff(day, 0, o.DateTimeBegin), 0),
  150. Amount = round(case oi.Quantity
  151. when 0 then oi.Price * (c.SubTotalPercentable + d.UpdateTax * c.SalesTaxPercentable) / 100
  152. else oi.Price
  153. end, 2)
  154. from #ReportSalesAnalysis_CalculatedOrders c
  155. inner join [Order] o on c.[Order] = o.ID
  156. inner join @locations l on o.Location = l.ID
  157. inner join [Order Item] oi on c.[Order] = oi.[Order] and oi.[Object Identification] = 2
  158. inner join Discount d on oi.[Object ID] = d.ID and d.ReportAsPaidOut = 1
  159. inner join @glcodes g on d.GLCode = g.ID and d.ReportAsPaidOut = 1
  160. ) s
  161. inner join @dates d on s.Date = d.date
  162. group by
  163. s.glcodesort,
  164. s.locationsort,
  165. d.sort;
  166.  
  167. insert into @orders (Location, Date, Discount)
  168. select l.ID, d.Date, -g.Amount
  169. from @glcodesdiscounts g
  170. inner join @locations l on g.locationsort = l.sort
  171. inner join @dates d on g.datesort = d.sort;
  172.  
  173. insert into @glcodesdiscounts (glcodesort, locationsort, datesort, Amount)
  174. select glcodesort, locationsort, 99999, sum(Amount) from @glcodesdiscounts group by glcodesort, locationsort
  175. union all
  176. select glcodesort, 99999, 99999, sum(Amount) from @glcodesdiscounts group by glcodesort
  177. union all
  178. select 999, locationsort, datesort, sum(Amount) from @glcodesdiscounts group by locationsort, datesort
  179. union all
  180. select 999, locationsort, 99999, sum(Amount) from @glcodesdiscounts group by locationsort
  181. union all
  182. select 999, 99999, 99999, sum(Amount) from @glcodesdiscounts group by case datesort when 0 then 0 end;
  183.  
  184. print '@glcodessales';
  185. declare @glcodessales table (glcodesort int, locationsort int, datesort int, Amount money);
  186. insert into @glcodessales (glcodesort, locationsort, datesort, Amount)
  187. select
  188. s.glcodesort,
  189. s.locationsort,
  190. d.sort,
  191. sum(Amount)
  192. from (
  193. select
  194. glcodesort = g.sort,
  195. locationsort = l.sort,
  196. Date = dateadd(day, datediff(day, 0, p.DateTime), 0),
  197. Amount = p.Amount * g.coef
  198. from PaidOut p
  199. inner join @glcodes g on p.PaidOutCode = g.id
  200. inner join @locations l on p.Location = l.ID
  201. where p.DateTime >= @DateBegin
  202. and p.DateTime < @DateEnd + 1
  203. ) s
  204. inner join @dates d on s.Date = d.date
  205. group by
  206. s.glcodesort,
  207. s.locationsort,
  208. d.sort;
  209.  
  210. insert into @glcodessales (glcodesort, locationsort, datesort, Amount)
  211. select glcodesort, locationsort, 99999, sum(Amount) from @glcodessales group by glcodesort, locationsort
  212. union all
  213. select glcodesort, 99999, 99999, sum(Amount) from @glcodessales group by glcodesort
  214. union all
  215. select 999, locationsort, datesort, sum(Amount) from @glcodessales group by locationsort, datesort
  216. union all
  217. select 999, locationsort, 99999, sum(Amount) from @glcodessales group by locationsort
  218. union all
  219. select 999, 99999, 99999, sum(Amount) from @glcodessales group by case datesort when 0 then 0 end;
  220.  
  221. print '@servicemoney';
  222. declare @servicemoney table (servicesort int, locationsort int, datesort int, Amount money);
  223. insert into @servicemoney (servicesort, locationsort, datesort, Amount)
  224. select
  225. s.sort,
  226. l.sort,
  227. d.sort,
  228. Amount = sum(ot.Price)
  229. from [OIL STOP REPORT].[OIL STOP REPORT].dbo.OperativeTotals ot
  230. inner join [OIL STOP REPORT].[OIL STOP REPORT].dbo.OperativeTotalsItemsDescription otid
  231. on ot.Object = otid.Object and ot.ObjectID = otid.ObjectID
  232. inner join @services s on otid.HistoryCode = s.code
  233. inner join @dates d on ot.Date = d.date
  234. inner join @locations l on ot.Location = l.id
  235. group by
  236. s.sort,
  237. l.sort,
  238. d.sort;
  239.  
  240. insert into @orders (Location, Date, Sale)
  241. select l.ID, d.Date, -s.Amount
  242. from @servicemoney s
  243. inner join @locations l on s.locationsort = l.sort
  244. inner join @dates d on s.datesort = d.sort;
  245.  
  246. insert into @servicemoney (servicesort, locationsort, datesort, Amount)
  247. select servicesort, locationsort, 99999, sum(Amount) from @servicemoney group by servicesort, locationsort
  248. union all
  249. select servicesort, 99999, 99999, sum(Amount) from @servicemoney group by servicesort
  250. union all
  251. select 999, locationsort, datesort, sum(Amount) from @servicemoney group by locationsort, datesort
  252. union all
  253. select 999, locationsort, 99999, sum(Amount) from @servicemoney group by locationsort
  254. union all
  255. select 999, 99999, 99999, sum(Amount) from @servicemoney group by case datesort when 0 then 0 end;
  256.  
  257. print '@ordermoney';
  258. declare @ordermoney table (locationsort int, datesort int, Sale money, Discount money, Tax money);
  259. insert into @ordermoney (locationsort, datesort, Sale, Discount, Tax)
  260. select
  261. l.sort,
  262. d.sort,
  263. Sale = sum(Sale),
  264. Discount = sum(Discount),
  265. Tax = sum(Tax)
  266. from @orders o
  267. inner join @locations l on o.Location = l.ID
  268. inner join @dates d on o.Date = d.date
  269. group by
  270. l.sort,
  271. d.sort;
  272.  
  273. insert into @ordermoney (locationsort, datesort, Sale, Discount, Tax)
  274. select locationsort, 99999, sum(Sale), sum(Discount), sum(Tax) from @ordermoney group by locationsort
  275. union all
  276. select 99999, 99999, sum(Sale), sum(Discount), sum(Tax) from @ordermoney
  277. union all
  278. select 0, 99999, null, null, null;
  279.  
  280. if not exists (select * from @servicemoney)
  281. delete from @header where sort = 400;
  282. if not exists (select * from @glcodessales)
  283. delete from @header where sort = 500;
  284. if not exists (select * from @glcodesdiscounts)
  285. delete from @header where sort = 600;
  286.  
  287. if object_id('tempdb..#ReportSalesAnalysis') is not null drop table #ReportSalesAnalysis;
  288.  
  289. select
  290. ld.locationsort,
  291. ld.datesort,
  292. columnsort = h.sort + case h.sort
  293. when 400 then ss.sort
  294. when 500 then ggs.sort
  295. when 600 then ggd.sort
  296. else 0
  297. end,
  298. Amount =
  299. case h.sort
  300. when 300 then isnull(o.Sale, 0)
  301. when 400 then isnull(s.Amount, 0)
  302. when 500 then isnull(gs.Amount, 0)
  303. when 600 then -isnull(gd.Amount, 0)
  304. when 700 then -isnull(o.Discount, 0)
  305. when 800 then isnull(o.Tax, 0)
  306. when 999 then isnull(o.Sale, 0) + isnull(s.Amount, 0) + isnull(gs.Amount, 0) - isnull(gd.Amount, 0) - isnull(o.Discount, 0) + isnull(o.Tax, 0)
  307. end,
  308. Caption =
  309. case h.sort
  310. when 100 then case ll.sort when 0 then h.description else case dd.sort when 99999 then 'Total' else ll.code end end
  311. when 200 then case ll.sort when 0 then h.description else convert(varchar, dd.date, 101) end
  312. when 400 then ss.code
  313. when 500 then ggs.code + ' ' + ggs.name
  314. when 600 then ggd.code + ' (Discounts)'
  315. else h.description
  316. end
  317. into #ReportSalesAnalysis
  318. from (select distinct locationsort from @ordermoney) l
  319. inner join (select distinct locationsort, datesort from @ordermoney) ld on l.locationsort = ld.locationsort
  320. inner join @locations ll on l.locationsort = ll.sort
  321. inner join @dates dd on ld.datesort = dd.sort
  322. cross join @header h
  323. left join @ordermoney o on h.sort in (300, 700, 800, 999)
  324. and ld.locationsort = o.locationsort
  325. and ld.datesort = o.datesort
  326. left join (select distinct t.*, m.servicesort from @servicemoney m left join @services t on m.servicesort = t.sort) ss
  327. on h.sort = ss.servicesort or (h.sort = 400 and ss.servicesort < 100)
  328. left join @servicemoney s on ss.servicesort = s.servicesort
  329. and ld.locationsort = s.locationsort -- this line produces the error
  330. -- the error is:
  331. -- The multi-part identifier "ld.locationsort" could not be bound
  332. and ld.datesort = s.datesort
  333. left join (select distinct t.*, m.glcodesort from @glcodessales m left join @glcodes t on m.glcodesort = t.sort) ggs
  334. on h.sort = ggs.glcodesort or (h.sort = 500 and ggs.glcodesort < 100)
  335. left join @glcodessales gs on ggs.glcodesort = gs.glcodesort
  336. and ld.locationsort = gs.locationsort
  337. and ld.datesort = gs.datesort
  338. left join (select distinct t.*, m.glcodesort from @glcodesdiscounts m left join @glcodes t on m.glcodesort = t.sort) ggd
  339. on h.sort = ggd.glcodesort or (h.sort = 600 and ggd.glcodesort < 100)
  340. left join @glcodesdiscounts gd on ggd.glcodesort = gd.glcodesort
  341. and ld.locationsort = gd.locationsort
  342. and ld.datesort = gd.datesort;
  343.  
  344. select
  345. *, (select count(distinct columnsort) from #ReportSalesAnalysis) as ColumnCnt
  346. from #ReportSalesAnalysis
  347. order by
  348. ld.locationsort,
  349. ld.datesort,
  350. columnsort;
  351.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty