Skip to content

Tag: sql-server

How to minimize my big query for showing counts using SQL

I am taking counts from CTE but common is Status(working,pening) and Divisions but my query is becoming big because left join is same but just status and division are different. I have wrote 10 left join count but by passing status and divisions. Below is my whole sql query Is there anything which we can minimize query by using group

SQL get row count from every table tenantwise

I am working on a multitenant application. Most of the tables contain a column tenant_id. I want the number of rows in all tables for a particular tenant_id. Example, Tables:- Sample result to get number of rows in every table having tenantid = 64 TableName Count Employee 2 Student 3 Teacher 1 How do I loop through all the tables

Two foreign keys referring to one primary key on SQL SERVER query select items

I’d to create a database that records transactions between two users. A user can transfer points (think of it as money) to another user. user table looks like: And the transaction table should look like: just i needed this result: Could someone give hints to provide SQL Server code? Select from_user, to_user, name, transfer_amount from transaction iner join users on

What should I do to join two columns with different name but has the same data type, so that I can get something I need from one of the table?

I have two table, [table a] and [table b]. So basically, I need work_week from [table a] therefore I want to join the columns together, shift_begin_datetime from [table a] and shift_start_datetime from [table b] as both of them have the same data type but they have different name. I want both of the column to be renamed as shift_start_dt and

Pass Parameters in Pivot SQL

I had this query below and got an error with parameter @emp_group during execution. Need your suggestion on how to pass the parameter. The query will run when removing the parameter but need to filter the records. thanks in advance. Answer You can can do the same thing with @emp_group like you did with @cols. Extract them vom the text

Check in T-SQL whether certain value sets exist in a related table

I have following tables: User – userId, userName, … Settings – settingId, userId, settingKey, settingValue for example for userId = 123, I might have: Then for example how can I query for all users that have settingValue of “0” corresponding to settingKey of “allowClient” and settingValue of “1” corresponding to settingKey of “allowAccess”? Sometimes the settingKey and settingValue that I’m