SQL get row count from every table tenantwise - sql

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:-
create table Employee(id integer, name varchar(100), tenantid integer);
insert into Employee(id, name, tenantid) values(1, "Stephan", 64);
insert into Employee(id, name, tenantid) values(2, "Alan", 64);
insert into Employee(id, name, tenantid) values(3, "Charles", 46);
create table Student(id integer, name varchar(100), standard integer, tenantid integer);
insert into Student(id, name, standard, tenantid) values(1, "James", 2, 64);
insert into Student(id, name, standard, tenantid) values(2, "Rony", 4, 64);
insert into Student(id, name, standard, tenantid) values(3, "Steve",6, 64);
create table Teacher(id integer, name varchar(100), subject varchar(100), tenantid integer);
insert into Teacher(id, name, subject, tenantid) values(1, "Alvin", "Science", 46);
insert into Teacher(id, name, subject, tenantid) values(2, "Neil", "Geography", 64);
insert into Teacher(id, name, subject, tenantid) values(3, "Amy", "Mathematics", 46);`
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 and query where tenantid = <tenantid>

Use Dynamic Query. Form the query dynamically for each table and use string_agg() to concatenate it and execute the query using sp_executesql
declare #tables varchar(max),
#tenantid int,
#sql nvarchar(max)
select #tables = 'Employee,Student,Teacher',
#tenantid = 64;
select #sql = string_agg('select [TableName] = ''' + value + ''', [Count] = count(*) from ' + quotename(value) + ' where tenantid = #tenantid', ' union all' + char(13) )
from string_split(#tables, ',')
print #sql
exec sp_executesql #sql, N'#tenantid int', #tenantid
db<>fiddle demo

Use UNION ALL:
SELECT 'Employee' AS "TableName", COUNT(*) AS "Count" FROM Employee WHERE tenantid = 64
UNION ALL
SELECT 'Student' AS "TableName", COUNT(*) AS "Count" FROM Student WHERE tenantid = 64
UNION ALL
SELECT 'Teacher' AS "TableName", COUNT(*) AS "Count" FROM Teacher WHERE tenantid = 64
View example (I'm not sure about efficiency):
CREATE VIEW some_counters_view AS
SELECT 'Employee' AS "TableName", tenantid FROM Employee
UNION ALL
SELECT 'Student' AS "TableName", tenantid FROM Student
UNION ALL
SELECT 'Teacher' AS "TableName", tenantid FROM Teacher
;
SELECT "TableName", COUNT(*) AS "Count"
FROM some_counters_view
WHERE tenantid = 64
GROUP BY "TableName"

Related

How to compare String Variable with Integer

I have this table structure and and some sample data. I want return data for multiple ids via parameter. I have declared a parameter string and now I want to compare it with the column but it ain't allowing because ID is integer.
Can anybody give me any help here ?
CREATE TABLE EMPLOYEE
(
ID INT,
EMPLOYEE_NAME VARCHAR(50)
);
INSERT INTO EMPLOYEE VALUES (1, 'Isaac Frempong');
INSERT INTO EMPLOYEE VALUES (2, 'Eric Ortizz');
DECLARE #StrID VARCHAR(20) = '1, 2'
SELECT * FROM EMPLOYEE
WHERE ID = #StrID
SELECT * FROM EMPLOYEE
WHERE #StrID+',' LIKE '%'+cast(ID as varchar(20))+'%,'
Pretty bad performance as it will need to do a table scan but safe enough.
Generally though, your list of IDs should be a table variable you can do a proper JOIN or IN with
The easiest solution is to use dynamic SQL
DECLARE #sql VARCHAR(1000) = 'SELECT * FROM EMPLOYEE WHERE ID IN (' + #StrID + ')';
EXEC(#sql);
For SQL Server 2017+ you could use STRING_SPLIT a table-valued function that splits a string into rows of substrings
CREATE TABLE EMPLOYEE
(
ID INT,
EMPLOYEE_NAME VARCHAR(50)
);
INSERT INTO EMPLOYEE VALUES (1, 'Isaac Frempong');
INSERT INTO EMPLOYEE VALUES (2, 'Eric Ortizz');
DECLARE #StrID VARCHAR(20) = '1, 2'
SELECT * FROM EMPLOYEE
WHERE ID IN (SELECT value FROM STRING_SPLIT (#StrID,','))
Refer this working fiddle
Create a user defined table type and pass it as a parameter.
CREATE TYPE [UDT_INTIDS] AS TABLE(
[ID] [int] NOT NULL
)
GO
-- create a table value
DECLARE #IDs [UDT_INTIDS];
INSERT #IDs VALUES (1),(2);
-- search using table value.
SELECT e.*
FROM EMPLOYEE e
WHERE e.ID IN (SELECT p.ID FROM #IDs p);
-- or
SELECT e.*
FROM EMPLOYEE e
JOIN #IDs p ON e.ID = p.ID;
See https://learn.microsoft.com/en-us/sql/relational-databases/tables/use-table-valued-parameters-database-engine?view=sql-server-2017 for more details.
You can use the Cast in SQL-Server to cast it to the appropriate datatype. Source Here
WHERE CAST(ID AS VARCHAR(20)) = #StrID
Alternatively: You can use CONVERT function.
WHERE CONVERT(VARCHAR(20), ID) = #StrID

Select columns from a table based on the column names from another table

I want to select data from following tables for a Jasper report.
AA_COLUMN_NAMES
AA_PAYMENT_DETAILS
DDL + DML
CREATE TABLE SLPAYROLL_LIVE.dbo.AA_COLUMN_NAMES(
id int PRIMARY KEY NOT NULL,
name varchar(500) NOT NULL,
active bit
);
INSERT INTO SLPAYROLL_LIVE.dbo.AA_COLUMN_NAMES (id, name, active) VALUES (1, 'col_1', 1);
INSERT INTO SLPAYROLL_LIVE.dbo.AA_COLUMN_NAMES (id, name, active) VALUES (2, 'col_2', 1);
INSERT INTO SLPAYROLL_LIVE.dbo.AA_COLUMN_NAMES (id, name, active) VALUES (3, 'col_3', 1);
INSERT INTO SLPAYROLL_LIVE.dbo.AA_COLUMN_NAMES (id, name, active) VALUES (4, 'col_4', 0);
CREATE TABLE SLPAYROLL_LIVE.dbo.AA_PAYMENT_DETAILS(
id int PRIMARY KEY NOT NULL,
username varchar(500),
col_1 varchar(500),
col_2 varchar(500),
col_3 varchar(500),
col_4 varchar(500)
);
INSERT INTO SLPAYROLL_LIVE.dbo.AA_PAYMENT_DETAILS (id, username, col_1, col_2, col_3, col_4) VALUES (1, 'chathura', '500', '200', '300', '0');
INSERT INTO SLPAYROLL_LIVE.dbo.AA_PAYMENT_DETAILS (id, username, col_1, col_2, col_3, col_4) VALUES (2, 'gihan', '300', '100', '100', '0');
I want to select only active columns from AA_PAYMENT_DETAILS. The active column names can be get from the AA_COLUMN_NAMES table.
I have googled my question and found the following solution.
Select columns from one table based on the column names from another table
Since I want to use that query in a Jasper Report, above solution does not worked. Does anyone know a solution?
You have to use dynamic SQL to do this - here is one fairly simple way:
DECLARE #SQL nvarchar(4000) = '';
SELECT #SQL = #SQL +', '+ name
FROM dbo.AA_COLUMN_NAMES
WHERE active = 1
SET #SQL = 'SELECT id, username' + #SQL + ' FROM dbo.AA_PAYMENT_DETAILS'
EXEC(#SQL)

Increment an ID within an INSERT INTO statement in AS400

Is there a way to insert a new record to a table that has an non-unique id and set this id to the next number in the same SQL statement?
Something like
INSERT INTO T1 (id, fname, lname)
VALUES ([last id + 1], 'Dan', 'Thomson');
this probably works
INSERT INTO T1 (id, fname, lname)
VALUES (ifnull((select max(id) from T1),0) + 1, 'Dan', 'Thomson')
DECLARE #COUNTER INT;
SET #COUNTER = 1;
WHILE(#COUNTER <= XXX)
BEGIN
INSERT INTO T1 (id, fname, lname)
VALUES (#COUNTER, #FNAME , #LNAME);
SET #COUNTER = #COUNTER + 1;
END
Built-in function MAX :
INSERT INTO T1 (id, fname, lname)
SELECT ifnull(MAX(id)+ 1,1), 'Dan', 'Thomson' FROM T1
Insert and set value with max()+1 problems
SELECT MAX(col) +1 is not safe -- it does not ensure that you aren't inserting more than one customer with the same customer_id value, regardless if selecting from the same table or any others.
For performance, add an index:
CREATE INDEX T1_INDEX1 ON T1 (id) DESC
References:
MAX()
CREATE INDEX
Index Advisor, Show Statements
Accelerated analytics - faster aggregations using the IBM DB2 for i encoded vector index (EVI) technology
Unit Test
-- Create Table
SET SCHEMA QTEMP;
CREAT TABLE testtbl (
id integer not null default,
fname char(10) not null default,
lname char(10) not null default)
;
-- Initialize Table with Data
insert into
testtbl ( id , fname, lname)
values
( 1, 'fname1', 'lname_1'),
( 2, 'fname2', 'lname_2'),
( 2, 'fname3', 'lname_3'),
( 3, 'fname4', 'lname_4')
;
-- Test Insert Statement
INSERT INTO
testtbl ( id, fname, lname )
SELECT ifnull(MAX( id ) + 1, 1), 'Dan', 'Thomson' FROM testtbl;
--Confirm Expectation
select * from testtbl;
ID FNAME LNAME
-------------------------------------
1 fname1 lname_1
2 fname2 lname_2
3 fname4 lname_4
4 Dan Thomson
if you want insert all content of table with unique id start with max +1
INSERT INTO T1 (id, fname, lname)
select
rownumber() over() + ifnull((select max(T1.id) from T1), 0),
T2.zone1, T2.zone2
from T2

equivalent of if exists in isqlplus?

I'm new to sql and trying to find out if there is a way to say something like this in isqlplus? I'm trying to insert a row with values if a certain value exists on another table.
IF EXISTS (SELECT ID FROM SOMETABLE WHERE ID = 1)
BEGIN
INSERT INTO ANOTHERTABLE (NBR, FNAME, LNAME) VALUES ( 2, 'John', 'Smith');
END
Any help appreciated.
You can add WHERE EXISTS in your query
INSERT INTO ANOTHERTABLE (NBR, FNAME, LNAME)
SELECT 2, 'John', 'Smith'
FROM DUAL
WHERE EXISTS (SELECT ID FROM SOMETABLE WHERE ID = 1)

comparing two colums in sqlserver and returing the remaining data

I have two tables. First one is student table where he can select two optional courses and other table is current semester's optional courses list.
When ever the student selects a course, row is inserted with basic details such as roll number, inserted time, selected course and status as "1". When ever a selected course is de-selected the status is set as "0" for that row.
Suppose the student has select course id 1 and 2.
Now using this query
select SselectedCourse AS [text()] FROM Sample.dbo.Tbl_student_details where var_rollnumber = '020803009' and status = 1 order by var_courseselectedtime desc FOR XML PATH('')
This will give me the result as "12" where 1 is physics and 2 is social.
the second table holds the value from 1-9
For e.g course id
1 = physics
2 = social
3 = chemistry
4 = geography
5 = computer
6 = Spoken Hindi
7 = Spoken English
8 = B.EEE
9 = B.ECE
now the current student has selected 1 and 2. So on first column, i get "12" and second column i need to get "3456789"(remaining courses).
How to write a query for this?
This is not in single query but is simple.
DECLARE #STUDENT AS TABLE(ID INT, COURSEID INT)
DECLARE #SEM AS TABLE (COURSEID INT, COURSE VARCHAR(100))
INSERT INTO #STUDENT VALUES(1, 1)
INSERT INTO #STUDENT VALUES(1, 2)
INSERT INTO #SEM VALUES(1, 'physics')
INSERT INTO #SEM VALUES(2, 'social')
INSERT INTO #SEM VALUES(3, 'chemistry')
INSERT INTO #SEM VALUES(4, 'geography')
INSERT INTO #SEM VALUES(5, 'computer')
INSERT INTO #SEM VALUES(6, 'Spoken Hindi')
INSERT INTO #SEM VALUES(7, 'Spoken English')
INSERT INTO #SEM VALUES(8, 'B.EEE')
INSERT INTO #SEM VALUES(9, 'B.ECE')
DECLARE #COURSEIDS_STUDENT VARCHAR(100), #COURSEIDS_SEM VARCHAR(100)
SELECT #COURSEIDS_STUDENT = COALESCE(#COURSEIDS_STUDENT, '') + CONVERT(VARCHAR(10), COURSEID) + ' ' FROM #STUDENT
SELECT #COURSEIDS_SEM = COALESCE(#COURSEIDS_SEM , '') + CONVERT(VARCHAR(10), COURSEID) + ' ' FROM #SEM WHERE COURSEID NOT IN (SELECT COURSEID FROM #STUDENT)
SELECT #COURSEIDS_STUDENT COURSEIDS_STUDENT, #COURSEIDS_SEM COURSEIDS_SEM
try this:
;WITH CTE as (select ROW_NUMBER() over (order by (select 0)) as rn,* from Sample.dbo.Tbl_student_details)
,CTE1 As(
select rn,SselectedCourse ,replace(stuff((select ''+courseid from course_details for xml path('')),1,1,''),SselectedCourse,'') as rem from CTE a
where rn = 1
union all
select c2.rn,c2.SselectedCourse,replace(rem,c2.SselectedCourse,'') as rem
from CTE1 c1 inner join CTE c2
on c2.rn=c1.rn+1
)
select STUFF((select ''+SselectedCourse from CTE1 for xml path('')),1,0,''),(select top 1 rem from CTE1 order by rn desc)