How to get associated timestamp with time_bucket_gapfill and MIN? - sql

I have a hypertable, sensor_data:
client_id | name | profile_id | time | exc | sensor_id | unit | val | valid
-----------+------+------------+----------------------------+-----+-----------+------+------+-------
tony | temp | 12345 | 2023-02-14 15:29:11.610973 | 0 | 12345 | c | 37.5 | t
tony | temp | 12345 | 2023-02-14 15:29:37.2002 | 0 | 12345 | c | 38.5 | t
tony | temp | 12345 | 2023-02-14 15:30:34.591719 | 0 | 12345 | c | 39.5 | t
tony | temp | 12345 | 2023-02-14 15:31:04.339514 | 0 | 12345 | c | 37.5 | t
tony | temp | 12345 | 2023-02-14 15:31:22.18442 | 0 | 12345 | c | 38.5 | t
tony | temp | 12345 | 2023-02-14 15:31:39.362446 | 0 | 12345 | c | 39.5 | t
tony | temp | 12345 | 2023-02-14 15:32:13.574646 | 0 | 12345 | c | 37.5 | t
tony | temp | 12345 | 2023-02-14 15:32:41.298819 | 0 | 12345 | c | 38.5 | t
tony | temp | 12345 | 2023-02-14 15:32:59.524967 | 0 | 12345 | c | 39.5 | t
tony | temp | 12345 | 2023-02-14 15:33:15.794619 | 0 | 12345 | c | 37.5 | t
tony | temp | 12345 | 2023-02-14 15:34:21.144824 | 0 | 12345 | c | 38.5 | t
tony | temp | 12345 | 2023-02-14 15:34:46.447752 | 0 | 12345 | c | 39.5 | t
I need to get the minimum value from the buckets with time_bucket_gapfill, but I want to see the associated time for the minimum value. For example:
minbucket | minval | time
---------------------+---------+----------------------------
2023-02-14 15:29:00 | 37.5 | 2023-02-14 15:29:11.610973
2023-02-14 15:29:30 | 38.5 | 2023-02-14 15:29:37.2002
2023-02-14 15:30:00 | |
2023-02-14 15:30:30 | 39.5 | 2023-02-14 15:30:34.591719
2023-02-14 15:31:00 | 37.5 | 2023-02-14 15:31:04.339514
2023-02-14 15:31:30 | 39.5 | 2023-02-14 15:31:39.362446
2023-02-14 15:32:00 | 37.5 | 2023-02-14 15:32:13.574646
2023-02-14 15:32:30 | 38.5 | 2023-02-14 15:32:41.298819
2023-02-14 15:33:00 | 37.5 | 2023-02-14 15:33:15.794619
2023-02-14 15:33:30 | |
2023-02-14 15:34:00 | 38.5 | 2023-02-14 15:34:21.144824
2023-02-14 15:34:30 | 39.5 | 2023-02-14 15:34:46.447752
I tried adding time to the group by, but that simply returned a Cartesian mess.
Here is the gapfill that produced the above data. I added the associated timestamps by hand.
select time_bucket_gapfill(make_interval(secs=>30), time, start=>'2023-02-14 15:29:11.610973', finish=>'2023-02-14 15:34:46.447752') as minbucket, min(val) as minval from sensor_data group by minbucket;

This is ugly, but it seems to work:
select gf.minbucket, gf.minval, av.mintime
from
(select time_bucket_gapfill(make_interval(secs=>30), time, start=>'2023-02-14 15:29:11.610973', finish=>'2023-02-14 15:34:46.447752') as minbucket, min(val) as minval from reading_parameter group by minbucket) gf
LEFT OUTER JOIN
(select time_bucket_gapfill(make_interval(secs=>30), time, start=>'2023-02-14 15:29:11.610973', finish=>'2023-02-14 15:34:46.447752') as minbucket, time as mintime, val as bval from reading_parameter) as av
on av.minbucket=gf.minbucket AND av.bval=gf.minval
order by gf.minbucket;

Related

How I can I add a count to rank null values in SQL Hive?

This is what I have right now:
| time | car_id | order | in_order |
|-------|--------|-------|----------|
| 12:31 | 32 | null | 0 |
| 12:33 | 32 | null | 0 |
| 12:35 | 32 | null | 0 |
| 12:37 | 32 | 123 | 1 |
| 12:38 | 32 | 123 | 1 |
| 12:39 | 32 | 123 | 1 |
| 12:41 | 32 | 123 | 1 |
| 12:43 | 32 | 123 | 1 |
| 12:45 | 32 | null | 0 |
| 12:47 | 32 | null | 0 |
| 12:49 | 32 | 321 | 1 |
| 12:51 | 32 | 321 | 1 |
I'm trying to rank orders, including those who have null values, in this case by car_id.
This is the result I'm looking for:
| time | car_id | order | in_order | row |
|-------|--------|-------|----------|-----|
| 12:31 | 32 | null | 0 | 1 |
| 12:33 | 32 | null | 0 | 1 |
| 12:35 | 32 | null | 0 | 1 |
| 12:37 | 32 | 123 | 1 | 2 |
| 12:38 | 32 | 123 | 1 | 2 |
| 12:39 | 32 | 123 | 1 | 2 |
| 12:41 | 32 | 123 | 1 | 2 |
| 12:43 | 32 | 123 | 1 | 2 |
| 12:45 | 32 | null | 0 | 3 |
| 12:47 | 32 | null | 0 | 3 |
| 12:49 | 32 | 321 | 1 | 4 |
| 12:51 | 32 | 321 | 1 | 4 |
I just don't know how to manage a count for the null values.
Thanks!
You can count the number of non-NULL values before each row and then use dense_rank():
select t.*,
dense_rank() over (partition by car_id order by grp) as row
from (select t.*,
count(order) over (partition by car_id order by time) as grp
from t
) t;

HiveQL Difference between rows, columns based in Dates

I have a table (t_stocks) with data like this:
exchanged,stock_symbol,closing_date,closing_price
NSE,TCS,2009-08-09,2200.1
NSE,TCS,2009-08-10,2300.1
NSE,TCS,2009-08-11,12200.1
NSE,TCS,2009-08-12,22300.1
NSE,TCS,2009-09-09,2200.1
NSE,TCS,2009-09-10,2300.1
NSE,TCS,2009-09-11,12200.1
NSE,TCS,2009-09-12,22300.1
NSE,INFY,2009-08-09,2500.34
NSE,INFY,2009-08-10,1500.34
NSE,INFY,2009-08-09,7500.34
NSE,INFY,2009-08-10,14500.34
NSE,INFY,2009-09-09,2500.34
NSE,INFY,2009-09-10,1500.34
NSE,INFY,2009-09-09,7500.34
NSE,INFY,2009-09-10,14500.34
NSE,TCS,2010-08-09,2200.1
NSE,TCS,2010-08-10,2300.1
NSE,TCS,2010-08-11,12200.1
NSE,TCS,2010-08-12,22300.1
NSE,TCS,2010-09-09,2200.1
NSE,TCS,2010-09-10,2300.1
NSE,TCS,2010-09-11,12200.1
NSE,TCS,2010-09-12,22300.1
NSE,INFY,2010-08-09,2500.34
NSE,INFY,2010-08-10,1500.34
NSE,INFY,2010-08-09,7500.34
NSE,INFY,2010-08-10,14500.34
NSE,INFY,2010-09-09,2500.34
NSE,INFY,2010-09-10,1500.34
NSE,INFY,2010-09-09,7500.34
NSE,INFY,2010-09-10,14500.34
...
...
I would need to write a query which generate a report as follow.
exchanged, stock_symbol , closing_date , closing_price , yesterday_closing , diff_yesterday_price
(Price difference between Yesterday prices and today prices) with an output like this:
+----------------+-------------------+-------------------+--------------------+------------------------+-----------------------+--+
| exchanged | stock_symbol | closing_date | closing_price | yesterday_closing | diff_yesterday_price |
+----------------+-------------------+-------------------+--------------------+------------------------+-----------------------+--+
| NSE | INFY | 2009-08-09 | 2500.34 | NULL | NULL |
| NSE | INFY | 2009-08-09 | 7500.34 | 2500.34 | -5000 |
| NSE | INFY | 2009-08-10 | 14500.34 | 7500.34 | -7000 |
| NSE | INFY | 2009-08-10 | 1500.34 | 14500.34 | 13000 |
| NSE | INFY | 2009-09-09 | 7500.34 | 1500.34 | -6000 |
| NSE | INFY | 2009-09-09 | 2500.34 | 7500.34 | 5000 |
| NSE | INFY | 2009-09-10 | 14500.34 | 1500.34 | -13000 |
| NSE | INFY | 2009-09-10 | 1500.34 | 2500.34 | 1000 |
| NSE | INFY | 2010-08-09 | 7500.34 | 14500.34 | 7000 |
| NSE | INFY | 2010-08-09 | 2500.34 | 7500.34 | 5000 |
.....
.....
May anyone give me some clues to do this in an efficient way.
Thanks in advance,
Regards.
You can use hive window function lag() to solve this. You can read more about window functions in hive here.
Here is the working DEMO in PostgreSQL, but same query works in HIVE as well.
select
exchanged,
stock_symbol,
closing_date,
closing_price,
yesterday_price,
(yesterday_price - closing_price) as diff_yesterday_price
from
(
select
*,
lag(closing_price) over (partition by stock_symbol order by closing_date) as yesterday_price
from stockExchange
) la
order by
stock_symbol,
closing_date

Error converting data type nvarchar to numeric but I have no idea why

I ran a query I created but I am getting an "Error converting data type nvarchar to numeric" error along with "Warning: Null value is eliminated by an aggregate or other SET operation." but I have no idea why as I am not converting anything.
Here is my query:
SELECT DISTINCT TOP 1000
O.Date_Entered
,O.Company_Code
,O.Division_Code
,O.Customer_Purchase_Order_Number
,O.Control_Number
,O.Customer_Number
,P.PickTicket_Number
,sh.PACKSLIP
,Accellos_Download
,Accellos_Allocated
,Accellos_Waved
,Accellos_Label
,Accellos_Last_Pick
,Accellos_Rating
,Accellos_Shipped
,Accellos_Upload
FROM [JMNYC-AMTDB].[AMTPLUS].[dbo].Orders o (nolock)
LEFT JOIN [JMNYC-AMTDB].[AMTPLUS].[dbo].PickTickets P (nolock)
on O.Company_Code = P.Company_Code
and O.Division_Code = P.Division_Code
and O.Control_Number = P.Control_Number
LEFT JOIN [JMDNJ-ACCELSQL].[A1WAREHOUSE].[dbo].SHIPHIST sh (nolock) ON o.Customer_Purchase_Order_Number = sh.cust_po
LEFT JOIN (
SELECT
Packslip
,max( case when Action like 'DNLOAD' then Date_Time end) as Accellos_Download
,max( case when Action like 'ALLOC' then Date_Time end) as Accellos_Allocated
,max( case when Action like 'WAVEORDER' then Date_Time end) as Accellos_Waved
,max( case when Action like 'NEWLABEL' then Date_Time end) as Accellos_Label
,max( case when Action like 'EOL_LSTP' then Date_Time end) as Accellos_Last_Pick
,max( case when Action like 'RATED' then Date_Time end) as Accellos_Rating
,max( case when Action like 'SHIPPED' then Date_Time end) as Accellos_Shipped
,max( case when Action like 'UPLOAD' then Date_Time end) as Accellos_Upload
FROM(
SELECT DISTINCT
Packslip
,Date_Time
,Action
from [JMDNJ-ACCELSQL].[A1Warehouse].[dbo].[RF_LOG2] RL (nolock)
)RLTS
group by Packslip
)RLTSS on Coalesce(sh.PACKSLIP, P.pickticket_number) = RLTSS.PACKSLIP
Here is a sample of the RF_LOG2 Table
+--------------------------------------+----------+----------+---------------------------------------------------------------------------------------------------------+--------+----------+----------+----------+----------+-----------+---------------------+----------------------+----------------------+--------------------+------------+----------+--------+--------+----------+------------------+------------+----------+----------+
| ROWID | PACKSLIP | BINLABEL | EXTENDED | TERMID | USERID | ACTION | QUANTITY | Q_SCALER | TOTLABEL | REFERENCE2 | REFERENCE3 | DATE_TIME | DATE_CREAT | CLIENTNAME | TENANTID | PO_NUM | SERIAL | LOCATION | LICENSE_PLATE | PURGE_FLAG | PACKSIZE | UPLOADED |
+--------------------------------------+----------+----------+---------------------------------------------------------------------------------------------------------+--------+----------+----------+----------+----------+-----------+---------------------+----------------------+----------------------+--------------------+------------+----------+--------+--------+----------+------------------+------------+----------+----------+
| BC5A92B0-F347-4E27-80C5-49798E1B6B75 | 90214801 | PICK | | 0 | | DNLOAD | 0.000000 | 0 | | E. Keith DuBose | l:1 u:1 | 20190726 13:15:29.87 | 0x00000000207E9F1E | 09 | | | | | | 1 | 1.000000 | 0 |
| 3564B24F-1AA9-42A4-83A4-D14151395CED | 90214801 | | | 0 | jsac | ALLOCORD | 0.000000 | 0 | | Allocated | READY TO WAVE | 20190726 13:25:54.51 | 0x00000000207E4672 | 09 | | | | | | 1 | 1.000000 | 0 |
| 0E5B3952-2BD4-4035-A645-1C024B8D3F10 | 90214801 | | | 0 | jsac | ALLOC | 0.000000 | 0 | | Release SWOG | | 20190726 13:25:54.54 | 0x00000000207F14C6 | 09 | | | | | | 1 | 1.000000 | 0 |
| 09575559-EB27-4CDB-8B35-56F741F779E1 | 90214801 | | | 0 | jsac | WAVEORDR | 0.000000 | 0 | | Wave:2392 | RF Picking | 20190726 15:05:31.71 | 0x00000000207EFE60 | 09 | | | | | | 1 | 1.000000 | 0 |
| 61B21B11-D638-4AA2-A94A-25B54650EBAD | 90214801 | | | 0 | | EOL_PRNT | 0.000000 | 0 | | New Carton | 00008139850296299650 | 20190726 15:06:03.79 | 0x00000000207E5A7D | 09 | | | | | | 1 | 1.000000 | 0 |
| 7B46FD91-A30D-4D92-A9E9-6024630D2710 | 90214801 | | | 0 | RFBASE | NEWLABEL | 0.000000 | 0 | 109629965 | 029629965 | | 20190726 15:06:03.80 | 0x00000000207E480E | 09 | | | | | | 1 | 1.000000 | 0 |
| 042D7D42-1D08-4926-AF5B-005868924302 | 90214801 | 3F88082A | 910B2307NSZ99000 /09 | 0 | LSAB | PICK_LP | 1.000000 | 1 | 109629965 | LP picking | | 20190726 15:55:58.92 | 0x00000000207F04F4 | 09 | | | | | 910B2307NSZ99000 | 1 | 1.000000 | 0 |
| 21711DE4-6119-47C0-B3F0-1A0AB816A679 | 90214801 | 3F88082A | 910B2307NSZ99000 /09 | 0 | LSAB | MOVE-OUT | 1.000000 | -1 | | 1 Packs of 1.000000 | via PICKING | 20190726 15:55:58.94 | 0x00000000207E32CC | 09 | | | | | | 1 | 1.000000 | 0 |
| E0D5C819-DC3C-4E21-9857-25476432A057 | 90214801 | 3F88082A | 910B2307NSZ99000 /09 | 0 | LSAB | PICKDETL | 1.000000 | -1 | 109629965 | | | 20190726 15:55:58.95 | 0x00000000207E239A | 09 | | | | | | 1 | 1.000000 | 0 |
| 20D981C1-CE83-459F-9D7A-1784CC215856 | 90214801 | | | 0 | LSAB | EOL_LSCP | 0.000000 | 0 | | Last Pick In Carton | 00008139850296299650 | 20190726 15:55:58.97 | 0x00000000207E07FE | 09 | | | | | | 1 | 1.000000 | 0 |
| CDBCBD5B-9DC7-4FE5-91C9-7C409EA4C2D9 | 90214801 | | | 0 | LSAB | PICKORDR | 0.000000 | 0 | | | | 20190726 15:55:58.97 | 0x00000000207F1CEE | 09 | | | | | | 1 | 1.000000 | 0 |
| DD637317-640E-4A8D-A8DB-9C2C587BA217 | 90214801 | 3F88082A | 910B2307NSZ99000 /09 | 0 | LSAB | PICKLINE | 1.000000 | -1 | | 1 | | 20190726 15:55:58.97 | 0x00000000207E8F55 | 09 | | | | | | 1 | 1.000000 | 0 |
| EE4D734C-8CCE-4C73-B133-C024D79A6054 | 90214801 | | | 0 | LSAB | EOL_LSTP | 0.000000 | 0 | | LAST PICK COMPLETED | 2 | 20190726 15:55:58.97 | 0x00000000207F516E | 09 | | | | | | 1 | 1.000000 | 0 |
| 06204BC1-87B1-4340-9712-C8996388B550 | 90214801 | | | 0 | BACKGRND | RATED | 0.000000 | 0 | 109629965 | 109629965 ACT99 | SHP1563345 | 20190729 08:30:39.86 | 0x000000002089F080 | 09 | | | | | | 1 | 1.000000 | 0 |
| 48759371-8B78-4901-8BE4-749FA55E1D40 | 90214801 | | | 0 | BACKGRND | EOL_SSYS | 0.000000 | 0 | | ShipSys Confirm | | 20190729 08:30:39.89 | 0x0000000020896EF1 | 09 | | | | | | 1 | 1.000000 | 0 |
| 904BF8C6-794D-4288-A594-22BA93A31095 | 90214801 | | | 0 | BACKGRND | SHIPPED | 0.000000 | 0 | | USPS PM | SHP1563345 | 20190729 08:30:39.90 | 0x000000002087F9F3 | 09 | | | | | | 1 | 1.000000 | 0 |
| ECA102C8-B7C4-46D3-A844-FBD0CFE79413 | 90214801 | | | 0 | sdob | SUSPEND | 0.000000 | 0 | | | | 20190729 09:45:40.12 | 0x00000000208922D8 | 09 | | | | | | 1 | 1.000000 | 0 |
| 867A7B87-5AB2-4EE7-8FDC-7175D406C0F0 | 90214801 | | | 0 | sdob | UNSUSPND | 0.000000 | 0 | | | | 20190729 10:07:56.88 | 0x00000000208A0AF5 | 09 | | | | | | 1 | 1.000000 | 0 |
| E5FB157B-9837-4DA8-B5D0-9A605603FD60 | 90214801 | | | 0 | sdob | SHIPCOMP | 0.000000 | 0 | | | ship_order() | 20190729 11:42:20.30 | 0x000000002089D1FD | 09 | | | | | | 1 | 1.000000 | 0 |
| 37D4B782-1184-4F91-913B-F1BA251740DF | 90214801 | | | 0 | sdob | SHIPPED | 0.000000 | 0 | | USPS PM | SHP1563345 | 20190729 11:42:20.32 | 0x000000002088482F | 09 | | | | | | 1 | 1.000000 | 0 |
| 4FDE75F7-D98B-451E-A106-0C9F29BADEE1 | 90214801 | | | 0 | sdob | EOL_EXTN | 0.000000 | 0 | | External Process | | 20190729 11:42:20.33 | 0x0000000020897E4A | 09 | | | | | | 1 | 1.000000 | 0 |
| C41D73C8-385A-4547-A684-7CEA1B7CE9DB | 90214801 | PICK | | 0 | C# | UPLOAD | 0.000000 | 0 | | E. Keith DuBose | | 20190729 11:43:45.66 | 0x00000000208A1D2F | | | | | | | 1 | 1.000000 | 0 |
+--------------------------------------+----------+----------+---------------------------------------------------------------------------------------------------------+--------+----------+----------+----------+----------+-----------+---------------------+----------------------+----------------------+--------------------+------------+----------+--------+--------+----------+------------------+------------+----------+----------+
What I am trying to do is to get the timestamp for each part of the order. So when it was created, when it was picked, and so on. I want this information to be displayed horizontally, per order. Also, it only crashed after running for like 5 minutes.
The warning Warning: Null value is eliminated by an aggregate or other SET operation happen because of the value of Date_Time that you do in max() containing NULL value.
For the error, I'm afraid that that causes from Coalesce(sh.PACKSLIP, P.pickticket_number). You should check the type and convert one of them to have the same type as another. From the hint from the table, you attached they both should be the numeric value.
To avoid the warning you can use below option.
SET ANSI_WARNINGS OFF

How to apply status on a account that has its running total reaching zero

Below is a sample data that i am trying to manipulate.
+----------------+------------------+---------+------+--------------+-------------+--+--+
| ACCOUNT_NUMBER | TRANSACTION_DATE | bal | Row# | RunningTotal | status | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 82.61 | 4 | 82.61 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 85.25 | 5 | 167.86 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 93.61 | 6 | 261.47 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 30/04/2015 | 78.95 | 7 | 340.42 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 22/05/2015 | -62.04 | 8 | 278.38 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/05/2015 | 98.95 | 9 | 377.33 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 30/06/2015 | 79.5 | 10 | 456.83 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 15/07/2015 | -345.76 | 11 | 111.07 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 12/05/2016 | -111.07 | 12 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 13 | 2.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 14 | 5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 15 | 7.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/04/2015 | 2.5 | 16 | 10 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2015 | 2.5 | 17 | 12.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/06/2015 | 0.67 | 18 | 13.17 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/07/2015 | -0.81 | 19 | 12.36 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2018 | 5.08 | 20 | 17.44 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/11/2018 | 1.02 | 21 | 18.46 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2019 | 1.48 | 22 | 19.94 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 8.38 | 23 | 8.38 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 10.65 | 24 | 19.03 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 25.07 | 25 | 44.1 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/04/2015 | 12.21 | 26 | 56.31 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/04/2015 | -20 | 27 | 36.31 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 20/05/2015 | -36.31 | 28 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/05/2015 | -3.69 | 29 | -3.69 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/05/2015 | 13.17 | 30 | 9.48 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/06/2015 | 9 | 31 | 18.48 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 25/07/2015 | -18.48 | 32 | 0 | CLEARED
Below is the script used to apply the status of the invoices. Essentially i want to be able to determine if a certain account has knocked off all its invoices. I have two conditions :
IF SUM of the balance equals zero then apply CLEARED
Second option which is what i am trying to figure out is to say. If somehow the final sum of the Running total isnt zero, but at the point where it clears let all the above invoices have cleared marker.
select *,
(CASE WHEN sum(bal) OVER (PARTITION BY ACCOUNT_NUMBER ) = 0 THEN 'CLEARED'
WHEN sum(bal) OVER (PARTITION BY ACCOUNT_NUMBER order by Row#,TRANSACTION_DATE ) = 0 THEN 'CLEARED'
else 'NOT_CLEARED'
end) as status
from #running_totals
order by Row#, TRANSACTION_DATE
Can some assist me on how to apply this
Expected Results
+----------------+------------------+---------+------+--------------+-------------+--+--+
| ACCOUNT_NUMBER | TRANSACTION_DATE | bal | Row# | RunningTotal | status | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 82.61 | 4 | 82.61 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 85.25 | 5 | 167.86 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/03/2015 | 93.61 | 6 | 261.47 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 30/04/2015 | 78.95 | 7 | 340.42 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 22/05/2015 | -62.04 | 8 | 278.38 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 31/05/2015 | 98.95 | 9 | 377.33 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 30/06/2015 | 79.5 | 10 | 456.83 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 15/07/2015 | -345.76 | 11 | 111.07 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 155 | 12/05/2016 | -111.07 | 12 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 13 | 2.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 14 | 5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/03/2015 | 2.5 | 15 | 7.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/04/2015 | 2.5 | 16 | 10 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2015 | 2.5 | 17 | 12.5 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/06/2015 | 0.67 | 18 | 13.17 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/07/2015 | -0.81 | 19 | 12.36 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2018 | 5.08 | 20 | 17.44 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 30/11/2018 | 1.02 | 21 | 18.46 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 953 | 31/05/2019 | 1.48 | 22 | 19.94 | NOT_CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 8.38 | 23 | 8.38 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 10.65 | 24 | 19.03 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/03/2015 | 25.07 | 25 | 44.1 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/04/2015 | 12.21 | 26 | 56.31 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/04/2015 | -20 | 27 | 36.31 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 20/05/2015 | -36.31 | 28 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/05/2015 | -3.69 | 29 | -3.69 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 31/05/2015 | 13.17 | 30 | 9.48 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 30/06/2015 | 9 | 31 | 18.48 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| 961 | 25/07/2015 | -18.48 | 32 | 0 | CLEARED | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| | | | | | | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| | | | | | | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
| | | | | | | | |
+----------------+------------------+---------+------+--------------+-------------+--+--+
Give this a try. Given the table of data you provided I would assume you will already have the RowNum row. You can probably improve on this, but it is a start.
;WITH CTE AS(
select t1.ACCOUNT_NUMBER AN3, t1.RowNum RN3
from #temp t1
CROSS APPLY(select ACCOUNT_NUMBER AN2,RowNum RN2
from #temp where RunningTotal=0) t2 where t1.ACCOUNT_NUMBER = t2.AN2 and t1.RowNum <= t2.RN2
)
select ACCOUNT_NUMBER,TRANSACTION_DATE,bal,RowNum,RunningTotal,Status,
CASE WHEN t2.AN3 IS NOT NULL THEN 'CLEARED'
ELSE 'NOT CLEARED' END Status
from #temp t1
LEFT JOIN CTE t2 on t1.ACCOUNT_NUMBER = t2.AN3 and t1.RowNum = t2.RN3
Try with this following logic-
SELECT *,
CASE
WHEN (SELECT MIN(ACCOUNT_NUMBER) FROM your_table) = ACCOUNT_NUMBER THEN 'CLEARED'
-- I have considered the MIN ACC_NUMBER as per your data
-- But you can also use a fix ACC_NUMBER if required like
-- WHEN 199= ACCOUNT_NUMBER THEN 'CLEARED'
WHEN
SUM(bal) OVER(
PARTITION BY ACCOUNT_NUMBER ORDER BY ACCOUNT_NUMBER
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) = 0 THEN 'CLEARED'
ELSE 'NOT_CLEARED'
END Status
FROM your_table
I think you can do this just by comparing the row number of the last row with status cleared and balance 0 to the last row with balance 0:
select rt.*,
(case when sum(bal) over (partition by account_number) = 0
then 'CLEARED'
when max(case when status = 'CLEARED' and bal = 0 then Row# end) =
max(case when bal = 0 then Row# end)
then 'CLEARED'
else 'NOT_CLEARED'
end) as status
from #running_totals rt
order by Row#, TRANSACTION_DATE

How to conditionally count rows from another table WITHOUT USING A CORRELATED SUBQUERY?

I have a dataset for which I have to conditionally count rows from table B that are between two dates in table A. I have to do this without the use of a correlated subquery in the SELECT clause, as this is not supported in Netezza - docs: https://www.ibm.com/support/knowledgecenter/en/SSULQD_7.0.3/com.ibm.nz.dbu.doc/c_dbuser_correlated_subqueries_ntz_sql.html.
Background on tables: Users can log in to a site (logins). When they log in, they can take actions, which are in (actions_taken). The desired output is a count of rows that are between the actions_taken action_date and lag_action_date.
Data and attempt found here: http://rextester.com/NLDH13254
Table: actions_taken (with added calculations - see RexTester.)
| user_id | action_type | action_date | lag_action_date | elapsed_days |
|---------|---------------|-------------|-----------------|--------------|
| 12345 | action_type_1 | 6/27/2017 | 3/3/2017 | 116 |
| 12345 | action_type_1 | 3/3/2017 | 2/28/2017 | 3 |
| 12345 | action_type_1 | 2/28/2017 | NULL | NULL |
| 12345 | action_type_2 | 3/6/2017 | 3/3/2017 | 3 |
| 12345 | action_type_2 | 3/3/2017 | 3/25/2016 | 343 |
| 12345 | action_type_2 | 3/25/2016 | NULL | NULL |
| 12345 | action_type_4 | 3/6/2017 | 3/3/2017 | 3 |
| 12345 | action_type_4 | 3/3/2017 | NULL | NULL |
| 99887 | action_type_1 | 4/1/2017 | 2/11/2017 | 49 |
| 99887 | action_type_1 | 2/11/2017 | 1/28/2017 | 14 |
| 99887 | action_type_1 | 1/28/2017 | NULL | NULL |
Table: logins
| user_id | login_date |
|---------|------------|
| 12345 | 6/27/2017 |
| 12345 | 6/26/2017 |
| 12345 | 3/7/2017 |
| 12345 | 3/6/2017 |
| 12345 | 3/3/2017 |
| 12345 | 3/2/2017 |
| 12345 | 3/1/2017 |
| 12345 | 2/28/2017 |
| 12345 | 2/27/2017 |
| 12345 | 2/25/2017 |
| 12345 | 3/25/2016 |
| 12345 | 3/23/2016 |
| 12345 | 3/20/2016 |
| 99887 | 6/27/2017 |
| 99887 | 6/26/2017 |
| 99887 | 6/24/2017 |
| 99887 | 4/2/2017 |
| 99887 | 4/1/2017 |
| 99887 | 3/30/2017 |
| 99887 | 3/8/2017 |
| 99887 | 3/6/2017 |
| 99887 | 3/3/2017 |
| 99887 | 3/2/2017 |
| 99887 | 2/28/2017 |
| 99887 | 2/11/2017 |
| 99887 | 1/28/2017 |
| 99887 | 1/26/2017 |
| 99887 | 5/28/2016 |
DESIRED OUTPUT: cnt_logins_between_action_dates field
| user_id | action_type | action_date | lag_action_date | elapsed_days | cnt_logins_between_action_dates |
|---------|---------------|-------------|-----------------|--------------|---------------------------------|
| 12345 | action_type_1 | 6/27/2017 | 3/3/2017 | 116 | 5 |
| 12345 | action_type_1 | 3/3/2017 | 2/28/2017 | 3 | 4 |
| 12345 | action_type_1 | 2/28/2017 | NULL | NULL | 1 |
| 12345 | action_type_2 | 3/6/2017 | 3/3/2017 | 3 | 2 |
| 12345 | action_type_2 | 3/3/2017 | 3/25/2016 | 343 | 7 |
| 12345 | action_type_2 | 3/25/2016 | NULL | NULL | 1 |
| 12345 | action_type_4 | 3/6/2017 | 3/3/2017 | 3 | 2 |
| 12345 | action_type_4 | 3/3/2017 | NULL | NULL | 1 |
| 99887 | action_type_1 | 4/1/2017 | 2/11/2017 | 49 | 8 |
| 99887 | action_type_1 | 2/11/2017 | 1/28/2017 | 14 | 2 |
| 99887 | action_type_1 | 1/28/2017 | NULL | NULL | 1 |
You don't need a correlated sub-query. Get the previous date using lag and join the logins table to count the actions between dates.
with prev_dates as (select at.*
,coalesce(lag(action_date) over(partition by user_id,action_type order by action_date)
,action_date) as lag_action_date
from actions_taken at
)
select at.user_id,at.action_type,at.action_date,at.lag_action_date
,at.action_date-at.lag_action_date as elapsed_days
,count(*) as cnt
from prev_dates at
join login l on l.user_id=at.user_id and l.login_date<=at.action_date and l.login_date>=at.lag_action_date
group by at.user_id,at.action_type,at.action_date,at.lag_action_date
order by 1,2,3