I’ve been playing around with the PIVOT function for a while. I have a table that looks like this Every ClientID is repeated once for each month. Every month new rows are added to the table with a new period associated to the row. It’s an incrementing table. What I need to do is to PIVOT the table, so that
Tag: tsql
SQL: Return Aggregated Results based on some conditions
I need help with making a conclusion in SQL Server based on some column’s values, like status aggregation sort of. As an example, below is a table containing server tasks and their status. If I want to return the aggregated status of each server here are the rules: if all the server tasks are at status ‘SUBMITTED’, then the aggregated
The difference between OUT and OUTPUT
In an application that uses SQL Server I came across a code that uses both OUT and OUTPUT parameters. What is the difference between OUT and OUTPUT parameters. Answer As noted in the comments, the 2 are synonyms. From the documentation the syntax for the clause is listed as below: So this denotes that either OUT or OUTPUT (or READONLY)
Return value from dynamic linked server stored procedure
I want to return a value from a dynamic linked server stored procedure. On the local server, how can I retrieve the value of @DocNumOut? I have trial many combination using EXEC, openquery, EXEC sp_executesql, but none can return the value into my local variable, @DocNumberOUT. Answer Use OUTPUT parameter on your Stored Procedure and don’t use the return value.
Divide the results of two select queries
I have two SQL count queries that on their own work fine, they are: Now how to do I add to the query to get the percentage of 26/130?? I have a new query to go along with how to get percentages. Here it is: this brings back results below: If I remove the Distinct from my query there are
sql Tables manipulation
Hi everyone, i have the above table ‘given table’ i would like to create a new table with columns ‘product’, ‘paid’ , ‘branchA’ and ‘branchB’ as shown in the ‘desired output table’ with the unique identifier column ‘id’. This must be a simple sql task, but here i am. Any constructive suggestions or code samples are welcome. thx in advance
How to count from first date till date?
I have a table like below in SQL server Date Store ID Sales Amount 01-01-2021 1001 1000$ 01-01-2021 1002 1000$ 01-01-2021 1003 0$ 02-01-2021 1001 1000$ 02-01-2021 1002 1000$ 02-01-2021 1003 1000$ 03-01-2021 1001 1000$ 03-01-2021 1002 1000$ 03-01-2021 1003 1000$ how to calculate count of previous days if Sales Amount > 0, using TSQL? (same as below column) Date
Divide by zero exception in SQL Server
I have an update statement which has division operation and one row throws exception Divide by zero exception and I see that none of the rows got updated . How to continue on exception so that rows which doesn’t encounter 0 gets updated and row which has 0 fails? Its just a normal update statement without use of any transaction.
Distinct few Columns and check condition on the column not included in distinct columns
Table “Module” has these columns How to select distinct columns of ModuleGroupDisplayAs and ModuleGroup And After that, Add column on checking these distinct columns has particular UserID and CompanyID. That means I want to check condition that distinct columns has this particular userID and CompanyID. The expected result would be as following. Answer It looks like you just need to
Show a string that contains two numbers in SQL [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed last year. Improve this question I’m studying SQL just recently and I have encountered a problem. I want to show the Address that contains only 2 digit numbers. Example data below: