請教各位大神SQL語法如下
select BRAND_NAME 它牌 ,COUNT (ASSET.BRAND_NAME)from ASSET
where BRAND_NAME not in ('brother','NCC')
GROUP BY BRAND_NAME
若需要計算所有數量該如何下???
這樣??
declare @ASSET table(
BRAND_NAME nvarchar(50)
)
insert into @ASSET
values('')
,('')
,('')
,('')
,('')
,('baby lock')
,('baby lock')
,('baby lock')
,('baby lock')
,('baby lock')
,('baby lock')
select BRAND_NAME 它牌
,COUNT (0) Num
,sum(COUNT (0))over() TotalNum
from @ASSET
where BRAND_NAME not in ('brother','NCC')
GROUP BY BRAND_NAME
或這樣??
select *
from ((
select BRAND_NAME 它牌
,COUNT (0) Num
from @ASSET
where BRAND_NAME not in ('brother','NCC')
GROUP BY BRAND_NAME
)union all(
select '總計'
,count(0) Num
from @ASSET
where BRAND_NAME not in ('brother','NCC')
)) k
您好感謝回復
請教若用此方式裡面共有9筆資料需要算總計
我該如何下?
那就是這個答案吧~
就二次查詢也可以
select *
from ((
select BRAND_NAME 它牌
,COUNT (0) Num
from @ASSET
where BRAND_NAME not in ('brother','NCC')
GROUP BY BRAND_NAME
)union all(
select '總計'
,count(0) Num
from @ASSET
where BRAND_NAME not in ('brother','NCC')
)) k
或這樣
select Sum(Num) 總計
from (
select BRAND_NAME 它牌
,COUNT (0) Num
from @ASSET
where BRAND_NAME not in ('brother','NCC')
GROUP BY BRAND_NAME
) k
感謝@純真的人^^
還是不對真不知該如何修改
必須宣告變數
那個@是我宣告臨時變數~你直接刪除~
像這樣~
select *
from ((
select BRAND_NAME 它牌
,COUNT (0) Num
from ASSET
where BRAND_NAME not in ('brother','NCC')
GROUP BY BRAND_NAME
)union all(
select '總計'
,count(0) Num
from ASSET
where BRAND_NAME not in ('brother','NCC')
)) k
select Sum(Num) 總計
from (
select BRAND_NAME 它牌
,COUNT (0) Num
from ASSET
where BRAND_NAME not in ('brother','NCC')
GROUP BY BRAND_NAME
) k
除非你有像我這樣宣告@ASSET才有用~
讚!厲害
我自己寫這樣
SELECT BRAND_NAME AS 它牌, COUNT(BRAND_NAME) AS AA
FROM ASSET
WHERE (BRAND_NAME NOT IN ('brother', 'NCC'))
GROUP BY BRAND_NAME
union
SELECT '', COUNT(BRAND_NAME) AS AA
FROM ASSET
WHERE (BRAND_NAME NOT IN ('brother', 'NCC'))
但你的更漂亮
讚
select A.BRAND_NAME 它牌 ,COUNT (A.BRAND_NAME)
,B.AllCnt
from ASSET A
JOIN (
SELECT COUNT(1) AllCnt
FROM ASSET
) B ON 1 = 1
where A.BRAND_NAME not in ('brother','NCC')
GROUP BY A.BRAND_NAME
select A.BRAND_NAME 它牌 ,COUNT (A.BRAND_NAME)
, (SELECT COUNT(1) FROM ASSET) AS AllCnt
from ASSET A
where A.BRAND_NAME not in ('brother','NCC')
GROUP BY A.BRAND_NAME
您好感謝回復 count後面無法帶簡碼
select A.BRAND_NAME 它牌 ,COUNT (A.BRAND_NAME)
,B.AllCnt
from ASSET A
JOIN (
SELECT COUNT(1) AllCnt
FROM ASSET
) B ON 1 = 1
where A.BRAND_NAME not in ('brother','NCC')
GROUP BY A.BRAND_NAME
不好意思,感謝您 我SQL語法還沒那麼熟
錯誤訊息如下
資料行 'B.AllCnt' 在選取清單中無效,因為它並未包含在彙總函式或 GROUP BY 子句中。