how can I dynamically change the number of records that needs to be filtered out?

Viewed 29

The final report is going to be in tableau, while SQL will be used to pull records. So a solution in any of these two is appreciated.

I have three tables A,B,C. I need to sample a total of 30 records from these three tables for a report for each month. On a ideal scenario, it should be 10 records each from these table. But,if for some reason, one of the tables have less than 10 records for a month, the sample includes all the data. Also, it needs to be compensated from the other two tables. (15+10+5 or any other combination).

is there a way to dynamically achieve this?

I tried to create separate worksheets in tableau, and merge it in a dashboard, but that doesn't work. Plus, they all should be presented as one single report, not as separate reports to download.

1 Answers

This works coming from SQL. It does have the drawback of always trying to pull the missing rows from table a first, but if that is not an issue this should suffice.

EDIT: Removed extraneous WHILE bits and just used TOP to try and get 10 rows from each table to start.

DECLARE @a TABLE (id INT IDENTITY, adata VARCHAR(10))
DECLARE @b TABLE (id INT IDENTITY, bdata VARCHAR(10))
DECLARE @c TABLE (id INT IDENTITY, cdata VARCHAR(10))

INSERT INTO @a
VALUES
('a1'),
('a2'),
('a3'),
('a4'),
('a5'),
('a6'),
('a7'),
('a8'),
('a9'),
('a10'),
('a11'),
('a12'),
('a13'),
('a14'),
('a15')

INSERT INTO @b
VALUES
('b1'),
('b2'),
('b3'),
('b4'),
('b5'),
('b6'),
('b7'),
('b8'),
('b9'),
('b10'),
('b11'),
('b12'),
('b13'),
('b14'),
('b15')

INSERT INTO @c
VALUES
('c1'),
('c2'),
('c3'),
('c4'),
('c5')

DECLARE @dataset TABLE (id INT IDENTITY, datasource VARCHAR(10), mdata VARCHAR(10), sourceid INT)

DECLARE @countert INT = 0

--try to get 10 rows from table a
INSERT INTO @dataset (datasource, mdata, sourceid)
SELECT TOP (10) 'a' AS datasource
    , adata AS mdata
    , id AS sourceid
FROM @a a
ORDER BY id

--try to get 10 rows from table b
INSERT INTO @dataset (datasource, mdata, sourceid)
SELECT TOP (10) 'b' AS datasource
    , bdata AS mdata
    , id AS sourceid
FROM @b b
ORDER BY id

--try to get 10 rows from table c
INSERT INTO @dataset (datasource, mdata, sourceid)
SELECT TOP (10) 'c' AS datasource
    , cdata AS mdata
    , id AS sourceid
FROM @c c
ORDER BY id

--set the total counter to the current number of rows
SET @countert = @countert + (SELECT COUNT(id) FROM @dataset)

--try to finish off from table a
WHILE @countert < 30
BEGIN
INSERT INTO @dataset (datasource, mdata, sourceid)
SELECT TOP (1) 'a' AS datasource
    , adata AS mdata
    , id AS sourceid
FROM @a a
WHERE NOT EXISTS (SELECT 1 FROM @dataset d WHERE datasource = 'a' AND d.sourceid = a.id)
ORDER BY id
SET @countert = @countert +1
END

--try to finish off from table b
WHILE @countert < 30
BEGIN
INSERT INTO @dataset (datasource, mdata, sourceid)
SELECT TOP (1) 'b' AS datasource
    , bdata AS mdata
    , id AS sourceid
FROM @b b
WHERE NOT EXISTS (SELECT 1 FROM @dataset d WHERE datasource = 'b' AND d.sourceid = b.id)
ORDER BY id
SET @countert = @countert +1
END

--try to finish off from table c
WHILE @countert < 30
BEGIN
INSERT INTO @dataset (datasource, mdata, sourceid)
SELECT TOP (1) 'c' AS datasource
    , cdata AS mdata
    , id AS sourceid
FROM @c c
WHERE NOT EXISTS (SELECT 1 FROM @dataset d WHERE datasource = 'c' AND d.sourceid = c.id)
ORDER BY id
SET @countert = @countert +1
END

SELECT * FROM @dataset
id datasource mdata sourceid
1 a a1 1
2 a a2 2
3 a a3 3
4 a a4 4
5 a a5 5
6 a a6 6
7 a a7 7
8 a a8 8
9 a a9 9
10 a a10 10
11 b b1 1
12 b b2 2
13 b b3 3
14 b b4 4
15 b b5 5
16 b b6 6
17 b b7 7
18 b b8 8
19 b b9 9
20 b b10 10
21 c c1 1
22 c c2 2
23 c c3 3
24 c c4 4
25 c c5 5
26 a a11 11
27 a a12 12
28 a a13 13
29 a a14 14
30 a a15 15
Related