0

I want to get the count of addmited records by user in 2 table. for example, I have a list of users(table STF) and want to know how many product did a user create (in table PV1) and how many product did he sell (in table dpq), I want to show these data like below: enter image description here

I have these 2 queries, and don't know how to show them in one table with 3 columns...

Query 1:

select staff_username,  COUNT(*)  as 'count 1'
from STF right join PV1 on STF.staff_username = PV1.admit_user
group by staff_username, staff_name + ' ' + staff_family

Query 2:

select trf_staff_id, COUNT(trf_staff_id)
from dpq join stf on trf_staff_id = stf.staff_username
group by trf_staff_id
4

2 回答 2

1

您可以尝试像这样加入两个查询。从您提供的有限信息来看,staff_username 似乎与 trf_staff_id 相同,因为您在第二个查询的连接条件中使用了它。

SELECT
    staff_username,
    count_1,
    count_2 FROM
    (
        SELECT
            staff_username,
            COUNT(*) AS count_1
        FROM
            STF
        RIGHT JOIN
            PV1
        ON
            STF.staff_username = PV1.admit_user
        GROUP BY
            staff_username,
            staff_name + ' ' + staff_family ) QRY_CNT_1 INNEER JOIN
    (
        SELECT
            trf_staff_id,
            COUNT(trf_staff_id) AS count_2
        FROM
            dpq
        JOIN
            stf
        ON
            trf_staff_id = stf.staff_username
        GROUP BY
            trf_staff_id ) QRY_CNT_2 ON
    QRY_CNT_2.trf_staff_id = QRY_CNT_1.staff_username
于 2018-02-13T14:17:10.677 回答
1

我猜你想组合两个表count,你可以join添加count

但你给我们的信息少,所以我不确定。

SELECT
    STF.staff_username,
    STF.Data_in_PV1,
    dpq.Data_in_dpq
FROM
(
    SELECT
        staff_username,
        COUNT(PV1.admit_user) AS 'Data_in_PV1'
    FROM
        STF
    RIGHT JOIN
        PV1
    ON
        STF.staff_username = PV1.admit_user
    GROUP BY
        staff_username
) STF 
INNER JOIN
(
    SELECT
        dpq.trf_staff_id,
        COUNT(trf_staff_id) AS 'Data_in_dpq'
    FROM
        dpq
    INNER JOIN
        stf
    ON
        dpq.trf_staff_id = stf.staff_username
    GROUP BY
        dpq.trf_staff_id 
) dpq ON dpq.trf_staff_id = STF.staff_username
于 2018-02-13T12:52:44.730 回答