I want to copy column state_name from table state into table district. here is my query. This code is working on mysql but not in SQL Server This is the state table This is the district table Answer In SQL Server, the corresponding syntax might be: This is more commonly written using an explicit JOIN: However, you probably shouldn’t be
Tag: sql
MYSQL moving data after distinct
I have data I am pulling from one table to another table but I have a Primary key on the ID column. how do I pull the rest of the data after a Select distinct to make sure I don’t have duplicates in the primary? where I have gotten the distinct store_id from it and inserted into the table but
How do I merge two SELECT queries with different WHERE clauses
I’m trying to write query on two different selects with different WHERE clause and using GROUP BY. I browsed for examples but mine is different since I have multiple fields for SELECT. Here is example data: These are the queries I would like to merge: I tried 2 methods as under: Both give Error: Please guide Answer seems like you
update value in first row of partition. sqlanywhere17
given a Table T id FKey col_value 1 1 NULL 2 1 NULL 3 2 NULL 4 3 NULL 5 4 NULL 6 4 NULL 7 4 NULL 8 5 NULL 9 5 NULL i want to Update each row with the highest id with value 1 for each given ForeignKey So my result looks like this: id FKey col_value
Why `not deferrable` constraint is deferred when using `with`?
When this script is run in PostgreSQL, why does Statement 1 and 2 work and only statement 3 gives an error even when both the foreign key constraint are explicitly not deferrable? Answer According to the docs non-deferrable unique constraints are checked for each row, contrary to the standards specification that they are checked only at the end of a
SQL query to retrieve entries if userId matches table1’s employee_id or table2’s contributor_id
I’m trying to retrieve all kpi_id, kpi_name, employee_id, contributor_id from table1 and table2 if the provided userId matches employee_id from table1 or contributor_id from table2. Sample table1: id kpi_name employee_id 1 kpi1 5 2 kpi2 6 3 kpi3 9 Sample table2: id kpi_id contributor_id 1 1 9 1 3 5 1 1 6 Now, if a given userId is 5
SQL to find records with last 3 same status
I have the following tasks table: I need to run a query that returns only the types which their last 3 tasks finished with status “FAILED”. How can I do this? Should I use a window function? Thanks Answer You can use aggregation and filtering after using row_number() to get the last three rows: Actually, a slightly simpler method is:
Is there a way to achieve below using pivot or something else in T-SQL?
I’m trying to achieve below using pivot in T-SQL: brand a revenue brand b revenue` brand c revenue branch 1 20,000 branch 6 9000 branch 9 11000 branch 3 15000 branch 5 2000 branch 6 8000 branch 9 10000 branch 10 1500 branch 4 5000 However I’m getting this result: brand a brand b brand c branch 20000 null null
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
SQL script within subquery (Big Query)?
Is there a way to have SQL scripts within a subquery ? For example: In my case it is not possible to move the variable declaration outside of the subquery. Thanks ! Answer For this particular example, you could use a CTE to define the parameter values: