-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.txt
143 lines (129 loc) · 4.71 KB
/
queries.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//////////////
SELECT
id,
customerName,
status,
date,
assemblies_qty as assemblies,
SUM(subtotal) as price
FROM
(
SELECT
orders.id AS id,
customers.id AS customerId,
customers.first_name as customerName,
orders.date as date,
order_status.description as status,
order_assemblies.assembly_id as assembly_id,
SUM(order_assemblies.qty) as assemblies_qty,
assembly_products.qty as assembly_products_qty,
products.id as product_id,
products.description as product,
products.price as product_price,
SUM(assembly_products.qty*products.price*order_assemblies.qty) as subtotal
FROM orders
INNER JOIN customers ON customers.id = orders.customer_id
INNER JOIN order_status ON order_status.id = orders.status_id
INNER JOIN order_assemblies ON order_assemblies.id = orders.id
INNER JOIN assembly_products ON assembly_products.id = order_assemblies.assembly_id
INNER JOIN products ON products.id = assembly_products.product_id
GROUP BY orders.id, order_assemblies.assembly_id, products.id
)
GROUP BY id
///////////////
Query Joins y agrupado en diferentes campos
SELECT
orders.id AS id,
customers.id AS customerId,
customers.first_name as customerName,
order_status.description as status,
order_assemblies.assembly_id as assembly_id,
SUM(order_assemblies.qty) as assamblies_qty,
assembly_products.qty as assembly_products_qty,
products.id as product_id,
products.description as product,
products.price as product_price,
SUM(assembly_products.qty*products.price*order_assemblies.qty) as subtotal
FROM orders
INNER JOIN customers ON customers.id = orders.customer_id
INNER JOIN order_status ON order_status.id = orders.status_id
INNER JOIN order_assemblies ON order_assemblies.id = orders.id
INNER JOIN assembly_products ON assembly_products.id = order_assemblies.assembly_id
INNER JOIN products ON products.id = assembly_products.product_id
WHERE customers.id = 0
GROUP BY orders.id, order_assemblies.assembly_id, products.id
//////// Trae productos del mismo assembly
SELECT products.*, assembly_products.id FROM products
INNER JOIN assembly_products ON assembly_products.product_id = products.id
WHERE assembly_products.id = 1
ORDER BY description ASC
////////////
SELECT tassemblies.id,
tassemblies.description,
COUNT(assemblies.id) as numProducts,
"SUM(products.price) as price
FROM assemblies
"INNER JOIN assembly_products ON assembly_products.id = assemblies.id
"INNER JOIN products ON products.id = assembly_products.product_id
"WHERE assemblies.description LIKE :desc
"GROUP BY assemblies.id
"ORDER BY assemblies.description ASC"
SELECT
assemblies.id,
assemblies.description,
COUNT(assemblies.id) as numProducts,
SUM(products.price) as price
FROM assemblies
INNER JOIN assembly_products ON assembly_products.id = assemblies.id
INNER JOIN products ON products.id = assembly_products.product_id
WHERE assemblies.description LIKE
///////
SELECT
orders.id,
customers.first_name,
order_status.description as status,
orders.date ,
order_assemblies.qty,
orders.change_log,
order_assemblies.assembly_id,
assemblies.description
FROM orders
INNER JOIN customers ON customers.id = orders.customer_id
INNER JOIN order_assemblies ON order_assemblies.id = orders.id
INNER JOIN order_status ON order_status.id = orders.status_id
INNER JOIN assemblies ON assemblies.id = order_assemblies.assembly_id
WHERE orders.customer_id = 1
ORDER BY date
//////////// PRODUCT QUANTITIES ORDERS PENDIENTES
SELECT
orders.id as orderId,
orders.status_id as statusId,
orders.customer_id as customerId,
order_assemblies.assembly_id as assembly_id,
order_assemblies.qty as assembliesQty,
assembly_products.product_id as productId,
assembly_products.qty as productQtyPerAssembly,
SUM(order_assemblies.qty*assembly_products.qty) as pendingProductsQty,
products.qty as stockProductsQty,
products.description as productDescription,
SUM(products.qty - order_assemblies.qty*assembly_products.qty) as diffProductsQty
FROM orders
INNER JOIN order_assemblies ON order_assemblies.id = orders.id
INNER JOIN assembly_products ON assembly_products.id = order_assemblies.assembly_id
INNER JOIN products ON products.id = product_id
WHERE status_id=0 AND orders.id=7
GROUP BY productId
HAVING diffProductsQty < 0
/////// Query ganancias
SELECT
orders.*,
products.*,
order_assemblies.id as assembly_id,
strftime("%m-%Y", date) as month_year,
COUNT(products.price) as sales,
SUM(products.price) as income
FROM orders
INNER JOIN order_assemblies ON order_assemblies.id = orders.id
INNER JOIN assembly_products ON assembly_products.id = order_assemblies.assembly_id
INNER JOIN products ON products.id = product_id
group by strftime("%m-%Y", date);