Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to create a frequency report Expand / Collapse
Author
Message
Posted Friday, May 3, 2013 11:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, May 4, 2013 7:55 AM
Points: 42, 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.
Post #1449318
Posted Saturday, May 4, 2013 7:58 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, May 4, 2013 7:55 AM
Points: 42, 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.
Post #1449423
Posted Saturday, May 4, 2013 5:53 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 9:53 PM
Points: 848, Visits: 5,467
Wrong design.

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

Then the rest is trivial.
Post #1449480
Posted Saturday, May 4, 2013 9:40 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 9:53 PM
Points: 848, Visits: 5,467
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)
Post #1449490
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse