I want to group by the records and that particular group if the condition matched then set final result like below For each group, I want to check the following condition Answer You can use conditional logic: Here is a db<>fiddle.
Tag: sql-server-2012
Compare 3 SQL Server tables and once matched based on some attribute put the result on one table when not matched put the result in another
I have the simple requirement, there are the 3 SQL Server tables like this: Table 1; columns Table 2; columns Table 3; columns I wish to join (inner joins) based on Name, DOB, Gender & PostCode across all the tables, if the record matches I would like to put the result in one table and the remaining records I wish
How to use ‘Count’, based on a particular column in SQL
I have a query and which give the count greater than 1, but what I expect is I need the result to be based on particular column(Rollno.) How to achieve it. Table Studies The below query gives me Count but not as expected The query gave me the below result But the expected result is As you can see the
If I delete a record from a table it is deleted at many other places
I am working on a database built by the previous team. I have to delete certain records from a table (Example shown below). While doing this process, some records from the other tables with the same id is getting deleted. Could someone help how to overcome this problem? Answer In SQL Server, there is statement called ON DELETE CASCADE which
How to select data where more than one column is populated
I have a sample table, called [SAMPLES] which I’ve added multiple columns to the end for different types of lab analysis results, however for the majority of samples only one analysis has been completed. I want to be able select only the samples where more than one analysis has been completed on the same sample. The table structure is something
How do select only swipe-in users from the table which has all swipe records
I have a SQL Server table task_swipe_rec which has only 6 columns. I want to know how many people swipe in right now for the assigned task and the list of minutes they spent on those tasks till now. I am expecting to get the list of tasks that are not swiped out (not completed) and number of minutes spent
List No of minutes worked by an employee using self join
I have a SQL Server table emp_swipe_rec which has only 6 columns. I want to know how much time (in minutes) both employees A and B worked for today (last 24 hours). Employee A already swiped out for the day. Employee B is still working. We don’t store two different columns for a swipe in/swipe out. We use swipe_type (I
Concatenate from rows in SQL server
I want to concatenate from multiple rows Table: |id |Attribute |Value | |——–|————|———| |101 |Manager |Rudolf | |101 |Account |456 | |101 |Code |B | |102 |Manager |Anna | |102 |Cardno |123 | |102 |Code |B | |102 |Code |C | The result I’m looking for is: |id |Manager|Account|Cardno|Code | |——–|——-|——-|——|———-| |101 |Rudolf |456 | |B | |102 |Anna |
Merge values in a column (B) based on common values in column (A) in SQL table
Question: Using SQL, how would you Merge values in a column (B) based on common values in column (A)? Table Structure: I have a SQL table (shown below), where Column A has ID’s and Column B contains Text related to ID’s and Column C contains Rank Order (the order in which text should be should be sorted). ID TEXT RANK_ORDER
IF when zero result from another Stored Procedure
I have a Stored Procedure which contains an Insert statement on it. From the above Stored Procedure, I want to have a condition from result of another Stored Procedure. With the following action: So, if cBranch is zero. Then execute Sp1 else do nothing. Need advice please. Really appreciated. Answer Try This: