Average a null columns in stored procedure - c#

I have a stored procedure and I want to average null columns.
This is my stored procedure :
SELECT
AVG(planned) AS Planned,
AVG(achieved) AS Achieved
FROM
Port
INNER JOIN
Technology ON Port.portID = Technology.portRef
I bind this stored procedure to a chart using datasource and when the column is null the C# code throws this error:
Value was either too large or too small for a Decimal.
How can I handle my stored procedure to avg those null columns?

This happens when the query does not return any values. Use this
SELECT
coalesce(avg(planned),0) as Planned,
coalesce(avg(achieved),0) as Achieved
FROM
Port inner join Technology on Port.portID = Technology.portRef

another way
SELECT avg(isnull(planned,0))as Planned,avg(isnull(achieved,0)) as Achieved
FROM Port inner join Technology on Port.portID = Technology.portRef

I have seen several people getting confused whether to use ISNULL or COALESCE.
I would strongly recommend COALESCE. In some cases both will run fine, but in some cases ISNULL if not properly handled will give wrong output.
Please check the below snippet :
SELECT
ISNULL(Nullif('test', 'test'), '12345') AS using_isnull,
COALESCE(Nullif('test', 'test'), '12345') AS using_coalesce,
ISNULL(Nullif('test', 'test'), 12345) AS int_using_isnull,
COALESCE(Nullif('test', 'test'), 12345) AS int_using_coalesce

Use the keyword unpivot
select id, AVG(Q)
from (select * from myTable) a
unpivot(Q for QQ IN(Q1,Q2,Q3,Q4,Q5)) b
group by id
If all the columns are null, it won't return anything though.

Related

Linq statement that queries field by multiple values gets converted to in statement

I've got a linq statement like this:
this.DataContext.TableName.Where(r => r.Field =="REQUEST" || r.Field== "UPDATE")
The statement when converted to sql looks like this:
WHERE ([Extent1].[Field] IN (N'REQUEST',N'UPDATE'))
rather than:
WHERE Field = 'REQUEST' or Field = 'UPDATE'
The bottom example runs twice as fast as the top. Is anyone able to point me in the right direction in order to get the converted SQL to look like the below example.
I'm using C# ASP.Net MVC 5, EF6, but whatever I seem to try just gives the same results and uses the IN statement.
I use LINQPad4 with:
Customers.Where(x => x.Name == "Tom" || x.Name == "Dick").Dump()
Generate:
-- Region Parameters
DECLARE #p0 NVarChar(1000) = 'Tom'
DECLARE #p1 NVarChar(1000) = 'Dick'
-- EndRegion
SELECT [t0].[ID], [t0].[Name]
FROM [Customer] AS [t0]
WHERE ([t0].[Name] = #p0) OR ([t0].[Name] = #p1)
IN vs OR is discussed here: IN vs OR in the SQL WHERE Clause
Hope this helps.
In this case if the concern is performance well, you have a big long list of articles to read to have your own opinion. For once maybe you have to concern about if your column needs and index and if your statistics are updated.
If your values are request and update as a string, evaluate if you need a related table to hold this values and have a related id in your table with an integer value. This int column can have an index and this will provide better performance and a smaller footprint in your data and index size.
LINQ will try to provide the best execution based in your database structure. So for instance if you provide a good design in your database you dont have to bother in most cases of what is the result sql from the linq query. (both in big querys and with big databases always make this check, and check the execution plan)

Search with multiple parameters in SQL Server

This question is related to both c# and SQL Server.
I want to figure out how to do a custom search.
I have three tables
Customer
CusId, Name, Telephone
Employee
EmpId, Name, Job
Appointment
AppId, Date, EmpId, CusId
My C# form has three checkboxes. I want to find the data according to those checked values.
Ex: when customer,employee,app check boxes have selected, I want to find data on depending on all those three values.
When only two or one is selected I want to search depending on those selection. Here there will be total 6 combinations.
How to write a query to get correct result when I pass those values as parameters to a stored procedure.
Do I have to write 6 stored procedures to get the result?
Are there any methods to do this easily?
Please help me to fix this matter. Thanks in advance.
With a query such as the below (would suggest in a stored proc):
-- Parameters to a SQL sproc
DECLARE #CustID INT, #EmpID INT, #AppointmentID INT
-- Set Parameters here for testing
SELECT *
FROM Appointment as A
INNER JOIN Employee as E
ON E.EmpID = A.EmpId
INNER JOIN Customer as C
ON C.CusID = A.CusID
WHERE (#CustID IS NULL OR C.CUsID = #CustID)
AND (#EmpID IS NULL OR E.EmpID = #EmpID)
AND (#AppointmentID IS NULL OR A.AppID = #AppointmentID)
You then need to pass in the parameters appropriately, either an ID if selected, or null if not filtering on one item.

How to create a SQL function using C#

I want to define a function to get "order number" parameter and return "last name" of the customer.these two parameters are in different tables i am using inner join.
the errors i am getting are: Incorrect syntax near (.
Incorrect syntax near the keyword RETURN.
Must declare the scalar variable #FindingLastName.
my code:
com.CommandText = "Create function Sales.FindingLastName (#OrderNumber varchar(10)) Returns nvarchar(50) As Begin Declare #FindingLastName(Select LastName from Sales.OrderDetails INNER JOIN Sales.Customers RETURN #FindingLastName END GO)";
com.Connection = con;
MessageBox.Show(com.ExecuteNonQuery().ToString());
You have lot of things missed in your SQL Syntax. Have a look at below SQL Syntax Change the ColumnNameto actual columnnames in join statement. May be you want to modify the select condition accordingly by adding WHERE clause.
Create function Sales.FindingLastName
(#OrderNumber varchar(10))
Returns nvarchar(50) As
Begin
Declare #FindingLastName varchar(50) =
(Select LastName from Sales.OrderDetails O INNER JOIN Sales.Customers C ON C.ColumnName =O.ColumnName)
RETURN #FindingLastName
END
One more question You can run this only once. Once the function is created, you can't create same function again. So i doubt are you sure you want to do it this way via c#?? !!
For more details on User Defined function in SQL Refer THIS LINK
What stands out there is the GO. Firstly, GO is not SQL. It only exists to tools like SSMS (etc), which use it to split a long script into separate commands. Inside an actual command, it is simply a syntax error. Consequently, GO can never appear inside nesting such as parenthesis, or a BEGIN / END block.
Basically, remove the GO.
However, it is pretty rare to use DDL inside ADO.NET, unless part of an automated schema migration tool etc.
I'm no hero with SQL, but i think this should work
Create function Sales.FindingLastName (#OrderNumber varchar(10))
RETURNS nvarchar(50)
As
RETURN
(
Select LastName
from Sales.OrderDetails INNER JOIN Sales.Customers
);

SQL to Linq Left Join with 'AND' in the SQL Join Statement

I have a SQL query that works perfectly that I need to convert to Linq. I need to return all the records one table and join it to a second table. I need to return all of the results from the first table joined with results from the second table where the value of a specific field in the second table equals a variable value (75 in the example below) or returns null.
So the total number of rows in the result should be the total number of rows from table1. The part of the row from the join from table2 should either show values from table2 where a record existed with a value of 75 or null where the record doesn't exist.
EDIT: I should mention that t1.field1 is an int and t2.field1 is a nullable int.
I tried multiple linq statements, grouping joins, asking coworkers, and googling til my eyes bleed. I'm stuck. I realize my question wording may not be clear, and I apologize in advance if it isn't.
Thanks in advance.
Chris
The SQL Query:
SELECT *
FROM table1 AS t1 LEFT OUTER JOIN
table2 AS t2 ON t1.field1 = t2.field1 AND t2.field2 = 75
Use DefaultIfEmpty - see LINQ - Left Join, Group By, and Count and http://msdn.microsoft.com/en-us/library/bb397895.aspx for samples of how to achieve this
An answer that works but isn't as elegant as I'd expected:
var q = from item1 in table1
join item2 in table2 on new { Field1=(int?)item1.Field1, Field2=75 }
equals new { item2.Field1, item2.Field2 } into gj
from subItem2 in gj.DefaultIfEmpty()
select new { F1= item1.Field1, F2 = ( subItem2 == null ? string.Empty : subItem2.Field2.ToString()) };
I couldn't see where to fit the where clause in (Field2 = 75) so instead went with the composite key between the tables to achieve the same effect.
Second bit of ugliness is the cast to the nullable int because or Field1 in the composite key so it can be equated to the corresponding field on table 2.
Obviously you return in the anonymous type whatever values you're interested in. Note you can't return null which is why I showed a string representation of Field2 just so you can see what is returned from the second table.
Thank you for your responses. Because I need some nulls, but only the ones where the specific id is null and not all values that are null, none of the solutions above will work. It seems that to do this in linq will be very tough if it is possible.
So, in the interest of time, I decided to take the SQL query that worked and turn it into a stored procedure and function import. I feel like that's probably not the right way to do it, but time is always a factor.
Thanks again for your responses.

Why linq fail to get temp table values

work on C# vs2008. I have a stored procedure ,This procedure holds temp table ,i need to get the temp table values.My sql query is bellow:
create procedure [dbo].[TestProcedure]
as
SELECT * INTO #temp1 from(select * from DischargePort) as b
select * from #temp1
drop table #temp1
My above query has a temp table named #temp1.After run this query in sql-server-management i get result ,but when i try to execute this procedure in linq ,I get no result.My linq syntax is bellow:
var r = ProviderName.TestProcedure();
Can anybody tell me why this problem arise,How to overcome this problem.Hope any body not say that linq can not handled the temp table or this kind of word.if have any query plz ask .Thanks in advance.
I don't think this is anything to do with the temporary table, but rather that Linq does not know what output is to expected.
With Dotnet Framework 4, this is easy, as you can do something like
(from r in ProviderName.TestProcedure().AsDynamic()
select new { r.Id, r.Description}).ToList()
(assumes Id and description are fields in DischargePort)
Otherwise, you need to do something in your designer to tell Linq what your procedure outputs. I have never had to do this, but perhaps this article will help.
When I think about it, in this particular case, you should be able to do something like
var results = (from r in ExecuteQuery<DischargePort>("exec TestProcedure")
select r ).ToList();
i would start by downloading linqpad to see the sql that linq is emitting, this may provide some clues. you could also use the sql profiler tool to see what query is being run.

Categories

Resources