Advertisement
Guest User

MAIN SQL QUERY

a guest
Feb 18th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. SELECT [head_id],
  2. [code],
  3. [name],
  4. [category_id],
  5. [tag_id],
  6. Sum(total_qty) AS total_qty,
  7. Sum(total_amount) AS total_amount,
  8. [branch_id],
  9. [for_location],
  10. [transaction_date],
  11. [voided],
  12. [deleted_at],
  13. [customer_type],
  14. [salesman_id],
  15. [created_at],
  16. [brand_name],
  17. [brand_id]
  18. FROM (SELECT [category].[id]
  19. AS
  20. [category_id],
  21. [category].[code],
  22. [category].[name],
  23. [category].[tag_id],
  24. pos_sales_detail.qty * pos_sales_detail.unit_qty
  25. AS total_qty,
  26. ( pos_sales_detail.qty * pos_sales_detail.unit_qty ) *
  27. pos_sales_detail.price AS
  28. total_amount,
  29. [pos_sales].[created_for]
  30. AS [branch_id],
  31. [pos_sales].[for_location],
  32. [pos_sales].[transaction_date],
  33. [pos_sales].[voided],
  34. [pos_sales].[deleted_at],
  35. [pos_sales].[customer_type],
  36. [pos_sales].[salesman_id],
  37. [pos_sales].[created_at],
  38. [brand].[name]
  39. AS [brand_name],
  40. [brand].[id]
  41. AS [brand_id]
  42. FROM [pos_sales]
  43. INNER JOIN [pos_sales_detail]
  44. ON [pos_sales_detail].[transaction_id] = [pos_sales].[id]
  45. AND [pos_sales_detail].[deleted_at] IS NULL
  46. INNER JOIN [product]
  47. ON [pos_sales_detail].[product_id] = [product].[id]
  48. INNER JOIN [category]
  49. ON [category].[id] = [product].[category_id]
  50. AND [category].[deleted_at] IS NULL
  51. INNER JOIN [system_code] AS [brand]
  52. ON [brand].[id] = [product].[brand_id]
  53. AND [brand].[type_id] = 1
  54. WHERE [pos_sales].[deleted_at] IS NULL
  55. AND [category].[id] IN ( 152, 153 )
  56. GROUP BY [category].[id],
  57. [category].[code],
  58. [category].[name],
  59. [category].[tag_id],
  60. [pos_sales].[id],
  61. [pos_sales].[created_for],
  62. [pos_sales].[for_location],
  63. [pos_sales].[transaction_date],
  64. [pos_sales].[voided],
  65. [pos_sales].[deleted_at],
  66. [pos_sales].[customer_type],
  67. [pos_sales].[salesman_id],
  68. [pos_sales].[created_at],
  69. [brand].[name],
  70. [brand].[id],
  71. [pos_sales_detail].[id],
  72. [pos_sales_detail].[qty],
  73. [pos_sales_detail].[unit_qty],
  74. [pos_sales_detail].[price]) AS inner_query
  75. LEFT JOIN (SELECT Substring(tag_id, 2, ( Charindex('|', tag_id, 2) ) - 2)
  76. AS
  77. head_id
  78. FROM category) AS head
  79. ON [inner_query].[category_id] = [head].[head_id]
  80. GROUP BY [head_id],
  81. [total_qty],
  82. [total_amount],
  83. [code],
  84. [name],
  85. [tag_id],
  86. [category_id],
  87. [branch_id],
  88. [for_location],
  89. [transaction_date],
  90. [voided],
  91. [deleted_at],
  92. [customer_type],
  93. [salesman_id],
  94. [created_at],
  95. [brand_name],
  96. [brand_id]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement