I have a table transaction_details:
transaction_id
customer_id
item_id
item_number
transaction_dttm
7765
1
23
1
2022-01-15
1254
2
12
4
2022-02-03
3332
3
56
2
2022-02-15
7658
1
43
1
2022-03-01
7231
4
56
1
2022-01-15
7231
2
23
2
2022-01-29
I need to form a table of the following type customer_aggr:
customer_id
amount_spent_lm
top_item_lm
1
700
glasses
2
20000
notebook
3
100
cup
When calculating, it is necessary to take into account the current price at the time of the transaction (dict_item_prices). Customers who have not made purchases in the last month are not included in the final table. he last month is defined as the last 30 days at the time of the report creation.
There is also a table dict_item_prices:
item_id
item_name
item_price
valid_from_dt
valid_to_dt
23
phone 1
1000
2022-01-01
2022-12-31
12
notebook
5000
2022-01-02
2022-12-31
56
cup
50
2022-01-02
2022-12-31
43
glasses
700
2022-01-01
2022-12-31
I am getting all of the information that I need from the following query:
SELECT
o.id,
o.customer_context,
o.organization_name,
o.shipping_name,
o.shipping_street1,
o.shipping_city,
o.shipping_state,
o.shipping_postal_code,
order_total,
shipping_charge,
sales_tax_charge,
discount_amount,
charge_date,
ship_date,
o.email,
shipping_country,
c.status,
c.unsubscribe,
c.last_logon,
c.last_action,
c.full_name AS customer_name,
c.email AS customer_email,
c.billing_email AS customer_billing_email,
c.organization_name AS customer_org_name,
c.phone AS customer_phone,
li.valid_from_dt,
li.valid_thru_dt,
pr.name,
sum(wi.order_qty) AS printed_book_count
FROM
online_order_onlineorder AS o
LEFT OUTER JOIN online_order_weborderitem AS wi ON (wi.web_order_id = o.id
AND format = 'PRT')
LEFT OUTER JOIN customer_customer AS c ON (c.id = o.customer_id)
LEFT OUTER JOIN customer_customer_curriculum_license AS li ON (li.customer_id = c.id)
LEFT OUTER JOIN product_curriculumlicense AS pli ON (pli.product_ptr_id = li.license_id)
LEFT OUTER JOIN product_product AS pr ON (pr.id = pli.product_ptr_id)
WHERE
o.status in('F', 'FF')
AND o.charge_date >= '2019-03-05'
AND o.charge_date < '2020-10-05'
GROUP BY
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
ORDER BY
charge_date,
shipping_name
The only problem is that I can not get the o.charge_date to match the li.valid_from_dt
I have tried adding comparison operators such as:
WHERE
o.status in('F', 'FF')
AND o.charge_date >= '2019-03-05'
AND o.charge_date < '2020-10-05'
AND li.valid_from_dt >= '2019-03-05'
AND li.valid_from_dt < '2020-10-05'
but as expected it just limits the pool of li.valid_from_dt and still doesn't match up with o.charge_date I also need to account for the fact that certain orders will have NULL for the li.valid_from_dt
The only relations between o.charge_date and li.valid_from_dt is that they both share a relation with the c.id is there some way to bring these tables together to match the two dates, and keep the all of the other data the same?
I have spent a while working on this and any help is greatly appreciated.
EDIT: Additional info, here is an example of the customer_customer_curriculum_license table from the same customer.
id
valid_from_dt
valid_thru_dt
created_on
updated_on
purchase_price
max_head_count
customer_id
license_id
2262
2014-06-03
2015-06-03
2015-06-24 18:35:36.884+00
2015-07-01 21:43:55.125+00
440.00
29
4178
1
2263
2014-06-03
2015-06-03
2015-06-24 18:35:36.888+00
2015-07-01 21:43:55.128+00
440.00
19
4178
17
2264
2014-06-03
2015-07-13
2015-06-24 18:35:36.891+00
2015-06-29 21:55:30.095+00
440.00
29
4178
13
2265
2014-06-03
2015-07-13
2015-06-24 18:35:36.894+00
2015-06-29 21:54:16.496+00
440.00
19
4178
20
2266
2014-06-03
2015-07-13
2015-06-24 18:35:36.897+00
2015-07-01 21:43:55.126+00
440.00
29
4178
14
2267
2014-06-03
2015-07-13
2015-06-24 18:35:36.901+00
2015-06-29 21:41:29.784+00
440.00
29
4178
16
And an example of the online_order_onlineorder table.
id
status
customer_context
email
phone
billing_name
billing_street1
billing_street2
billing_state
billing_city
billing_postal_code
shipping_name
shipping_street1
shipping_street2
shipping_state
shipping_city
shipping_postal_code
order_total
shipping_charge
sales_tax_rate
sales_tax_charge
discount_amount
authorization_code
reference_number
transaction_id
created_on
updated_on
ship_date
charge_date
is_shipped
tracking_number
applied_offer_id
customer_id
billing_country
shipping_country
shipping_option_id
shipping_weight
customer_name
organization_contact
organization_name
gift_message
is_gift
418
FF
O
example#example.com
0.00
0.00
0.00000
0.00
0.00
2012-06-28 05:00:00+00
2015-06-24 18:40:55.194+00
f
4177
US
0.00
f
420
FF
O
example#example.com
0.00
0.00
0.00000
0.00
0.00
2012-07-05 05:00:00+00
2015-06-24 18:40:55.214+00
f
4177
US
0.00
f
The only related field between the two tables is customer_id and I need to match the o.charge_date and the li.valid_from_dt with the same o.customer_id to get the date when they purchased and when the license started that same day per each order.
My Results, as you can see the customer ordered 2019-03-05 16:02:24.10583+00, but the valid_from_dt is incorrect, it should be the same date the customer ordered.
id
customer_context
organization_name
shipping_name
shipping_street1
shipping_city
shipping_state
shipping_postal_code
order_total
shipping_charge
sales_tax_charge
discount_amount
charge_date
ship_date
email
shipping_country
status
unsubscribe
last_logon
last_action
customer_name
customer_email
customer_billing_email
customer_org_name
customer_phone
valid_from_dt
valid_thru_dt
name
printed_book_count
33733
O
None
John Doe
111 test
Test
HI
99999
180.00
0.00
0.00
0.00
2019-03-05 16:02:24.10583+00
example#example.com
United States (Domestic And Apo/Fpo/Dpo Mail)
O
f
2020-08-19 13:49:26.338082+00
None
John Doe
example#example.com
2017-04-25
2018-04-24
Valid for 365 Days
Well, from the much appreciated help from Richard Huxton, I have settled with the following SQL query.
EDITED, the previous query had an issue. The following is the correct query to use. I converted both o.charge_date and li.valid_from_dt using ::date, I was then able to match on the exact data that I needed!
SELECT DISTINCT ON (o.id)
o.id,
o.customer_context,
o.organization_name,
o.shipping_name,
o.shipping_street1,
o.shipping_city,
o.shipping_state,
o.shipping_postal_code,
order_total,
shipping_charge,
sales_tax_charge,
discount_amount,
charge_date,
ship_date,
o.email,
shipping_country,
c.status,
c.unsubscribe,
c.last_logon,
c.last_action,
c.full_name AS customer_name,
c.email AS customer_email,
c.billing_email AS customer_billing_email,
c.organization_name AS customer_org_name,
c.phone AS customer_phone,
li.valid_from_dt,
li.valid_thru_dt,
pr.name,
sum(wi.order_qty) AS printed_book_count
FROM
online_order_onlineorder AS o
LEFT OUTER JOIN online_order_weborderitem AS wi ON (wi.web_order_id = o.id
AND format = 'PRT')
LEFT OUTER JOIN customer_customer AS c ON (c.id = o.customer_id)
LEFT OUTER JOIN customer_customer_curriculum_license AS li ON (li.valid_from_dt::date = o.charge_date::date)
LEFT OUTER JOIN product_curriculumlicense AS pli ON (pli.product_ptr_id = li.license_id)
LEFT OUTER JOIN product_product AS pr ON (pr.id = pli.product_ptr_id)
WHERE
o.status in('F', 'FF')
AND o.charge_date >= '2019-03-05'
AND o.charge_date < '2020-10-05'
GROUP BY
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
ORDER BY
id,
charge_date,
shipping_name
I am trying to use the built-in filter function in PostgreSQL to filter for a date range in order to sum only entries falling within this time-frame.
I cannot understand why the filter isn't being applied.
I am trying to filter for all product transactions that have a created_at date of the previous month (so in this case that were created in June 2017).
SELECT pt.created_at::date, pt.customer_id,
sum(pt.amount/100::double precision) filter (where (date_part('month', pt.created_at) =date_part('month', NOW() - interval '1 month') and
date_part('year', pt.created_at) = date_part('year', NOW()) ))
from
product_transactions pt
LEFT JOIN customers c
ON c.id= pt.customer_id
GROUP BY pt.created_at::date,pt.customer_id
Please find my expected results (sum of the amount for each day in the previous month - for each customer_id if an entry for that day exists) and the actual results I get from the query - below (using date_trunc).
Expected results:
created_at| customer_id | amount
2017-06-30 1 220.5
2017-06-28 15 34.8
2017-06-28 12 157
2017-06-28 48 105.6
2017-06-27 332 425.8
2017-06-25 1 58.0
2017-06-25 23 22.5
2017-06-21 14 88.9
2017-06-17 2 34.8
2017-06-12 87 250
2017-06-05 48 135.2
2017-06-05 12 95.7
2017-06-01 44 120
Results:
created_at| customer_id | amount
2017-06-30 1 220.5
2017-06-28 15 34.8
2017-06-28 12 157
2017-06-28 48 105.6
2017-06-27 332 425.8
2017-06-25 1 58.0
2017-06-25 23 22.5
2017-06-21 14 88.9
2017-06-17 2 34.8
2017-06-12 87 250
2017-06-05 48 135.2
2017-06-05 12 95.7
2017-06-01 44 120
2017-05-30 XX YYY
2017-05-25 XX YYY
2017-05-15 XX YYY
2017-04-30 XX YYY
2017-03-02 XX YYY
2016-11-02 XX YYY
The actual results give me the sum for all dates in the database, so no date time-frame is being applied in the query for a reason I cannot understand. I'm seeing dates that are both not for June 2017 and also from previous years.
Use date_trunc(..) function:
SELECT pt.created_at::date, pt.customer_id, c.name,
sum(pt.amount/100::double precision) filter (where date_trunc('month', pt.created_at) = date_trunc('month', NOW() - interval '1 month'))
from
product_transactions pt
LEFT JOIN customers c
ON c.id= pt.customer_id
GROUP BY pt.created_at::date
I'm trying to get the Running total from the values from the previous months. And if the month is january I would like to get the total from the previous year and so forth.
I hope someone can help.
WITH CTE AS (SELECT COUNT(LZP.pv_zaaknummer) AS [Aantal LZP]
, YEAR(LZP.lzp_actief_vanaf) AS Jaar
, MONTH(LZP.lzp_actief_vanaf) AS Maand
FROM dm.crm_LZP_Vn_zaaktype_leefzorgplan_registrerenExtensionBase_hist AS LZP
WHERE LZP.LZP_actief_tot_LDTS > GETDATE()
GROUP BY YEAR(LZP.lzp_actief_vanaf)
, MONTH(LZP.lzp_actief_vanaf)
)
SELECT a.Jaar
, a.Maand
, a.[Aantal LZP]
, (
SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE b.Jaar <= a.Jaar
) AS [Running Total 1]
, (
SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE b.Jaar <= a.Jaar
AND b.Maand <= a.Maand
) AS [Running Total 2]
FROM CTE AS a
ORDER BY a.Jaar, a.Maand;
Results as of now, I'm getting the Totals per year and then the running totals per year:
Jaar Maand Aantal LZP Running Total 1 Running Total 2
2014 4 11 661 11
2014 5 52 661 63
2014 6 70 661 133
2014 7 76 661 209
2014 8 39 661 248
2014 9 86 661 334
2014 10 112 661 446
2014 11 120 661 566
2014 12 95 661 661
2015 1 57 3327 57
2015 2 109 3327 166
2015 3 196 3327 362
2015 4 200 3327 573
2015 5 169 3327 794
2015 6 233 3327 1097
2015 7 276 3327 1449
2015 8 224 3327 1712
2015 9 203 3327 2001
2015 10 291 3327 2404
2015 11 296 3327 2820
2015 12 412 3327 3327
2016 1 311 6062 368
2016 2 341 6062 818
2016 3 476 6062 1490
2016 4 440 6062 2141
2016 5 418 6062 2780
2016 6 500 6062 3583
2016 7 249 6062 4184
I would like it to be:
Running Total 3
11
63
133
209
248
334
446
566
661
718
827
1023
1223
1392
1625
1901
2125
2328
2619
2915
3327
3638
3979
4455
4895
5313
5813
6062
you could do it without subquery try
....................)
SELECT a.Jaar
, a.Maand
, a.[Aantal LZP],SUM([Aantal LZP]) over (order by jaar) [Running Total 1]
, SUM([Aantal LZP]) over (partition by jaar order by maand) [Running Total 2],
SUM([Aantal LZP]) over ( order by jaar,maand) [Running Total 3]
FROM CTE AS a
ORDER BY a.Jaar, a.Maand;
old version
SELECT a.Jaar
, a.Maand
, a.[Aantal LZP],(SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE b.Jaar <= a.Jaar
) [Running Total 1],
(SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE b.Jaar <= a.Jaar and Maand<=a.Maand
) [Running Total 2]
, (SELECT SUM(b.[Aantal LZP])
FROM CTE AS b
WHERE dateadd(year,jaar-1900,dateadd(month,maand-1,0)) <= dateadd(year,a.jaar-1900,dateadd(month,a.maand-1,0))
) [Running Total 3]
FROM CTE AS a
ORDER BY a.Jaar, a.Maand;
Found the Solution but now I must find to integrate it so it responds to my reportparameters Jaar and Maand
DECLARE #SalesTbl TABLE (Jaar int, Maand int, Aantal int, RunningTotal int)
DECLARE #Jaar int,
#Maand int,
#Aantal int,
#RunningTotal int
SET #RunningTotal = 0
DECLARE rt_cursor CURSOR
FOR
WITH CTE AS
(SELECT COUNT(LZP.pv_zaaknummer) AS [Aantal LZP]
, YEAR(LZP.lzp_actief_vanaf) AS Jaar
, MONTH(LZP.lzp_actief_vanaf) AS Maand
FROM dm.crm_LZP_Vn_zaaktype_leefzorgplan_registrerenExtensionBase_hist AS LZP
WHERE LZP.LZP_actief_tot_LDTS > GETDATE()
GROUP BY LZP.lzp_actief_vanaf
)
SELECT A.Jaar, A.Maand, SUM(A.[Aantal LZP])
FROM CTE AS A
GROUP BY Jaar, Maand
ORDER BY A.Jaar, A.Maand
OPEN rt_cursor
FETCH NEXT FROM rt_cursor INTO #Jaar, #Maand, #Aantal
WHILE ##FETCH_STATUS = 0
BEGIN
SET #RunningTotal = #RunningTotal + #Aantal
INSERT #SalesTbl VALUES (#Jaar, #Maand,#Aantal,#RunningTotal)
FETCH NEXT FROM rt_cursor INTO #Jaar, #Maand, #Aantal
END
CLOSE rt_cursor
DEALLOCATE rt_cursor
SELECT * FROM #SalesTbl
MySQL client version: 5.0.24a
Hey Folks,
I have a table WorkOrders_errors that looks like this:
ID CO CAR NAME CAN BLN INDATE MODDATE EX
66897 461 57 KKLU KKLUSH9862088 AKLU6013312 1/27/2014 1:00 1/27/2014 1:00 -1
60782 461 57 KKLU KKLUHB21629300 AKLU6501153 1/26/2014 22:00 1/26/2014 22:00 1
74188 461 57 KKLU KKLUHB21629300 AKLU6501153 1/27/2014 10:00 1/27/2014 10:00 1
66645 461 57 KKLU KKLUSH8222080 AKLU6501744 1/26/2014 21:45 1/26/2014 21:45 1
63307 461 126 ZIMU ZIMUGOA321986 AMFU3037671 1/27/2014 1:15 1/27/2014 1:15 1
65081 461 24 CMDU CMDUAU1337382 AMFU3043761 1/26/2014 21:30 1/26/2014 21:30 1
72660 461 24 CMDU CMDUAU1337382 AMFU3043761 1/27/2014 9:30 1/27/2014 9:30 1
I need only the records with the most recent MODDATE, ie ID Record 74188, not 60782.
I have tried this a few ways, but without success. Most recently tried
SELECT * FROM (
SELECT * FROM WorkOrders_errors ORDER BY ModDate DESC) as tmp
GROUP BY can
ORDER BY can
'ALSO TRIED
SELECT t1.*
FROM WorkOrders_errors t1
WHERE t1.Can = (SELECT t2.Can
FROM WorkOrders_errors t2
WHERE t2.Can = t1.Can
ORDER BY t2.Can DESC
LIMIT 1)
These both seem to take a Huge amount of resources/time. The table only has about 80,000 rows.
Thanks anyone!