查询计数项目和查看对象的人

时间:2019-07-15 14:15:35

标签: sql

我正在尝试编写一个查询,该查询对项目进行计数并查看谁。我不知道是否有可能。基本上,我试图让它查看某些东西,以确定是否应该对其进行计数并由查看它的人对其进行排序。

我一直在环顾四周,而我所看过的所有示例均未显示类似的内容。我知道下面的代码只是一遍又一遍地计数相同的事物,但是每次我尝试使它考虑审查员时,它基本上告诉我它不能这样做。 编辑。根据要求,这里是查询中调用的4个表。

CREATE TABLE examiner (
    examinerID INT IDENTITY(1,1) PRIMARY KEY,
    firstName CHAR(10),
    lastName CHAR(15));

CREATE TABLE evidence (
    evidenceID INT IDENTITY(1,1) PRIMARY KEY,
    faNumber VARCHAR(50) NOT NULL ,
    itemNumber VARCHAR(10) UNIQUE,
    evidenceNumber CHAR(10),
    isCDDVD VARCHAR(3),
    make CHAR(20),
    modle CHAR(20),
    identifier CHAR(20),
    typeOfDevice CHAR(20),
    size INT,
    notes VARCHAR(50));

CREATE TABLE storageMedia (
    PRIMARY KEY (belongsTo,subKey),
    belongsTO VARCHAR(10) CONSTRAINT FK_Evidence_StorageMedia_BelongsTo FOREIGN KEY REFERENCES evidence(itemNumber) ON DELETE CASCADE,
    subKey VARCHAR(5),
    faNumber VARCHAR(50) NOT NULL,
    make CHAR(20),
    modle CHAR(20),
    identifier CHAR(20),
    typeOfStorageDevice CHAR(20),
    evidenceNumber CHAR(10),
    size INT,
    notes VARCHAR(50));

CREATE TABLE faLog (
    faid INT IDENTITY(1,1) PRIMARY KEY,
    faNumber VARCHAR(50) NOT NULL UNIQUE,
    caseNumber VARCHAR(50) NOT NULL,
    startDate CHAR(10),
    endDate CHAR(10),
    examinerID INT CONSTRAINT FK_Examiner_FALog_examinerID FOREIGN KEY REFERENCES examiner(examinerID) ON DELETE CASCADE,
    DetectiveID INT CONSTRAINT FK_Detective_FALog_DetectiveID FOREIGN KEY REFERENCES detective(detectiveID) ON DELETE CASCADE,
    subjectID INT CONSTRAINT FK_theSubject_FALog_theSubjectID FOREIGN KEY REFERENCES theSubject(subjectID) ON DELETE CASCADE,
    authorazation CHAR(10),
    faStatus CHAR(15));

    ALTER TABLE evidence ADD CONSTRAINT FK_FAlog_Evidence_FANumber FOREIGN KEY (faNumber) REFERENCES faLog(faNumber) ON DELETE CASCADE;
    ALTER TABLE storageMedia ADD CONSTRAINT FK_FAlog_storageMedia_FANumber FOREIGN KEY (faNumber) REFERENCES faLog(faNumber);

相关查询

SELECT DISTINCT e.examinerID,
            e.lastName,
            (SELECT COUNT(evidence.typeOfDevice) 
            FROM evidence
            WHERE (evidence.typeOfDevice='HDD' OR evidence.typeOfDevice='SSD'))
            +
            (SELECT COUNT(storageMedia.typeOfStorageDevice) 
            FROM storageMedia
            WHERE (storageMedia.typeOfStorageDevice ='HDD' OR storageMedia.typeOfStorageDevice ='SSD')) AS 'HDD/SSD'
FROM examiner e
    INNER JOIN faLog FA ON FA.examinerID =e.examinerID
    INNER JOIN evidence EVI ON FA.faNumber= EVI.faNumber
    INNER JOIN storageMedia SM ON SM.faNumber =FA.faNumber
--WHERE (EVI.typeOfDevice ='HDD' OR EVI.typeOfDevice ='SSD') OR (SM.typeOfStorageDevice ='HDD'OR SM.typeOfStorageDevice ='SSD')
GROUP BY e.examinerID, e.lastName

我正在尝试这样输出。

ID|   Name   |HDD/SSD|

3 |   John   | 15 
8 |   Chris  | 7 
12|   Jake   | 8 
68|   Tyler  | 5 

...

但是上面的代码看起来像这样。

ID|   Name    |HDD/SSD 

3 |   John    | 15

8 |   Chris   | 15

12|   Jake    | 15

68|   Tyler   | 15

...

2 个答案:

答案 0 :(得分:1)

您不需要子查询。以下SQL是您想要的吗?如果没有,则必须将子查询更改为关联的。

SELECT e.examinerID,
                e.lastName,
                 COUNT(DISTINCT EVI.typeOfDevice) 
                +
                COUNT(SM.typeOfStorageDevice) AS 'HDD/SSD'
    FROM examiner e
        INNER JOIN faLog FA ON FA.examinerID =e.examinerID
        INNER JOIN evidence EVI ON FA.faNumber= EVI.faNumber
        INNER JOIN storageMedia SM ON SM.faNumber =FA.faNumber
    WHERE (EVI.typeOfDevice='HDD' OR EVI.typeOfDevice='SSD')
    AND (SM.typeOfStorageDevice ='HDD' OR SM.typeOfStorageDevice ='SSD')
    GROUP BY e.examinerID, e.lastName

答案 1 :(得分:0)

就像@peterHe先前所说的那样,我需要将其更改为相关查询。这就是我修复它的方式 谢谢@peterHe

SELECT e.lastName, e.firstName, (SELECT COUNT(*) FROM evidence EVI INNER JOIN faLog FA ON FA.faNumber=EVI.faNumber WHERE EVI.typeOfDevice IN ('HDD','SSD') AND (FA.examinerID=e.examinerID)) +(SELECT COUNT(*) FROM storageMedia SM INNER JOIN faLog FA ON FA.faNumber=SM.faNumber WHERE SM.typeOfStorageDevice IN ('HDD','SSD') AND (FA.examinerID=e.examinerID)) FROM examiner e