SQL server 2012 - Oder by Debit, Credit - sql

I have a table data like this:
TranNo Acc No Bal Acc Debit Credit
1 511 131 1000
1 521 131 200
1 333 131 80
1 131 511 1000
1 131 521 200
1 131 333 80
Is there a way to select data and order like this? I'm using SQL Server 2012. Thank you!
TranNo Acc No Bal Acc Debit Credit
1 131 511 1000
1 511 131 1000
1 521 131 200
1 131 521 200
1 131 333 80
1 333 131 80

I think so. I think that ordering by the least and greatest of the accounts gets the pairs that you want. Then, additional logic gets the debit before the credit:
select t.*
from table t
order by (case when accno < balacc then accno else balacc end),
(case when accno < balacc then balacc else accno end),
(case when debit > 0 then 1 else 0 end);

Related

SQL to find related rows in Loop in ANSI SQL or Snowflake SQL

I have a requirement where I need to link all related CUSTOMER ID and assign a Unified Cust ID to all the related Cust_id.
Ex: for below data,
INPUT DATA
PK_ID CUST_ID_1 CUST_ID_2 CUST_ID_3
1 123 456 567
2 898 567 780
3 999 780 111
4 111 222 333
Based on CUST_ID_1/CUST_ID_2/CUST_ID_3 need to link all the and assign a Unified ID to all the rows.
OUTPUT DATA
Unified ID CUST_ID_1 CUST_ID_2 CUST_ID_3
1000 123 456 567
1000 898 567 780
1000 999 780 111
1000 111 222 333
Trying to perform Self Join but it cannot be definite. Is there a function or ANSI SQL feature which can help in this?
What i have tried,
CREATE TEMP TBL_TEMP AS(
SELECT A.PK_ID
FROM TBL A
LEFT JOIN TBL B
ON A.CUST_ID_1=B.CUST_ID_1
AND A.PK_ID<>B.PK_ID)
UPDATE TBL
FROM TBL_TEMP
SET UNIFIED_ID=SEQ_UNIF_ID.nextval
WHERE TBL.PK_ID=TBL_TEMP.PK_ID
This update i have to write for each column and multiple times.
If you are ok with gap in sequences then following is what I can come up with as of now.
update cust_temp a
set unified_id = t.unified_id
from
(
select
case
when (select count(*) from cust_temp b
where arrays_overlap(array_construct(a.cust_id_1,a.cust_id_2,a.cust_id_3),
array_construct(b.cust_id_1,b.cust_id_2,b.cust_id_3)))>1 -- match across data-set
then 1000 -- same value for common rows
else
ts.nextval --- using sequence for non-common rows
end unified_id,
a.cust_id_1,a.cust_id_2,a.cust_id_3
from cust_temp a, table(getnextval(SEQ_UNIF_ID)) ts) t
where t.cust_id_1 = a.cust_id_1
and t.cust_id_2 = a.cust_id_2
and t.cust_id_3 = a.cust_id_3;
Updated data-set
select * from cust_temp;
UNIFIED_ID
CUST_ID_1
CUST_ID_2
CUST_ID_3
1000
123
456
567
1000
898
567
780
1000
111
222
333
20000
100
200
300
1000
999
780
111
1000
234
123
901
23000
260
360
460
24000
160
560
760
Original data set -
select * from cust_temp;
UNIFIED_ID
CUST_ID_1
CUST_ID_2
CUST_ID_3
NULL
123
456
567
NULL
898
567
780
NULL
111
222
333
NULL
100
200
300
NULL
999
780
111
NULL
234
123
901
NULL
260
360
460
NULL
160
560
760
Arrays_overlap logic is thanks to #Simeon.
Following procedure can be used -
EXECUTE IMMEDIATE $$
DECLARE
duplicate number;
x number;
BEGIN
duplicate := (select count(cnt) from (select a.unified_id,count(*) cnt from cust_temp a,
cust_temp b
where
arrays_overlap(array_construct(a.cust_id_1,a.cust_id_2,a.cust_id_3),
array_construct(b.cust_id_1,b.cust_id_2,b.cust_id_3))
AND a.cust_id_1 != b.cust_id_1
AND a.cust_id_2 != b.cust_id_2
AND a.cust_id_3 != b.cust_id_3
group by a.unified_id) where cnt>1
);
for x in 1 to duplicate do
update cust_temp a
set a.unified_id = (select min(b.unified_id) uid from cust_temp b
where arrays_overlap(array_construct(a.cust_id_1,a.cust_id_2,a.cust_id_3),
array_construct(b.cust_id_1,b.cust_id_2,b.cust_id_3)));
end for;
END;
$$
;
Which will produce following output dataset -
UNIFIED_ID
CUST_ID_1
CUST_ID_2
CUST_ID_3
1000
100
200
300
2000
123
456
567
2000
898
567
780
2000
111
222
333
2000
999
780
111
2000
234
123
901
7000
260
360
460
8000
160
560
760
8000
186
160
766
For an input data-set as -
UNIFIED_ID
CUST_ID_1
CUST_ID_2
CUST_ID_3
1000
100
200
300
2000
123
456
567
3000
898
567
780
4000
111
222
333
5000
999
780
111
6000
234
123
901
7000
260
360
460
8000
160
560
760
9000
186
160
766

Cumulative Stock Holding

I am trying to create a stock holding based on the below data.
Input and Desired Output
I have tried using creating a transactions column (Starting + UK Open POs - UK Sales).
Then used the below SQL code to create a stock holding.
Sum OVER ( TRANSACTIONS)
[ <PARTITION BY No_ ]
[ <ORDER BY Date ]
But the problem is I don't want the stock holding to go into a negative. I want it to show 0, so when 960 units come in on 14/04/19 the stock holding is 921 units (960-39) instead of 116 units.
The column highlighted in yellow is my desired output. I need this over 5k SKUs (column no_)
Any help would be very appreciated.
No_ Date UK-Open PO UK-Sales Starting Stock Trans. Cumul Stock Stock Level
111111 22/03/2019 47 100 53 53 53
111111 24/03/2019 330 -330 -277 0
111111 31/03/2019 443 -443 -720 0
111111 07/04/2019 85 -85 -805 0
111111 14/04/2019 960 39 921 116 921
111111 21/04/2019 960 112 848 964 1769
111111 28/04/2019 100 -100 864 1669
111111 05/05/2019 504 -504 360 1165
111111 12/05/2019 606 -606 -246 559
111111 19/05/2019 118 -118 -364 441
111111 26/05/2019 400 -400 -764 41
111111 02/06/2019 674 -674 -1438 0
111111 09/06/2019 338 -338 -1776 0
111111 16/06/2019 206 -206 -1982 0
111111 23/06/2019 115 -115 -2097 0
111111 30/06/2019 500 66 434 -1663 434
111111 07/07/2019 33 -33 -1696 401
Suppressing the negative numbers as you are doing requires remembering what has happened on all previous rows. Alas, this can't be done using window function.
The alternative is a recursive CTE:
with t as (
select no_, date, starting_stock, trans,
row_number() over (partition by no_ order by date) as seqnum
from <table>
),
cte as (
select no_, date, trans, seqnum,
starting_stock as stock_level
from t
where seqnum = 1
union all
select t.no_, t.date, t.trans, t.seqnum,
(case when cte.starting_stock + t.trans < 0 then 0
else cte.starting_stock + t.trans
end) as stock_level
from cte join
t
on t.seqnum = cte.seqnum + 1 and
t.no_ = cte.no_
)
select *
from cte
option (maxrecursion 0);
You only need the option if the number of rows exceeds 100 from the recursion.

SQL: Increment a row when value in another row changes

I have the following table:
Sequence Change
100 0
101 0
103 0
106 0
107 1
110 0
112 1
114 0
115 0
121 0
126 1
127 0
134 0
I need an additional column, Group, whose values increment based on the occurrence of 1 in Change. How is that done? I'm using Microsoft Server 2012.
Sequence Change Group
100 0 0
101 0 0
103 0 0
106 0 0
107 1 1
110 0 1
112 1 2
114 0 2
115 0 2
121 0 2
126 1 3
127 0 3
134 0 3
You want a cumulative sum:
select t.*, sum(change) over (order by sequence) as grp
from t;

proc sql statement to sum on values/rows that match a condition

I have a data table like below:
Table 1:
ROWID PERSONID YEAR pidDifference TIMETOEVENT DAYSBETVISIT
10 111 2009 . 100 .
110 120 2009 9 10 .
231 120 2009 0 20 10
222 120 2010 0 40 20
221 222 2009 102 10 30
321 222 2009 0 30 20
213 222 2009 0 10 20
432 321 2009 99 10 0
211 432 2009 111 20 10
212 432 2009 0 20 0
I want to sum over the DAYSBETVISIT column only when the pidDifference value is 0 for each PERSONID. So I wrote the following proc sql statement.
proc sql;
create table table5 as
(
select rowid, YEAR, PERSONID, pidDifference, TIMETOEVENT, DAYSBETVISIT,
SUM(CASE WHEN PIDDifference = 0 THEN DaysBetVisit ELSE 0 END)
from WORK.Table4_1
group by PERSONID,TIMETOEVENT, YEAR
);
quit;
However, the result I got was not summing the DAYSBETVISIT values in rows where PIDDifference = 0 within the same PERSONID. It just output the same value as was present in DAYSBETVISIT in that specific row.
Column that I NEED (sumdays) but don't get with above statement (showing the resultant column using above statement as OUT:
ROWID PERSONID YEAR pidDifference TIMETOEVENT DAYSBETVISIT sumdays OUT
10 111 2009 . 100 . 0 0
110 120 2009 9 10 . 0 0
231 120 2009 0 20 10 30 10
222 120 2010 0 40 20 30 20
221 222 2009 102 10 30 0 0
321 222 2009 0 30 20 40 20
213 222 2009 0 10 20 40 20
432 321 2009 99 10 0 0 0
211 432 2009 111 20 10 0 0
212 432 2009 0 20 0 0 0
I do not know what I am doing wrong.
I am using SAS EG Version 7.15, Base SAS version 9.4.
For your example data it looks like you just need to use two CASE statements. One to define which values to SUM() and another to define whether to report the SUM or not.
proc sql ;
select personid, piddifference, daysbetvisit, sumdays
, case when piddifference = 0
then sum(case when piddifference=0 then daysbetvisit else 0 end)
else 0 end as WANT
from expect
group by personid
;
quit;
Results
pid
PERSONID Difference DAYSBETVISIT sumdays WANT
--------------------------------------------------------
111 . . 0 0
120 0 10 30 30
120 0 20 30 30
120 9 . 0 0
222 0 20 40 40
222 0 20 40 40
222 102 30 0 0
321 99 0 0 0
432 0 0 0 0
432 111 10 0 0
SAS proc sql doesn't support window functions. I find the re-merging aggregations to be a bit difficult to use, except in the obvious cases. So, use a subquery or join and group by:
proc sql;
create table table5 as
select t.rowid, t.YEAR, t.PERSONID, t.pidDifference, t.TIMETOEVENT, t.DAYSBETVISIT,
tt.sum_DaysBetVisit
from WORK.Table4_1 t left join
(select personid, sum(DaysBetVisit) as sum_DaysBetVisit
from WORK.Table4_1
group by personid
having min(pidDifference) = max(pidDifference) and min(pidDifference) = 0
) tt
on tt.personid = t.personid;
Note: This doesn't handle NULL values for pidDifference. If that is a concern, you can add count(pidDifference) = count(*) to the having clause.

How to assign correlative numbers to rows only using SQL?

I have the following table in an Oracle database:
InvoiceNumber InvoiceDate InvoiceCorrelative
------------- ----------- ------------------
123 02-03-2009 0
124 02-03-2009 0
125 02-04-2009 0
126 02-04-2009 0
127 02-04-2009 0
128 02-05-2009 0
129 02-06-2009 0
130 02-06-2009 0
... ... ...
And I want to set a value for the InvoiceCorrelative column in every row in order to have a sequence of numbers starting at 1 for each date. In the example above I want the table to look like this:
InvoiceNumber InvoiceDate InvoiceCorrelative
------------- ----------- ------------------
123 02-03-2009 1
124 02-03-2009 2
125 02-04-2009 1
126 02-04-2009 2
127 02-04-2009 3
128 02-05-2009 1
129 02-06-2009 1
130 02-06-2009 2
... ... ...
Is it possible to do it only using SQL statements?. I've been playing with rownum but didn't get anywhere.
Try:
ROW_NUMBER() OVER (PARTITION BY InvoiceDate ORDER BY InvoiceNumber)
Using Standard SQL,
Update TableName T Set
InvoiceCorrelative =
(Select Count(*) From TableName
Where InvoiceDate = T.InvoiceDate
And InvoiceNumber <= T.InvoiceNumber)