I need to find how many true bit exists in my binary value. example: Answer Result:
Tag: tsql
How to calculate running total (month to date) in SQL Server 2008
I’m trying to calculate a month-to-date total using SQL Server 2008. I’m trying to generate a month-to-date count at the level of activities and representatives. Here are the results I want to generate: From the following tables: ACTIVITIES_FACT table LU_TIME table I’m not sure how to do this: I keep running into problems with multiple-counting or aggregations not being allowed
Transform and group row data into columns
I’m looking for a way to combine different rows into a single row with columns. Let’s take this example table: Desired output: Please note that Data centers are not necessarily these three, they can be N different data centers that I will not know in advance. Answer With a known set of sourceDataCenter values, you can just use a simple
Get number of employees who worked in more than one department with SQL query
I’m trying to figure out a query which show the number (amount) of employees who worked in more than 1 departments. Here the table name and fields: Employee(id_employee, employee_name, salary) Department(id_dept, dept_name, budget) Department_Employee(id_employee, id_dept, workhours_percentage) Suppose the content of Department_Employee table is With a right query, the result should be 2 (employees), because there are 2 employees who work
SQL count of distinct records against multiple criteria
I have a table in SQL SERVER “usage” that consists of 4 columns Now I need to query the table so that I can find how many users used an app under a license within a time frame. Bearing in mind that even if user 1 used the app on say january 1st and 2nd they would only count as
SQL Split Function and Ordering Issue?
I have the following Split function, When I write, This will give me, I need to maintain the order. Answer A simpler function: Sample usage: Or to return orders from a table ordered by input:
Unpivot with column name
I have a table StudentMarks with columns Name, Maths, Science, English. Data is like I want to get it arranged like the following: With unpivot I am able to get Name, Marks properly, but not able to get the column name in the source table to the Subject column in the desired result set. How can I achieve this? I
Combining (concatenating) date and time into a datetime
Using SQL Server 2008, this query works great: Gives me two columns like this: I’m trying to combine them into a single datetime using the plus sign, like this: I’ve looked on about ten web sites, including answers on this site (like this one), and they all seem to agree that the plus sign should work but I get the
how to convert date to a format `mm/dd/yyyy`
I’m having a sql table with date column named CREATED_TS which holds the dates in different format eg. as shown below Now I want to convert these to format mmddyyyy before as i am comparing the dates in WHERE clause of my SELECT query. I tried using but got the result as, I need the result as eg. 02/20/2012 in
sql server single row multiple columns into one column
I have table like this From this I need to create a temp table or table like this: Is there a way I can do this in SQL Server? Answer Check this Fiddle If you do want NULL values in the rest, you may try the following: This is the new Fiddle And here is the code: