SQL Query to Map Debit Transaction to Deposited Transaction in FIFO manner - sql

I am seeking help here to Map outgoing debit Transaction to deposit Transaction. Attached is the screen shot on the type of dataset I receive.
explanation of the data -
A customer can buy a Point Pack (Think of this as a Deposit of dollar amount to their account). Customer can use that Deposited dollar amount to active\Renew certain services\products(a debit Charge to their account).What I am looking for here is to Map all the debit Transaction to its deposited transaction on the basis of First In First Out (Fifo). customer can stop using certain product\service and may eligible for a credit(as a refund to their account and the point goes back to pool)
Original Data Set
Desire Output
Thank you for reply. Here is DDL and sample data.
Create Table #salesTran
(customer Varchar(20),
TransactionID varchar(20),
TransactionDate Datetime,
TransactionType Varchar(20),
TransactionSubType Varchar(20),
Points Float,
SerialNumber Varchar(50),
ServiceType Varchar(20)
)
INSERT INTO #salesTran VALUES ('Cust001','ABC001','1/1/2021 21:20:27', 'Credit','In',100,'ABCD-EFGH-IJKL-123','PointPack');
INSERT INTO #salesTran VALUES ('Cust001','ABC002','1/2/2021 18:22:36', 'Debit','Out',50,'ABCD-0001','Service A');
INSERT INTO #salesTran VALUES ('Cust001','ABC003','1/2/2021 13:56:55', 'Debit','Out',30,'ABCD-0002','Service B');
INSERT INTO #salesTran VALUES ('Cust001','ABC004','1/31/2021 20:16:31', 'Credit','In',100,'ABCD-EFGH-IJKL-456','PointPack');
INSERT INTO #salesTran VALUES ('Cust001','ABC005','2/1/2021 16:21:00', 'Debit','Out',20,'ABCD-0003','Service B');
INSERT INTO #salesTran VALUES ('Cust001','ABC006','2/1/2021 16:49:15', 'Credit','Refund',20,'ABCD-0003','Service B');
INSERT INTO #salesTran VALUES ('Cust001','ABC007','2/1/2021 16:49:20', 'Debit','Out',30,'ABCD-0003','Service C');
INSERT INTO #salesTran VALUES ('Cust001','ABC008','2/1/2021 20:46:42', 'Debit','Out',50,'ABCD-0001','Service A');
INSERT INTO #salesTran VALUES ('Cust001','ABC009','2/1/2021 20:21:51', 'Credit','In',50,'ABCD-EFGH-IJKL-789','PointPack');
INSERT INTO #salesTran VALUES ('Cust001','ABC010','3/2/2021 18:22:36', 'Debit','Out',50,'ABCD-0001','Service A');
INSERT INTO #salesTran VALUES ('Cust001','ABC011','3/2/2021 13:56:55', 'Debit','Out',30,'ABCD-0002','Service B');

Related

Can't run SQL query due to type conversion failure, key violation, and more

I get an error when trying to run this SQL query in MS Access:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, 21/09/2015);
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, 05/03/2016, 1);
This is the error:
enter image description here
The Learner table looks like this:
CREATE TABLE Learner
(
learnerPersonIDPKFK INT NOT NULL PRIMARY KEY,
registrationDate DATETIME,
recommendedByLearnerPersonIDFK INT NOT NULL,
CONSTRAINT fk_recommendedByLearnerPersonIDFK
FOREIGN KEY(recommendedByLearnerPersonIDFK)
REFERENCES Learner (learnerPersonIDPKFK)
);
Try wrapping your dates in single quotes:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '21/09/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '05/03/2016', 1);
Depending on your localization settings for the DATETIME format, you may need to use the MM/DD/YYYY format. Remember, your passing in a varchar and letting SQL try to "guess" how to implicitly convert it:
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate)
VALUES (1, '09/21/2015');
INSERT INTO Learner (learnerPersonIDPKFK, registrationDate, recommendedByLearnerPersonIDFK)
VALUES (2, '03/05/2016', 1);
Use a simple tester to verify if SQL can "guess" your date formating:
SELECT CAST('09/21/2015' AS DATETIME)

Oracle SQL Plus not all table columns displayed

I've created this table :
CREATE TABLE PRODUCT (
P_CODE CHAR(8),
P_DESCRIPT CHAR(200),
P_INDATE CHAR(20),
P_ONHAND INTEGER,
P_MIN INTEGER,
P_PRICE NUMBER,
P_DISCOUNT NUMBER,
V_CODE CHAR(5)
);
And I've inserted the following data in the table:
INSERT INTO PRODUCT VALUES ('11QER/31','Power painter, 15 psi, 3-nozzle','12/2/96',8,5,109.99,0.00,25595);
INSERT INTO PRODUCT VALUES ('13-Q2/P2','7.25-in. pwr. Saw blade','11/12/96',32,15,14.99,0.05,21344);
INSERT INTO PRODUCT VALUES ('14-Q1/L3','9.00-in. pwr. Saw blade','11/12/96',18,12,17.49,0.00,21344);
INSERT INTO PRODUCT VALUES ('156-QQ2','Hrd. Cloth, 1/4-in., 2x50','8/14/96',15,8,39.95,0.00,23119);
INSERT INTO PRODUCT VALUES ('1558-QW1','Hrd. Cloth, 1/2-in., 3x50','8/14/96',23,5,43.99,0.00,23119);
INSERT INTO PRODUCT VALUES ('2232/QTY','B&D jigsaw, 12-in, blade','10/29/96',8,5,109.92,0.05,24288);
INSERT INTO PRODUCT VALUES ('2232/QWE','B&D jigsaw, 8-in, blade','9/23/96',6,5,99.87,0.05,24288);
INSERT INTO PRODUCT VALUES ('2238/QPD','B&D cordless drill, 1/2-in.','10/19/96',12,5,38.95,0.05,25595);
INSERT INTO PRODUCT VALUES ('23109-HB','Claw hammer','11/19/96',23,10,9.95,0.10,21225);
INSERT INTO PRODUCT VALUES ('23114-AA','Sledge hammer, 12 lb.','12/1/96',8,5,14.40,0.05,null);
INSERT INTO PRODUCT VALUES ('54778-2T','Rat-tail file, 1/8-in. fine','6/14/96',43,20,4.99,0.00,'21344');
INSERT INTO PRODUCT VALUES ('89-WRE-Q','Hicut chain saw, 16 in.','7/6/96',11,5,256.99,0.05,'24288');
INSERT INTO PRODUCT VALUES ('PVC23DAT','PVC pipe, 3.5-in., 8-ft','12/19/96',188,75,5.87,0.00,null);
INSERT INTO PRODUCT VALUES ('SM-18277','1.25-in. metal screw, 25','11/28/96',172,75,6.99,0.00,'21225');
INSERT INTO PRODUCT VALUES ('SW-23116','2.5-in. wd. screw, 50','9/23/96',237,100,8.45,0.00,'21231');
INSERT INTO PRODUCT VALUES ('WR3/TT3','Steel matting, 4''x8''x1/6", .5" mesh','11/16/96',18,5,119.95,0.10,'25595');
INSERT INTO PRODUCT VALUES ('11QER/31','Power painter, 15 psi, 3-nozzle','12/2/96',8,5,109.99,0.00,25595);
INSERT INTO PRODUCT VALUES ('13-Q2/P2','7.25-in. pwr. Saw blade','11/12/96',32,15,14.99,0.05,21344);
INSERT INTO PRODUCT VALUES ('14-Q1/L3','9.00-in. pwr. Saw blade','11/12/96',18,12,17.49,0.00,21344);
INSERT INTO PRODUCT VALUES ('156-QQ2','Hrd. Cloth, 1/4-in., 2x50','8/14/96',15,8,39.95,0.00,23119);
INSERT INTO PRODUCT VALUES ('1558-QW1','Hrd. Cloth, 1/2-in., 3x50','8/14/96',23,5,43.99,0.00,23119);
INSERT INTO PRODUCT VALUES ('2232/QTY','B&D jigsaw, 12-in, blade','10/29/96',8,5,109.92,0.05,24288);
INSERT INTO PRODUCT VALUES ('2232/QWE','B&D jigsaw, 8-in, blade','9/23/96',6,5,99.87,0.05,24288);
INSERT INTO PRODUCT VALUES ('2238/QPD','B&D cordless drill, 1/2-in.','10/19/96',12,5,38.95,0.05,25595);
INSERT INTO PRODUCT VALUES ('23109-HB','Claw hammer','11/19/96',23,10,9.95,0.10,21225);
INSERT INTO PRODUCT VALUES ('23114-AA','Sledge hammer, 12 lb.','12/1/96',8,5,14.40,0.05,null);
INSERT INTO PRODUCT VALUES ('54778-2T','Rat-tail file, 1/8-in. fine','6/14/96',43,20,4.99,0.00,'21344');
INSERT INTO PRODUCT VALUES ('89-WRE-Q','Hicut chain saw, 16 in.','7/6/96',11,5,256.99,0.05,'24288');
INSERT INTO PRODUCT VALUES ('PVC23DAT','PVC pipe, 3.5-in., 8-ft','12/19/96',188,75,5.87,0.00,null);
INSERT INTO PRODUCT VALUES ('SM-18277','1.25-in. metal screw, 25','11/28/96',172,75,6.99,0.00,'21225');
INSERT INTO PRODUCT VALUES ('SW-23116','2.5-in. wd. screw, 50','9/23/96',237,100,8.45,0.00,'21231');
INSERT INTO PRODUCT VALUES ('WR3/TT3','Steel matting, 4''x8''x1/6", .5" mesh','11/16/96',18,5,119.95,0.10,'25595');
now when I do the following:
SELECT * FROM PRODUCT;
only the first 4 columns is returned!
I tried setting the following:
SET WRAP ON
SET TERMOUT OFF
SET VERIFY OFF
SET TRIMSPOOL OFF
SET LINESIZE 2000
SET LONG 200000
SET PAGES 0
And now only two columns appear.
Please help.
If the column should be CHAR(200), then you should leave it alone - don't change it to CHAR(50) only because you can't display it properly.
Instead, you are probably OK by showing only the first 50 characters when you query the table. You could do that in the query itself, like this:
select p_code, substr(p_descript, 1, 50) as p_descript, p_indate, ...
Alternatively, you can tell SQL Plus to display the column in 50-character width, with a SQL Plus command - you were trying to do that, you just didn't find the right command. It is the column command; specifically in this case:
column p_descript format a50
(note this is NOT followed by semicolon ; since it is not a SQL command) This will wrap the values that are actually longer than 50 characters. If instead you want them to be truncated, issue an additional SQL Plus command:
set wrap off

sql how can i use automatic timing

I am new to MSSQL and learning more every day, I have a website that uses a field called 'LoginTime' which is an INT and whenever a user logins that field increases +1 for that particular user . What I would like to do now is to make that field automatically start back at 0 on the 1st day of each month. That way I can get averages of how many times users login within a month at the end of each month. What should i be looking into in order to allow my sql server to do this ... triggers , stored procedures etc..
Instead of maintaining a counter on the table, add a new record for each successful login. Throw a query together that aggregates login attempts by user by month-year... example as follows:
DECLARE #LoginTable TABLE
(
UserName VARCHAR(50),
LoginTime DATETIME
)
INSERT INTO #LoginTable VALUES ('user1', '2014-01-01')
INSERT INTO #LoginTable VALUES ('user1', '2014-01-02')
INSERT INTO #LoginTable VALUES ('user1', '2014-01-08')
INSERT INTO #LoginTable VALUES ('user1', '2014-02-01')
INSERT INTO #LoginTable VALUES ('user1',' 2014-02-16')
INSERT INTO #LoginTable VALUES ('user2', '2014-01-07')
INSERT INTO #LoginTable VALUES ('user2', '2014-01-09')
INSERT INTO #LoginTable VALUES ('user2', '2014-02-24')
INSERT INTO #LoginTable VALUES ('user2',' 2014-02-26')
SELECT UserName, LEFT(CONVERT(VARCHAR, LoginTime, 120), 7) AS [Year-Month], COUNT(Logintime) AS Logins
FROM #LoginTable
GROUP BY UserName, LEFT(CONVERT(VARCHAR, LoginTime, 120), 7)
Step 1 - Drop the column from the table
Step 2 - If you don't have one already, add a table to store logins. It's fields should be the userId and the date and time of logging in.
This way, you can see how often people logged in during any date range.

SSRS: How do I display a list of customers, from A - K then L to Z, without having two separate reports?

I would like one report with a drop down menu, showing:
Customers A-K,
Customers L-Z
All Customers
I have an SQL table for customers, with the standard columns (Acc No., name, address, etc).
I would like this split by name as it takes a long time to bring the result of the entire table down.
I would prefer this to be in one report. Is this possible? I am using SQL Server 2008 R2 with reporting services.
I have tried a few different methods with no luck, so I'm open to any suggestions!
Thanks for reading my question and thanks in advance if you can find the time to help!
Please feel free to ask any questions.
create table #alphab
( alphaname varchar(15));
insert into #alphab VALUES ('ANT')
insert into #alphab values ('CAT')
insert into #alphab values ('pAT')
insert into #alphab values ('mAT')
insert into #alphab values ('dAT')
insert into #alphab values ('rAT')
insert into #alphab values ('dAT')
insert into #alphab values ('lAT')
insert into #alphab values ('cAT')
insert into #alphab values ('zAT')
insert into #alphab values ('xAT')
insert into #alphab values ('wAT')
insert into #alphab values ('oAT')
insert into #alphab values ('sAT')
insert into #alphab values ('yAT')
insert into #alphab values ('uAT')
select alphaname
from #alphab
where alphaname LIKE
CASE
WHEN #alphabetorder = 1 THEN ('[A-K | a-k]%')
WHEN #alphabetorder= 2 THEN ('[L-Z | l-z]%')
END
order by alphaname

Sql Query for range

i am developing a website
there are two form searchscreen and serachresultscreen
in first form i select a range from two dropdown list (i.e From value1 to value 2)
range is for diamond clarity like in order respectively
FL,IF,VVS,VVS1,VVS2,SI,SI1,I1,I2
when i range query it on next form like
clarity>=FL and clarity<=VVS1
then it show result with all values between alphabet F to V (i.e FL,IF,VVS,VVS1,VV2,SI,I1,I2
please suggest me some answere
My required output is(when selected between from=FL to=VVS1)
then result output value FL,IF,VVS,VVS1 only
You can not use range operator BETWEEN for your String values. To address this issue create table with corresponding numeric weighting for those Diamond Clarity values.
while filtering based on Clarity fetch its order and then put all those ORDER (Numeric Number)
within Range Operator
EDIT :
CREATE TABLE DIAMOND_CLARITY
(CLARITY VARCHAR(10),
CLARITY_ORDER int
)
INSERT INTO DIAMOND_CLARITY values ('FL',1)
INSERT INTO DIAMOND_CLARITY values ('IF',2)
INSERT INTO DIAMOND_CLARITY values ('VVS',3)
INSERT INTO DIAMOND_CLARITY values ('VVS1',4)
INSERT INTO DIAMOND_CLARITY values ('VVS2',5)
INSERT INTO DIAMOND_CLARITY values ('SI',6)
INSERT INTO DIAMOND_CLARITY values ('SI1',7)
INSERT INTO DIAMOND_CLARITY values ('I1',8)
INSERT INTO DIAMOND_CLARITY values ('I2',9)
Query to fetch records between ranges
SELECT CLARITY from DIAMOND_CLARITY where CLARITY_ORDER
BETWEEN
(SELECT CLARITY_ORDER FROM DIAMOND_CLARITY where CLARITY='FL')
AND
(SELECT CLARITY_ORDER FROM DIAMOND_CLARITY where CLARITY='VVS')