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 5 months ago. Improve this question I have a dataset where a column contains 1 and 0 in a single cell which reference security …
Tag: sql-server
Include zero counts when grouping by multiple columns and setting filters
I have a table (tbl) containing category (2 categories), impact (3 impacts), company name and date for example: I want to get the count of records for each category and impact and be able to add rows with zero count and also be able to filter by company and date. In the example result set below, the count res…
How to Get Count(*) and table data based on where condition in single query
I’ve a stored procedure like this: In the above ex I’ll be getting the table data and the Count of total rows based on where in two queries. This is working fine, but in my actual case the select query is large and calling it twice, once for table and second for count like above is costing time. J…
How to use data from multiple similar columns as rows in SQL
I have a table as below: ItemName 1mth_presale_cost 2mth_presale_cost 1mth_postsale_cost 2mth_postsale_cost 1000 10.1 12.1 12.5 15.1 1001 20.2 15.2 25.2 17.3 I want the result to be like below table: ItemName 1mth_cost 2mth_cost 1000 10.1 12.1 1000 12.5 15.1 1001 20.2 15.2 1001 25.2 17.3 I don’t want to…
Rank Date based on today’s date
I have a simple date dimension, I’m attempting to write a Window function (without much luck..) to net the following result. where 1 is today and 0 is yesterday. What can I try next? I’m unsure what to search for. Answer Assuming the dates are continuous, we don’t even need analytic function…
How to decompose the tables for optimal querying
Suppose I have three tables Actor, Movie, Producer with following attributes Actor(Id,ActorName,Bio) Producer(Id,ProducerName,Bio) Movie(Id,Name,Description,ActorName,ProducerName) There can be many actors in a movie and actor can act many movies, same goes with producer. How to effectively decompose/create n…
Copy all values from source table to destination table dynamically in SQL Server. If there is some value in destination table then delete and insert
I have created 2 tables and populated the values in my source table. Now, I want to populate these values into the destination table dynamically and also check if there is any value in the destination table then delete all those values and insert them from the source. This is my approach which is not executin…
Date in where clause returning 0 rows
I am trying to get data as follows WHERE DateCommande = ‘2018-09-05’ but it doesn’t work on my computer. Returns rows, including rows that show 2018-09-05 as the datetime value in a datetime column. However, on SSMS on my computer, if I add a WHERE clause, the query returns 0 rows: Answer yy…
Is it possible you instatiate a variable in SQL server using a select statement with another variable
I am attempting to DECLARE a variable then SET it’s value using a secondary variable which has already had it’s value set. I have tried: To no avail. The result of the final SELECT statement is null. The following works: but I have several tables linked via dependencies and to delete an entry I ne…
Best way to update the same column without using x update statements (if possible)?
I have a few questions regarding the following scenario – Consider this table (db<>fiddle here): I want to replace each class subject with a number code. For example: If I try to do this in one update statement, it won’t let me do that. The only thing I could think of was four separate state…