tsql

TSQL, help with small query about user's age

不问归期 提交于 2020-01-24 12:15:54
问题 I have a table with registered users, in which i save year as varchar values simply because i take just an year. I want to create pie chart with ages to show which users are more likely to register. Query below gives me count of user ages which appear more than 5 times in Table to avoid small results. While these small results, below "having ount(userID)>5" i want to appear as others. What should i add to this query or possibly to re-design it. I can create silly solutions like to take all

Checking a range of dates in SQL

孤者浪人 提交于 2020-01-24 10:43:30
问题 Following on from a question I put on yesterday, I need to return a range of "available" dates for a laptop rollout "booking system". I want to populate a table of possible available dates a user can book a slot on by checking for each date the total possible number of slots, and subtracting the number of slots already booked. The logic is as follows: A technician can build 3 laptops per day. On any day there may be 1, 2 or 3 technicians available. A table holds the bookings made I don't want

Calculate Work Time from Shift and Break Table

依然范特西╮ 提交于 2020-01-24 06:41:30
问题 I am using SQL Server 2008. I have two table Shift and Break with following data: The shifts will be 7 days week. Data in SHIFT TABLE ID Desc Start_Time End_Time 1 1st 07:20:00 15:20:00 2 2nd 15:20:00 23:20:00 3 3rd 23:20:00 07:20:00 Data in BREAK TABLE ID Desc Start_Time End_Time 1 1st Shift - 1st break 09:10:00 09:25:00 2 1st Shift - Lunch 11:30:00 12:05:00 3 1st Shift - 2nd break 13:30:00 13:45:00 4 2nd Shift - 1st break 17:10:00 17:25:00 5 2nd Shift - Lunch 19:30:00 20:05:00 6 2nd Shift -

SQL Calculate Days between two dates in one table

假装没事ソ 提交于 2020-01-24 05:54:05
问题 I have a table dbo.Trans which contains an id called bd_id (varchar) and transfer_date (Datetime), also an identifier member_id pk is trns_id and is sequential Duplicates of bd_id and member_id exist in the table. transfer_date |bd_id| member_id | trns_id 2008-01-01 00:00:00 | 432 | 111 | 1 2008-01-03 00:00:00 | 123 | 111 | 2 2008-01-08 00:00:00 | 128 | 111 | 3 2008-02-04 00:00:00 | 123 | 432 | 4 ....... For each member_id, I want to get the amount of days between dates and for each bd_id E.G

SQL Geography data type column in GROUP BY clause

寵の児 提交于 2020-01-24 05:49:39
问题 I'm using SQL Server and creating a script which will get the workers geographical location out of my database. The script is below. SELECT w.display_name, w.geo_location FROM jobs j WITH(NOLOCK) INNER JOIN workers w WITH(NOLOCK) ON w.worker_id = j.worker_id WHERE ..... The problem is that I want to add GROUP BY w.display_name, w.geo_location to the script as there are duplicate records being shown. Added a column with the data type geography to a group by clause causes an error to be thrown.

How to delete records in SQL 2005 keeping transaction logs in check

淺唱寂寞╮ 提交于 2020-01-24 05:35:25
问题 I am running the following stored procedure to delete large number of records. I understand that the DELETE statement writes to the transaction log and deleting many rows will make the log grow. I have looked into other options of creating tables and inserting records to keep and then Truncating the source, this method will not work for me. How can I make my stored procedure below more efficient while making sure that I keep the transaction log from growing unnecessarily? CREATE PROCEDURE

T-SQL split on delimiter

家住魔仙堡 提交于 2020-01-24 04:25:52
问题 I am working with an employee hierarchy string that is in the format of the following. These number represent employeeID numbers and how the are structured within the company, thus being able to follow the chain of management. 123|456|789|012|345|320 I am trying to take this string of data and turn it into a temp table so I can work with each of the ID's as their own value. I tried making a function to split the string: ALTER FUNCTION [dbo].[SplitString] (@String NVARCHAR(4000), @Delimiter

Readonly connections with ADO.NET, SQLite and TSQL

ぃ、小莉子 提交于 2020-01-24 04:12:09
问题 My code reads via one connection and writes via another. I dont want to accidentally write with the read connection. How can i make the connection readonly? I am using SQLite ATM and will convert sections of code to tsql when the prototype is over. 回答1: You can add Read Only=True to the read-only connection. Data Source=filename;Read Only=True; 来源: https://stackoverflow.com/questions/2004010/readonly-connections-with-ado-net-sqlite-and-tsql

SQL Server: Put stored procedure result set into a table variable without specifying its schema

江枫思渺然 提交于 2020-01-24 02:12:28
问题 I have a stored procedure with many parameters. I want to insert (or maybe select) like this: INSERT INTO @TEMP_TABLE EXECUTE STORED_PROCEDURE without defining the schema of @TEMP_TABLE . 回答1: You cannot do this with a @tablevariable . The work around in the link posted by Joe uses SELECT ... INTO . This is not currently supported by table variables (and won't ever be from the response to this connect item) as the schema for table variables needs to be known at compile time. 回答2: for example:

Inserting records into DB table in multithreaded environment

[亡魂溺海] 提交于 2020-01-24 01:49:33
问题 I have this TSQL code that checks if the 'sadsadsad' exists and if not inserts it into the table. if not exists(select id from [ua_subset_composite] where ua = 'sadsadsad') begin insert into [ua_subset_composite] select 'sadsadsad',1,null,null,null,null end My concern is that in production where there will be multiple threads running concurrently, a situation may occur that a record will slip thru between the not exists select and the insert. I don't want to add a unique constraint on the