Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am requesting for the cooperation of the experts who have not been able to find a solution after many struggles.
I want to use a query through SQL Server 2019. A picture of which
Attached here.
CREATE TABLE [dbo].[Retailer_Statement]
([SL_No] [int] IDENTITY(1,1) NOT NULL,
[Date] [date] NULL,
[Retailer_Name] [nvarchar](50) NULL,
[Retailer_Address] [nvarchar](100) NULL,
[Contact_No] [nvarchar](20) NULL,
[Due_Amount] [decimal](18, 2) NULL,
[Payment_amount] [decimal](18, 2) NULL,
[Company_Name] [nvarchar](50) NULL,
[Naration] [varchar](50) NULL
) ON [PRIMARY]
GO


insert into Retailer_Statement values ('5/2/21','a2','b2','234',1200,0,'k1','text1')
insert into Retailer_Statement values ('6/3/21','a2','b2','234',100,0,'k1','text2')
insert into Retailer_Statement values ('6/6/21','a2','b2','234',0,500,'k1','text3')
insert into Retailer_Statement values ('6/8/21','x2','y2','852',700,0,'k1','text4')
insert into Retailer_Statement values ('6/9/21','x2','y2','852',100,0,'k1','text5')
insert into Retailer_Statement values ('6/12/21','x2','y2','852',0,1000,'k1','text6')
insert into Retailer_Statement values ('7/2/21','x2','y2','852',500,0,'k2','text7')
insert into Retailer_Statement values ('7/3/21','x2','y2','852',0,800,'k2','text8')
insert into Retailer_Statement values ('7/5/21','d','e','711',600,0,'k1','text9')
insert into Retailer_Statement values ('7/8/21','d','e','711',400,0,'k1','text10')
insert into Retailer_Statement values ('7/11/21','d','e','711',200,0,'k1','text11')
insert into Retailer_Statement values ('7/12/21','d','e','711',0,500,'k2','text12')
insert into Retailer_Statement values ('7/17/21','d','e','711',800,0,'k2','text13')
insert into Retailer_Statement values ('7/18/21','d','e','711',900,0,'k1','text14')
insert into Retailer_Statement values ('7/20/21','d','e','711',600,0,'k2','text15')
insert into Retailer_Statement values ('7/21/21','d','e','711',0,1000,'k1','text16')
insert into Retailer_Statement values ('7/22/21','d','e','711',700,0,'k1','text17')
insert into Retailer_Statement values ('7/23/21','d','e','711',0,900,'k2','text18')
insert into Retailer_Statement values ('7/24/21','x2','y2','852',300,0,'k1','text19')
insert into Retailer_Statement values ('7/25/21','a2','b2','234',0,500,'k2','text20')
insert into Retailer_Statement values ('7/28/21','d','e','711',600,0,'k2','text21')
insert into Retailer_Statement values ('7/29/21','x2','y2','852',0,8000,'k1','text22')
insert into Retailer_Statement values ('7/30/21','d','e','711',9000,0,'k2','text23')
insert into Retailer_Statement values ('8/1/21','a2','b2','234',600,0,'k2','text24')
insert into Retailer_Statement values ('8/2/21','a2','b2','234',300,0,'k2','text25')
insert into Retailer_Statement values ('8/3/21','a2','b2','234',0,1100,'k1','text26')
insert into Retailer_Statement values ('8/4/21','a2','b2','234',800,0,'k2','text27')
insert into Retailer_Statement values ('8/5/21','a2','b2','234',0,3000,'k1','text28')

What I have tried:

Date Name Address contact no due payment Blance company naration
5/2/21 a2 b2 234 1200 0 1200 k1 text1
6/3/21 a2 b2 234 100 0 1300 k1 text2
6/6/21 a2 b2 234 0 500 800 k1 text3
8/3/21 a2 b2 234 0 1100 -300 k1 text26
8/5/21 a2 b2 234 0 3000 -3300 k1 text28
7/25/21 a2 b2 234 0 500 -500 k2 text20
8/1/21 a2 b2 234 600 0 100 k2 text24
8/2/21 a2 b2 234 300 0 400 k2 text25
8/4/21 a2 b2 234 800 0 1200 k2 text27
Total Balance -2100
Posted
Updated 28-Sep-21 5:18am
Comments
Richard MacCutchan 28-Sep-21 4:40am    
What is the problem?

1 solution

Lots of problems here.
First off, as Richard has said, why are you storing dates as strings? That always gives problems because the system can't tell if "5/2/21" is meant to be 5th Feb 2021 (European), 2nd May 2021 (USA), or 21st Feb 2005 (Japan / ISO), or perhaps a Persian date even.

And when you compare strings, the whole comparison is based of the first pair of different characters in the two strings, so even if it is ISO format, "5/2/21" would be after "15/2/21" because the first character is different.

Always use appropriate datatypes - it may seem more work to start with, but it saves you huge amounts of effort later!

Then there is your INSERT statements: because you don't list the columns to INSERT to, the system tries to INSERT your data to the columns in definition order: so your date information is INSERTed to your IDENTITY field, and the system won't let you do that.

Now the design: you are duplicating data, and that's a massive source of errors: your Company information should be in a separate table with a foreign key instead of duplicated each time you add a row. Same for your Retailer info.

Fix all that, and then start thinking about your problem, but explain it to us properly - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with, and we have no idea what you expect to get, or why, let alone what code you tried!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900