ROLLUP GROUP BY 옵션을 사용하십시오.
Generates the simple GROUP BY aggregate rows, plus subtotal or super-aggregate rows,
and also a grand total row.
The number of groupings that is returned equals the number of expressions
in the <composite element list> plus one. For example, consider the following statement.
Copy Code
SELECT a, b, c, SUM (<expression>)
FROM T
GROUP BY ROLLUP (a,b,c)
One row with a subtotal is generated for each unique combination of values of
(a, b, c), (a, b), and (a). A grand total row is also calculated.
Columns are rolled up from right to left.
The column order affects the output groupings of ROLLUP and can affect the number
of rows in the result set.
뭔가처럼
DECLARE @Table TABLE(
Zone VARCHAR(10),
District VARCHAR(10),
State VARCHAR(10),
Sales FLOAT
)
INSERT INTO @Table SELECT 'A','A','A',1
INSERT INTO @Table SELECT 'A','A','B',1
INSERT INTO @Table SELECT 'A','B','A',1
INSERT INTO @Table SELECT 'B','A','A',1
SELECT Zone,
District,
State,
SUM(Sales)
FROM @Table
WHERE <Your Condition here> --THIS IS WHERE YOU USE THE WHERE CLAUSE
GROUP BY ROLLUP (Zone,District,State)
두 번째와 3 largets을 얻으려면, 당신도 내가 싶어 무엇을 (ROW_NUMBER (Transact-SQL))
;WITH Vals AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY RequiredCol DESC) RowNum
FROM YourTable
)
SELECT *
FROM Vals
WHERE RowNum IN (2,3)
또는
SELECT TOP 2
*
FROM (
SELECT TOP 3
*
FROM YourTable
ORDER BY RequiredCol DESC
) sub
ORDER BY RequiredCol
내가 원하는 것은 지난 달 기간에 다음 배급 자에 의해 지역 수준보다 구역 수준에 의해 요약 수준을 얻는 것입니다 ... 귀하의 질의에 뭔가가 없습니다 ... – Sanju
나는 지난 달 또는 지난 달 .... 두 번째 질문에 관해 나를 도와주십시오 – Sanju
테이블에 날짜 열이있는 경우 where 절에서 해당 날짜를 사용하여 지난 달의 데이터 만 선택합니다. * DateCol @ Date1과 @ Date2 * –