SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to create a frequency report


How to create a frequency report

Author
Message
RZ52
RZ52
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 74
Hi,
I have two tables containing test results in categorical scale.
Table one contains PCR results for some factors (P1, P2, ....). The assigned values are (0='Not tested', 1='Positive', 2='Negative')

IF object_id('tempdb..#TestTb1') IS NOT NULL
BEGIN
DROP TABLE #TestTb1
END
CREATE TABLE #TestTb1 ([Sample_ID] VARCHAR(10), [P1] INT, [P2] INT, [P3] INT, [P4] INT)

INSERT INTO #TestTb1
SELECT 'Sample1', 1, 1, 0, 2 UNION ALL
SELECT 'Sample2', 0, 0, 2, 1 UNION ALL
SELECT 'Sample3', 1, 2, 0, 1 UNION ALL
SELECT 'Sample4', 0, 1, 0, 1 UNION ALL
SELECT 'Sample5', 1, 1, 1, 1 UNION ALL
SELECT 'Sample6', 2, 1, 1, 2 UNION ALL
SELECT 'Sample7', 1, 2, 1, 2 UNION ALL
SELECT 'Sample8', 0, 2, 0, 1 UNION ALL
SELECT 'Sample9', 1, 1, 0, 0 UNION ALL
SELECT 'Sample10', 2, 2, 2, 1 UNION ALL
SELECT 'Sample11', 1, 1, 0, 0 UNION ALL
SELECT 'Sample12', 2, 1, 2, 0



Table two includes antibiotic resistance test results for different antibiotics (AB1, AB2, ....). The values are (0='Not tested', 1='Sensitive', 2='Intermediate', 3='Resistant')

IF object_id('tempdb..#TestTb2') IS NOT NULL
BEGIN
DROP TABLE #TestTb2
END
CREATE TABLE #TestTb2 ([Sample_ID] VARCHAR(10), [AB1] INT, [AB2] INT, [AB3] INT, [AB4] INT)

INSERT INTO #TestTb2
SELECT 'Sample1', 3, 1, 0, 2 UNION ALL
SELECT 'Sample2', 2, 1, 3, 3 UNION ALL
SELECT 'Sample3', 3, 3, 3, 3 UNION ALL
SELECT 'Sample4', 0, 2, 0, 3 UNION ALL
SELECT 'Sample5', 1, 3, 1, 2 UNION ALL
SELECT 'Sample6', 3, 1, 2, 3 UNION ALL
SELECT 'Sample7', 1, 1, 1, 2 UNION ALL
SELECT 'Sample8', 3, 2, 0, 1 UNION ALL
SELECT 'Sample9', 1, 1, 0, 3 UNION ALL
SELECT 'Sample10', 2, 3, 2, 1 UNION ALL
SELECT 'Sample11', 3, 1, 0, 2 UNION ALL
SELECT 'Sample12', 2, 1, 3, 0



Now, I need to build a report to show the frequency of positive (=1) PCR factors (P1,P2,...) for each antibiotic (AB1,AB2, ....) when the sample is resistant (=3) for the antibiotic.

The result report supposes to be like this :

Antibiotic Factor Freq.
(=3) (Factor=1)
---------- ------- -------------
AB1 P1 3
AB1 P2 3
AB1 P3 1
AB1 P4 2
AB2 P1 2
AB2 P2 1
AB2 P3 1
AB2 P4 3
AB3 P1 1
AB3 P2 1
AB3 P3 0
AB3 P4 2
AB4 P1 2
AB4 P2 3
AB4 P3 1
AB4 P4 3


As we have around 20 antibiotics, 20 PCR factors and thousands of samples, any suggestions will be a great help and will be appreciated.

Thanks in advance.
RZ52
RZ52
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 74
Hello again,

I managed to find a simple solution that seems working on sample tables.


;WITH Final_TB AS
(
SELECT X.SAMPLE_ID, X.AntiBio, #TestTb1.P1, #TestTb1.P2, #TestTb1.P3, #TestTb1.P4 FROM
(
SELECT [Sample_ID],'AB1' AS [AntiBio]
FROM #TestTb2 WHERE [AB1] = '3'
UNION ALL
SELECT [Sample_ID],'AB2' AS [AntiBio]
FROM #TestTb2 WHERE [AB2] = '3'
UNION ALL
SELECT [Sample_ID],'AB3' AS [AntiBio]
FROM #TestTb2 WHERE [AB3] = '3'
UNION ALL
SELECT [Sample_ID],'AB4' AS [AntiBio]
FROM #TestTb2 WHERE [AB4] = '3'
) X JOIN #TestTb1 ON X.[Sample_ID] = #TestTb1.[Sample_ID]
)
SELECT AntiBio, PCR, COUNT(1) AS [Freq.]
FROM
(
SELECT [Sample_ID], [AntiBio], 'P1' AS [PCR]
FROM Final_TB WHERE [P1] = '1'
UNION ALL
SELECT [Sample_ID], [AntiBio], 'P2' AS [PCR]
FROM Final_TB WHERE [P2] = '1'
UNION ALL
SELECT [Sample_ID], [AntiBio], 'P3' AS [PCR]
FROM Final_TB WHERE [P3] = '1'
UNION ALL
SELECT [Sample_ID], [AntiBio], 'P4' AS [PCR]
FROM Final_TB WHERE [P4] = '1'
) Y
GROUP BY Antibio, PCR ORDER BY Antibio, PCR



It returns the expected table. However, as I mentioned with too many variables (the variable numbers are dynamic), I search for a more flexible solution.

Any suggestions ?

Thanks in advance.
pietlinden
pietlinden
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25026 Visits: 14761
Wrong design.

CREATE TABLE Test(
PatientID int,
TestID int,
Reading decimal(10,4)
TestDate DateTime
);

Then the rest is trivial.
pietlinden
pietlinden
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25026 Visits: 14761
This design:

CREATE TABLE #TestTb1 ([Sample_ID] VARCHAR(10), [P1] INT, [P2] INT, [P3] INT, [P4] INT)

is going to bite you. I would strongly recommend not doing that. (I worked on even small databases in Ack!cess that were like this, and performance was abominable. If you create a union query, you may lose the benefits your indexing provides, so you'll get table scans every time you query anything. Performance killer.

If you changed your structure to something like

CREATE TABLE #TestTb1 ([Sample_ID] VARCHAR(10)
, [TestID], [P1] INT)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search