SQL 计算修剪后的行数
SQL count rows after trimming
我有一个复杂的数据库,其中有很多行我需要计算。有些行在主要项目前面有额外的 9 个字符。这是示例:
select trim(item.t_item) as Item, count(item.t_item) as counterItem
from ttdsls400201 main
inner join ttccom100201 client on client.t_bpid = main.t_ofbp
inner join ttdsls401201 item on item.t_orno = main.t_orno
WHERE client.t_nama = N'LIEBHERR TRANSP. SYST. GMBH & CO KG' and trim(item.t_item)not like N'3%' and trim(item.t_item) not like N'6%' and t_item like N'%LTSM-11085421-008%'
group by item.t_item, main.t_ddat , main.t_corn, main.t_oamt, main.t_ccur
ORDER BY counterItem desc
这个 select 给我以下输出:
我将使用以下内容删除前 9 个字符(LTSM 之前的所有字符):
select RIGHT((t_item), LEN(t_item) - 9) AS MyTrimmedColumn, count(RIGHT((t_item), LEN(t_item) - 9)) as counterItem
from ttdsls400201 main
inner join ttccom100201 client on client.t_bpid = main.t_ofbp
inner join ttdsls401201 item on item.t_orno = main.t_orno
WHERE client.t_nama = N'LIEBHERR TRANSP. SYST. GMBH & CO KG' and trim(item.t_item)not like N'3%' and trim(item.t_item) not like N'6%' and t_item like N'%LTSM-11085421-008%'
group by item.t_item, main.t_ddat , main.t_corn, main.t_oamt, main.t_ccur
ORDER BY counterItem desc
所以我得到了这个结果:
问题是为什么counterItem=1的最后两行counter不在最上面?它们与顶部相同,但它们并不相反。
而不是使用分组 item.t_item 你应该需要使用这个 RIGHT((t_item), LEN(t_item) - 9)
因为如果您在分组语句 item.t_item 中使用此列,它会根据文本 'SLS000932'、'SLS000953'[ 进行分隔=11=]
select RIGHT((t_item), LEN(t_item) - 9) AS MyTrimmedColumn, count(RIGHT((t_item),
LEN(t_item) - 9)) as counterItem
from ttdsls400201 main
inner join ttccom100201 client on client.t_bpid = main.t_ofbp
inner join ttdsls401201 item on item.t_orno = main.t_orno
WHERE client.t_nama = N'LIEBHERR TRANSP. SYST. GMBH & CO KG' and trim(item.t_item)not
like N'3%' and trim(item.t_item) not like N'6%' and t_item like N'%LTSM-11085421-008%'
group by RIGHT((t_item), LEN(t_item) - 9), main.t_ddat , main.t_corn, main.t_oamt,
main.t_ccur
ORDER BY counterItem desc
我有一个复杂的数据库,其中有很多行我需要计算。有些行在主要项目前面有额外的 9 个字符。这是示例:
select trim(item.t_item) as Item, count(item.t_item) as counterItem
from ttdsls400201 main
inner join ttccom100201 client on client.t_bpid = main.t_ofbp
inner join ttdsls401201 item on item.t_orno = main.t_orno
WHERE client.t_nama = N'LIEBHERR TRANSP. SYST. GMBH & CO KG' and trim(item.t_item)not like N'3%' and trim(item.t_item) not like N'6%' and t_item like N'%LTSM-11085421-008%'
group by item.t_item, main.t_ddat , main.t_corn, main.t_oamt, main.t_ccur
ORDER BY counterItem desc
这个 select 给我以下输出:
我将使用以下内容删除前 9 个字符(LTSM 之前的所有字符):
select RIGHT((t_item), LEN(t_item) - 9) AS MyTrimmedColumn, count(RIGHT((t_item), LEN(t_item) - 9)) as counterItem
from ttdsls400201 main
inner join ttccom100201 client on client.t_bpid = main.t_ofbp
inner join ttdsls401201 item on item.t_orno = main.t_orno
WHERE client.t_nama = N'LIEBHERR TRANSP. SYST. GMBH & CO KG' and trim(item.t_item)not like N'3%' and trim(item.t_item) not like N'6%' and t_item like N'%LTSM-11085421-008%'
group by item.t_item, main.t_ddat , main.t_corn, main.t_oamt, main.t_ccur
ORDER BY counterItem desc
所以我得到了这个结果:
问题是为什么counterItem=1的最后两行counter不在最上面?它们与顶部相同,但它们并不相反。
而不是使用分组 item.t_item 你应该需要使用这个 RIGHT((t_item), LEN(t_item) - 9)
因为如果您在分组语句 item.t_item 中使用此列,它会根据文本 'SLS000932'、'SLS000953'[ 进行分隔=11=]
select RIGHT((t_item), LEN(t_item) - 9) AS MyTrimmedColumn, count(RIGHT((t_item),
LEN(t_item) - 9)) as counterItem
from ttdsls400201 main
inner join ttccom100201 client on client.t_bpid = main.t_ofbp
inner join ttdsls401201 item on item.t_orno = main.t_orno
WHERE client.t_nama = N'LIEBHERR TRANSP. SYST. GMBH & CO KG' and trim(item.t_item)not
like N'3%' and trim(item.t_item) not like N'6%' and t_item like N'%LTSM-11085421-008%'
group by RIGHT((t_item), LEN(t_item) - 9), main.t_ddat , main.t_corn, main.t_oamt,
main.t_ccur
ORDER BY counterItem desc