|
i have three tables:
Table_provider
Name_Provider
Table_purchase
ID_Purchase
Date_Purchase
Name_Provider
Amount
Table_payment
ID_payment
Date_payment
Name_Provider
Amount
I want to get this resutl:
Name_Provider Sum(Amount purchase) sum(Amount payement)
thanks in advance:
|
|
|
|
|
Member 10283191 wrote: I want to get this resutl: Try SQL. You're welcome.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Im developping vb.net application with access database thats why i want this query
|
|
|
|
|
I would love to help if you have a question, but you are ordering code.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Identify the primary/foreign keys, make the joins and create the query.
If name_provider is your FK then you are screwed as that is an editable field. Get a book on SQL and that will explain the errors in that structure.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
As others have pointed out, that is a bad table schema. If you insist on having separate tables for purchase and payment then it should look like this:
Table_provider
ID_Provider
Name_Provider
Table_purchase
ID_Purchase
Date_Purchase
ID_Provider
Amount
Table_payment
ID_payment
Date_payment
ID_Provider
Amount Personally I would have single transaction table with a transaction type column but each to their own.
Whether you follow my advice or not, you will need to JOIN the tables based on the column that is common to all of them ID_Provider or Name_Provider if you leave things as they are. Here is an article that tells you how to do that Joining Tables in SQL[^]
Note we are referring to "SQL" in the sense of T-SQL - the "language" and not SQL Server the database. As you are connecting from VB.NET to Access I presume you are using ADO or OLEDB - both of which will require SQL statements.
Once you have worked out how to join your tables you already have the SELECT clause essentially written...
SELECT Provider.Name_Provider. Sum(purchase.Amount), sum(Payment.Amount)
Give it a go, but if you still get stuck reply to this message with the code that isn't working and we will try to help.
|
|
|
|
|
CHill60 wrote: Personally I would have single transaction table with a transaction type column but each to their own This only works for a retail POS system where there is no credit supplied. As an invoicing exercise the link between purchase and payment is never designed as 1-1.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Quote: As an invoicing exercise the link between purchase and payment is never designed as 1-1. Good point, although I wasn't trying to suggest that sort of link between an invoice and a payment, just that in its simplest terms a transaction is money either positive or negative. Similar to Accounting Systems Model[^]
Of course the OP doesn't have a column to indicate a credit transaction on the purchase table anyway. Or an Order table, or Invoice, or product ....
|
|
|
|
|
I suspect the OP is either doing a training excercise or, Ghu forbid, writing something for a friend in need, one does not know retail and the other does not know software, fun times.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi all,
I have a stored procedure below:
ALTER PROCEDURE [dbo].[Usp_Add_ProgramType] (@Type varchar(15), @Description varchar(15), @IsValid bit
, @CreatedBy varchar(500), @ModifiedBy varchar(500))
AS
BEGIN
if not exists(select top 1 1 from [dbo].[ProgramTypeLKP] where [Type]=@Type)
begin
INSERT INTO [dbo].[ProgramTypeLKP] ([Type], [Description], IsValid, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy)
values(@Type, @Description, @IsValid, getdate(), @CreatedBy, GETDATE(), @ModifiedBy)
end
--else
--begin
----Want to return some thing here that makes EF funnction to return 0 or -1 or some value so that I can show user a message that its a duplicate record.
--end
END
Even if the Condition fails the EF method is returning value as 1, but I want to get a value on which I will show the end user a message that he is trying to attempt to insert a duplicate record.
Like in the below script, I am checking for the result or errors, I have to get error in the response, or at least in the result some where so that I can show the user a message, I am using Kendo Grid, Create and Update with Popup (just a foot-note):
function onRequestEnd(e)
{
//Check request type
if (e.type == "create")
{
if ((e.response != null) && ((e.response.Errors != null) || (e.response.data["0"].Result <= 1)))
{
//Set some label value in red only when to say attempt to insert duplicate record
}
}
else if (e.type == "update")
{
if ((e.response != null) && ((e.response.Errors != null) || (e.response.data["0"].Result <= 1)))
{
//Set some label value in red only when to say attempt to insert duplicate record
}
}
}
Can anybody please help me in this regards, thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
ProgramType - I would have assumed that the client app already has a list of prexisting records to check against.
Assuming ProgramType has an identity field to generate the ID I would return the Select @@Scope_Identity after the insert or select -1 in the else segment
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I tried it as below but still in the duplicate situation also it was returning me value 1, I didn't really get it why?
ALTER PROCEDURE [dbo].[Usp_Add_LookupRecord] (@LookupTableId int,
@Type varchar(100), @Description varchar(500), @ForeignKeyId int, @CreatedBy varchar(500), @ModifiedBy varchar(500), @IsValid bit<br />
)
AS
BEGIN
declare @LookupTableName varchar(100)=(select top 1 LookupTableName FROM [dbo].[ListOfLookupTables] WHERE PkListOfLookupTablesId=@LookupTableId)
declare @InsertedId int=0, @FKProgramTypeLKPId int = (select top 1 PKProgramTypeLKPId from ProgramTypeLKP where Type='Mental Health')
IF (@LookupTableName='ProgramTypeLKP')
BEGIN
IF NOT EXISTS(SELECT Top 1 1 FROM dbo.ProgramTypeLKP WHERE [Type]=@Type AND ISNULL(IsValid, 1)=1)
BEGIN
INSERT INTO dbo.ProgramTypeLKP ([Type], [Description], CreatedDate, CreatedBy, ModifiedDate, ModifiedBy, IsValid)
VALUES(LEFT(@Type, 15), LEFT(@Description, 500), GETDATE(), LEFT(@CreatedBy, 500), GETDATE(), LEFT(@ModifiedBy, 500), @IsValid)
SET @InsertedId=SCOPE_IDENTITY()
END
ELSE
BEGIN
RETURN 0
END
END
ELSE IF (@LookupTableName='AddressTypeLKP')
BEGIN
IF NOT EXISTS(SELECT Top 1 1 FROM dbo.AddressTypeLKP WHERE [Description]=@Type AND ISNULL(IsValid, 1)=1)
BEGIN
INSERT INTO dbo.AddressTypeLKP ([Description], FKProgramTypeLKPId, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy, IsValid)
VALUES(LEFT(@Description, 20), @FKProgramTypeLKPId, GETDATE(), LEFT(@CreatedBy, 30), GETDATE(), LEFT(@ModifiedBy, 30), @IsValid)
SET @InsertedId=SCOPE_IDENTITY()
END
ELSE
BEGIN
RETURN 0
END
END
RETURN @InsertedId
END
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
That is one nasty design! I would have a seperate procedure for each table!
indian143 wrote: IF NOT EXISTS(SELECT Top 1 1 FROM dbo.ProgramTypeLKP WHERE [Type]=@Type AND ISNULL(IsValid, 1)=1) Test the result of that query - I'll bet it never finds a record.
The following does not seem to make sense outside of an IF statement.
indian143 wrote: declare @LookupTableName varchar(100)=(select top 1 LookupTableName FROM [dbo].[ListOfLookupTables] WHERE PkListOfLookupTablesId=@LookupTableId)
declare @InsertedId int=0, @FKProgramTypeLKPId int = (select top 1 PKProgramTypeLKPId from ProgramTypeLKP where Type='Mental Health')
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes I can understand that, but there are 25 tables with same Columns Code and Description, one table is selected with Dropdown to add, update or delete, so I have written just one stored procedure instead of writing multiple ones. That's why I used LookupTableId, Because I have listed all those tables into one LookupTable and the LookupTableId comes from that table.
I think it better suited for this scenario, but yes in general its not a good practice.
About Program Type, its always the same Program Type that's why I just took the id and used it.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
How are you calling the stored procedure? The ExecuteSqlCommand method returns the number of rows affected, not the return value of the stored procedure.
There are two ways to get the return value:
var returnValue = new SqlParameter()
{
ParameterName = "@ReturnValue",
SqlDbType = SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output
};
context.Database.ExecuteSqlCommand("exec dbo.Usp_Add_LookupRecord", returnValue, ...other parameters here...);
int result = (int)returnValue.Value; Or:
int result = context.Database.SqlQuery<int>("exec dbo.Usp_Add_LookupRecord", ... parameters here ...).First();
sql server - EF ExecuteStoredCommand with ReturnValue parameter - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am not sure but its calling the SP in the following way:
public virtual int Usp_Add_LookupRecord(Nullable<int> lookupTableId, string type, string description, Nullable<int> foreignKeyId, string createdBy, string modifiedBy, Nullable<bool> isValid)
{
var lookupTableIdParameter = lookupTableId.HasValue ?
new ObjectParameter("LookupTableId", lookupTableId) :
new ObjectParameter("LookupTableId", typeof(int));
var typeParameter = type != null ?
new ObjectParameter("Type", type) :
new ObjectParameter("Type", typeof(string));
var descriptionParameter = description != null ?
new ObjectParameter("Description", description) :
new ObjectParameter("Description", typeof(string));
var foreignKeyIdParameter = foreignKeyId.HasValue ?
new ObjectParameter("ForeignKeyId", foreignKeyId) :
new ObjectParameter("ForeignKeyId", typeof(int));
var createdByParameter = createdBy != null ?
new ObjectParameter("CreatedBy", createdBy) :
new ObjectParameter("CreatedBy", typeof(string));
var modifiedByParameter = modifiedBy != null ?
new ObjectParameter("ModifiedBy", modifiedBy) :
new ObjectParameter("ModifiedBy", typeof(string));
var isValidParameter = isValid.HasValue ?
new ObjectParameter("IsValid", isValid) :
new ObjectParameter("IsValid", typeof(bool));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("Usp_Add_LookupRecord", lookupTableIdParameter, typeParameter, descriptionParameter, foreignKeyIdParameter, createdByParameter, modifiedByParameter, isValidParameter);
}
And when I changed the stored procedure to return -3 is record exists, it is returning me -1, here is my SP:
ALTER PROCEDURE [dbo].[Usp_Add_LookupRecord] (@LookupTableId int,
@Type varchar(100), @Description varchar(500), @ForeignKeyId int, @CreatedBy varchar(500), @ModifiedBy varchar(500), @IsValid bit<br />
)
AS
BEGIN
declare @LookupTableName varchar(100)=(select top 1 LookupTableName FROM [dbo].[ListOfLookupTables] WHERE PkListOfLookupTablesId=@LookupTableId)
declare @InsertedId int=0, @FKProgramTypeLKPId int = (select top 1 PKProgramTypeLKPId from ProgramTypeLKP where Type='Mental Health')
IF (@LookupTableName='ProgramTypeLKP')
BEGIN
IF NOT EXISTS(SELECT Top 1 1 FROM dbo.ProgramTypeLKP WHERE [Type]=@Type AND ISNULL(IsValid, 1)=1)
BEGIN
INSERT INTO dbo.ProgramTypeLKP ([Type], [Description], CreatedDate, CreatedBy, ModifiedDate, ModifiedBy, IsValid)
VALUES(LEFT(@Type, 15), LEFT(@Description, 500), GETDATE(), LEFT(@CreatedBy, 500), GETDATE(), LEFT(@ModifiedBy, 500), @IsValid)
SET @InsertedId=SCOPE_IDENTITY()
END
ELSE
BEGIN
RETURN -3
END
END
ELSE IF (@LookupTableName='AddressTypeLKP')
BEGIN
IF NOT EXISTS(SELECT Top 1 1 FROM dbo.AddressTypeLKP WHERE [Description]=@Type AND ISNULL(IsValid, 1)=1)
BEGIN
INSERT INTO dbo.AddressTypeLKP ([Description], FKProgramTypeLKPId, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy, IsValid)
VALUES(LEFT(@Description, 20), @FKProgramTypeLKPId, GETDATE(), LEFT(@CreatedBy, 30), GETDATE(), LEFT(@ModifiedBy, 30), @IsValid)
SET @InsertedId=SCOPE_IDENTITY()
END
ELSE
BEGIN
RETURN -3
END
END
RETURN @InsertedId
END
I don't understand why is it returning the -1 instead of -3, is there any way to get the result -3?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
-- modified 8-Aug-18 13:02pm.
|
|
|
|
|
Try something like this:
public virtual int Usp_Add_LookupRecord(int? lookupTableId, string type, string description, int? foreignKeyId, string createdBy, string modifiedBy, bool? isValid)
{
var returnValue = new SqlParameter
{
ParameterName = "@ReturnValue",
SqlDbType = SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output
};
var lookupTableIdParameter = new SqlParameter("LookupTableId", SqlDbType.Int)
{
Value = (object)lookupTableId ?? DBNull.Value
};
var typeParameter = new SqlParameter("Type", SqlDbType.VarChar, 100)
{
Value = (object)type ?? DBNull.Value
};
var descriptionParameter = new SqlParameter("Description", SqlDbType.VarChar, 500)
{
Value = (object)description ?? DBNull.Value
};
var foreignKeyIdParameter = new SqlParameter("ForeignKeyId", SqlDbType.Int)
{
Value = (object)foreignKeyId ?? DBNull.Value
};
var createdByParameter = new SqlParameter("CreatedBy", SqlDbType.VarChar, 500)
{
Value = (object)createdBy ?? DBNull.Value
};
var modifiedByParameter = new SqlParameter("ModifiedBy", SqlDbType.VarChar, 500)
{
Value = (object)modifiedBy ?? DBNull.Value
};
var isValidParameter = new SqlParameter("IsValid", SqlDbType.Bit)
{
Value = (object)isValid ?? DBNull.Value
};
Database.ExecuteSqlCommand("exec dbo.Usp_Add_LookupRecord",
returnValue,
lookupTableIdParameter,
typeParameter,
descriptionParameter,
foreignKeyIdParameter,
createdByParameter,
modifiedByParameter,
isValidParameter);
return (int)returnValue.Value;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes I did it thanks a lot Rich
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Hello,
120 agents dialing in predictive mode and i have 150 channels.problem is that my database is not able to simultaneously update userstate/channels state .due to it my only 35-40 agents are on call and rest waiting for call.then how can i reduce wait time?is there any database maintenance script available which i can use everyday for database maintanance?i dont want to use manually because it may cause other problem.or how can i overcome this problem?
Any help will be apprecited.
I didn't find the right solution from the Internet.
References:
http://www.vicidial.org/VICIDIALforum/viewtopic.php?t=6180
[url=https://blog.advids.co/20-video-examples-from-it-asset-management-software-solution-and-services/]IT asset management software video[/url]
Thank you.
|
|
|
|
|
What is your DBMS?
What is the OS of the computer it is installed on?
|
|
|
|
|
Spam is not a reference.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
But that has to be some sort of record? ... a link to a 10 year old question
|
|
|
|
|
please help me out ! i have accidentally delete my all data from my portable hard drive. how can i recover my data.
|
|
|
|
|
Did you try to google for "recover deleted data" or something similar?
|
|
|
|
|
Hi,
I need to drop a schema in which there are some tables, is there anyway? when I searched, the links are showing dynamic sql to generate drop table text, I want to check before dropping with if exists condition, if I am dropping them, can somebody give me any link or advice or something? I am also researching about it as I am in little urgency I am writing here if I can get some help faster, any help would be greatly helpful - thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|