SELECT * ,
COUNT(0) AS '一共'
FROM
(SELECT b.OrderID,
b.ID AS "BathNoId",
p.TicketNo,
SUM(t.TotalAmount) AS TotalMoney
FROM b AS b
LEFT JOIN p AS p WITH(NOLOCK) ON b.ID=p.BathNoId
LEFT JOIN t AS t WITH(NOLOCK) ON t.BathNoId=b.ID
WHERE t.AccountType=1
AND EXISTS
(SELECT 0
FROM
(SELECT '151223768047' _OrderID) a
WHERE a._OrderID= b.OrderID)
GROUP BY b.OrderID,
b.ID,
p.TicketNo ) AS bbb
GROUP BY bbb.BathNoId ,
bbb.OrderID,
bbb.TotalMoney,
bbb.TicketNo
不知道到一条一句怎么统计BathNoId值重复出现的个数,并且保留全部数据,所以又嵌套了一层,我想在
一共这里统计相同BathNoId值的个数并保留全部数据怎么做 谢谢
4 个解决方案
#1
可以試一下下面的sql
with temp as (
SELECT b.OrderID,
b.ID AS "BathNoId",
p.TicketNo,
SUM(t.TotalAmount) AS TotalMoney
FROM b AS b
LEFT JOIN p AS p WITH(NOLOCK) ON b.ID=p.BathNoId
LEFT JOIN t AS t WITH(NOLOCK) ON t.BathNoId=b.ID
WHERE t.AccountType=1
AND EXISTS
(SELECT 0
FROM
(SELECT '151223768047' _OrderID) a
WHERE a._OrderID= b.OrderID)
GROUP BY b.OrderID,
b.ID,
p.TicketNo
)
select *,(select count(*) from temp where OrderId=a.OrderId and BathNoId=a.BathNoId and TicketNo=a.TicketNo) as '一共'
from temp a
#2
with bbb as
(
SELECT b.OrderID,
b.ID AS "BathNoId",
p.TicketNo,
SUM(t.TotalAmount) AS TotalMoney
FROM b AS b
LEFT JOIN p AS p WITH(NOLOCK) ON b.ID=p.BathNoId
LEFT JOIN t AS t WITH(NOLOCK) ON t.BathNoId=b.ID
WHERE t.AccountType=1
AND EXISTS
(SELECT 0
FROM
(SELECT '151223768047' _OrderID) a
WHERE a._OrderID= b.OrderID)
GROUP BY b.OrderID,
b.ID,
p.TicketNo
)
select c.*, d.一共 from bbb c left join (select OrderID, BathNoId, count(0) AS '一共' from bbb group by OrderID, BathNoId) d on c.OrderID = d.OrderID and c.BathNoId = d.BathNoId
#3
SELECT * ,
COUNT(0) over(partition by bbb.BathNoId) AS '一共'
FROM
(SELECT b.OrderID,
b.ID AS "BathNoId",
p.TicketNo,
SUM(t.TotalAmount) AS TotalMoney
FROM b AS b
LEFT JOIN p AS p WITH(NOLOCK) ON b.ID=p.BathNoId
LEFT JOIN t AS t WITH(NOLOCK) ON t.BathNoId=b.ID
WHERE t.AccountType=1
AND EXISTS
(SELECT 0
FROM
(SELECT '151223768047' _OrderID) a
WHERE a._OrderID= b.OrderID)
GROUP BY b.OrderID,
b.ID,
p.TicketNo ) AS bbb
#4
你可以用COUNT(0)OVER(PARTITION BY a.OrderID,a.BatchNo)
你看看是不是这样的?
结果:
你看看是不是这样的?
;WITH a(OrderID,BatchNo,TicketNo,TotalMoney) AS (
SELECT '11111111',9147,'155-1111111',4.00 UNION
SELECT '11111111',9147,'325-1111111',4.00 UNION
SELECT '11111111',9148,'352-1111111',2.00 UNION
SELECT '11111111',9149,'375-1111111',2.00
)
SELECT *,COUNT(0)OVER(PARTITION BY a.OrderID,a.BatchNo) AS '一共' FROM a
结果:
<pre>
OrderID BatchNo TicketNo TotalMoney 一共
11111111 9147 155-1111111 4.00 2
11111111 9147 325-1111111 4.00 2
11111111 9148 352-1111111 2.00 1
11111111 9149 375-1111111 2.00 1
</pre>
#1
可以試一下下面的sql
with temp as (
SELECT b.OrderID,
b.ID AS "BathNoId",
p.TicketNo,
SUM(t.TotalAmount) AS TotalMoney
FROM b AS b
LEFT JOIN p AS p WITH(NOLOCK) ON b.ID=p.BathNoId
LEFT JOIN t AS t WITH(NOLOCK) ON t.BathNoId=b.ID
WHERE t.AccountType=1
AND EXISTS
(SELECT 0
FROM
(SELECT '151223768047' _OrderID) a
WHERE a._OrderID= b.OrderID)
GROUP BY b.OrderID,
b.ID,
p.TicketNo
)
select *,(select count(*) from temp where OrderId=a.OrderId and BathNoId=a.BathNoId and TicketNo=a.TicketNo) as '一共'
from temp a
#2
with bbb as
(
SELECT b.OrderID,
b.ID AS "BathNoId",
p.TicketNo,
SUM(t.TotalAmount) AS TotalMoney
FROM b AS b
LEFT JOIN p AS p WITH(NOLOCK) ON b.ID=p.BathNoId
LEFT JOIN t AS t WITH(NOLOCK) ON t.BathNoId=b.ID
WHERE t.AccountType=1
AND EXISTS
(SELECT 0
FROM
(SELECT '151223768047' _OrderID) a
WHERE a._OrderID= b.OrderID)
GROUP BY b.OrderID,
b.ID,
p.TicketNo
)
select c.*, d.一共 from bbb c left join (select OrderID, BathNoId, count(0) AS '一共' from bbb group by OrderID, BathNoId) d on c.OrderID = d.OrderID and c.BathNoId = d.BathNoId
#3
SELECT * ,
COUNT(0) over(partition by bbb.BathNoId) AS '一共'
FROM
(SELECT b.OrderID,
b.ID AS "BathNoId",
p.TicketNo,
SUM(t.TotalAmount) AS TotalMoney
FROM b AS b
LEFT JOIN p AS p WITH(NOLOCK) ON b.ID=p.BathNoId
LEFT JOIN t AS t WITH(NOLOCK) ON t.BathNoId=b.ID
WHERE t.AccountType=1
AND EXISTS
(SELECT 0
FROM
(SELECT '151223768047' _OrderID) a
WHERE a._OrderID= b.OrderID)
GROUP BY b.OrderID,
b.ID,
p.TicketNo ) AS bbb
#4
你可以用COUNT(0)OVER(PARTITION BY a.OrderID,a.BatchNo)
你看看是不是这样的?
结果:
你看看是不是这样的?
;WITH a(OrderID,BatchNo,TicketNo,TotalMoney) AS (
SELECT '11111111',9147,'155-1111111',4.00 UNION
SELECT '11111111',9147,'325-1111111',4.00 UNION
SELECT '11111111',9148,'352-1111111',2.00 UNION
SELECT '11111111',9149,'375-1111111',2.00
)
SELECT *,COUNT(0)OVER(PARTITION BY a.OrderID,a.BatchNo) AS '一共' FROM a
结果:
<pre>
OrderID BatchNo TicketNo TotalMoney 一共
11111111 9147 155-1111111 4.00 2
11111111 9147 325-1111111 4.00 2
11111111 9148 352-1111111 2.00 1
11111111 9149 375-1111111 2.00 1
</pre>