sql-server-2008-r2

How can I group student scores into quintile using SQL Server 2008

雨燕双飞 提交于 2019-12-02 14:11:32
Can anyone help me to group student scores into quintile? I think there is a feature in SQL Server 2012, but still we haven t upgraded to it as we are using 2008R2. I tried Ntile(5)` but it is not generating the desired result. I need below Quintile column Student Score Quintile ------------------------ Student1 20 1 Student2 20 1 Student3 30 2 Student4 30 2 Student5 40 2 Student6 40 2 Student7 50 3 Student8 50 3 Student9 60 3 Student10 70 4 Student11 70 4 Student12 80 4 Student13 80 4 Student14 90 5 You must have been doing something wrong when using NTILE(5) - that IS the function to use!

Auto Increment SQL Value

做~自己de王妃 提交于 2019-12-02 12:53:55
In the infinte wisdom of the global DBA at a firm I am working at right now he has created a table that takes an int as an ID field, but does not auto increment the number. I am passing up a table valued parameter from .Net because it has roughly 100 or more rows of data that are being passed up at any one time and I dont want to kill the application, hammer the network or the SQL Server. So this is my stored procedure CREATE PROCEDURE sp_Insert_Supporting_Error_Info (@tvp [dbo].udt_CEQZW READONLY) as begin INSERT INTO CEQZW ERROR_VAR_ID, ERROR_ID, ERROR_VAR_TYPE_CD, ERROR_VAR_VALUE) SELECT

PIVOT on Multiple Columns

╄→гoц情女王★ 提交于 2019-12-02 12:40:15
问题 I have data like this: Product Group Product Level Quatity Sold Trend ============================================================== Group 1 L1 10 up Group 1 L2 20 up Group 1 L3 30 down Group 2 L1 20 up Group 2 L2 40 up Group 2 L3 60 down Group 2 L4 80 down I need to get the data in this format: Product Group L1 L1Trend L2 L2Trend L3 L3Trend L4 L4Trend ====================================================================================== Group 1 10 up 20 up 30 down Group 2 20 up 40 up 60 down

Sending Email in SQL Server 2008 R2

浪子不回头ぞ 提交于 2019-12-02 12:25:14
I am tasked with a feature to send e-mail reminders to employees in my company if they haven't completed an attestation form via an intranet Web application. I was thinking of writing a stored procedure that gets called in a nightly database job (SQL Server 2008 R2). The proc would select employee e-mail address values and loop through them via cursor, so that for each e-mail found an e-mail is sent using msdb.dbo.sp_send_dbmail. The concern I have is that this is for a large company and tens of thousands of e-mail could go out nightly. Is there a way to mitigate performance concerns when

How to eliminate NULL fields in TSQL

一世执手 提交于 2019-12-02 11:55:56
I am developing a TSQL query for SQL Server 2008 R2. I am trying to develop this query to identify one record / client. Because some of these values are NULL, I am currently doing LEFT JOINS on most of the tables. But the problem with the LEFT JOINs is that now I get > 1 record for some clients. But if I change this to INNER JOINs then some clients are excluded entirely because they have NULL values for these columns. How do I limit the query result to just one record / client regardless of NULL values? And if there are non-NULL values then I want it to choose the record with non-NULL values.

Dynamic case statement using SQL Server 2008 R2

回眸只為那壹抹淺笑 提交于 2019-12-02 11:48:08
I have the following case statement as shown below: Example: I have the case statement : case cola when cola between '2001-01-01' and '2001-01-05' then 'G1' when cola between '2001-01-10' and '2001-01-15' then 'G2' when cola between '2001-01-20' and '2001-01-25' then 'G3' when cola between '2001-02-01' and '2001-02-05' then 'G4' when cola between '2001-02-10' and '2001-02-15' then 'G5' else '' end Note : Now I want to create dynamic case statement because of the values dates and name passing as a parameter and it may change. Declare @dates varchar(max) = '2001-01-01to2001-01-05,2001-01

Stored procedure without cursors

我只是一个虾纸丫 提交于 2019-12-02 11:47:10
How can I write the following sp without the cursor?. More over its not giving me the desired output. I didn't write this, I am trying to interpret what is wrong with this. ALTER PROCEDURE [dbo].[AccreditationExpiryCheck] AS BEGIN SET NOCOUNT ON; declare @taskTypeId int = 19 -- Accreditations, automated declare @firstActionTypeId int = 23 -- Accreditation expiring declare @nextActionTypeId int = 3 -- Call company declare @companyId int declare @accreditationId int declare @comment nvarchar(max) = N' accreditation for this company has expired.' -- find all companies and accreditations expiring

Sum of data in varchar column

不羁的心 提交于 2019-12-02 11:02:55
问题 i have to sum element in a column(SCENARIO1) that is varchar and contain data like (1,920, 270.00, 0, NULL) but when i try to convert data into int or decimal i get this error : "he command is wrong when converting value "4582014,00" to int type" here is my request : select sum( convert( int, SCENARIO1) ) from Mnt_Scenario_Exercice where code_pere='00000000' any help please 回答1: try this select sum(cast(replace(SCENARIO1, ',', '.') as decimal(29, 10))) from Mnt_Scenario_Exercice where code

Hash encrypting password when inserting into database

风格不统一 提交于 2019-12-02 10:51:05
I'm doing an application for school and I'm in need of help in encrypting passwords when inserting them into my users database.I'm programming in c# programming language and i'm using MS server 2008 R2 for manipulating my database. I'm thinking of doing a HASH encryption and I would love if someone helped me out. Here's my code for inserting data into database : using (SqlConnection con = new SqlConnection("Data Source=HRC0;Initial Catalog=users;Integrated Security=True")) //MLHIDE using (SqlCommand sc = new SqlCommand("if NOT exists (select * from users where UserName = @username) insert into

select with count for years

折月煮酒 提交于 2019-12-02 10:49:58
问题 I have pickup table which looks like this create table Pickup ( PickupID int IDENTITY, ClientID int , PickupDate date , PickupProxy varchar (200) , PickupHispanic bit default 0, EthnCode varchar(5) , CategCode varchar (2) , AgencyID int, Primary Key (PickupID), ); and it containe pickups for clients I need to create report based on this table which should looks like this I know i need to use CASE but really do not know how to put years and calculate average pickups for each year. and how to