-5

1 つにまとめる必要がある 3 つのクエリがあります。どうぞ

SELECT COUNT(e.EmployeeID) AS TotalEmpCount
FROM Employees as e
WHERE HomePhone <> ''

SELECT COUNT(e.EmployeeID) AS USEmpCount
FROM Employees as e
INNER JOIN Orders as o ON e.EmployeeID=o.EmployeeID
WHERE e.HomePhone <> '' AND e.Country='US' AND o.OrderDate BETWEEN '11/01/2011' AND GETDATE()


SELECT COUNT(e.EmployeeID) AS UKEmpCount
FROM Employees as e
INNER JOIN Orders as o ON e.EmployeeID=o.EmployeeID
WHERE HomePhone <> '' AND Country ='UK' AND o.OrderDate BETWEEN '01/01/2011' AND '12/31/2012'

TotalEmpCount、USEmpCount、UKEmpCount の 3 つの列を返す 1 つのクエリが必要です。

このクエリは T-SQL と Linq 形式で必要です

4

2 に答える 2

1

これにより、同じ結果が得られるはずです。

SELECT COUNT(e.EmployeeID) AS TotalEmpCount,
    USEmpCount = SUM(CASE WHEN e.Country='US' AND o.OrderDate BETWEEN '11/01/2011' AND GETDATE() THEN 1 ELSE 0 END),
    UKEmpCount = SUM(CASE WHEN e.Country ='UK' AND o.OrderDate BETWEEN '01/01/2011' AND '12/31/2012' THEN 1 ELSE 0 END)
FROM Employees as e
LEFT JOIN Orders o
    ON e.EmployeeID = o.EmployeeID
WHERE HomePhone <> ''
于 2012-11-15T20:22:58.380 に答える
0

自分で考え出した:

SELECT  (
        SELECT COUNT(*) 
FROM Employees as e
WHERE HomePhone <> ''
        ) AS TotalEmpCount,
        (
        SELECT COUNT(*)
FROM Employees as e
INNER JOIN Orders as o ON e.EmployeeID=o.EmployeeID
WHERE e.HomePhone <> '' AND e.Country='US' AND o.OrderDate BETWEEN '11/01/2011' AND GETDATE()
        )  AS USEmpCount,
        (
        SELECT COUNT(*)
FROM Employees as e
INNER JOIN Orders as o ON e.EmployeeID=o.EmployeeID
WHERE HomePhone <> '' AND Country ='UK' AND o.OrderDate BETWEEN '01/01/2011' AND '12/31/2012'
        )  AS UKEmpCount
于 2012-11-15T20:15:45.527 に答える