SQL query to fetch output using table variables amp; join(使用表变量获取输出的 SQL 查询加入)
问题描述
我正在编写一个 SQL 查询来获取一个表的数据,并使用多个或嵌套 SQL 语句或使用表变量将其与更多数据连接起来,然后再使用连接.
I am writing a SQL query to fetch the data of one table and join it with some more data either using multiple or nested SQL statements or using table variable and then use join later.
条件是,如果Spends Table中的Medium列是TV,那么在Output TableTV_Spends 应显示电视的 Spends 和该特定市场牌.这同样适用于 Medium Print.
The condition is that if the Medium column in Spends Table is TV then in the Output Table the TV_Spends should show the Spends of TV and that particular market & Brand. And this same is for Medium Print.
另外一个条件是,如果Spends Table中的Type列是CWB,那么该Primary_Brand的Spendscode> 和 Medium 不应用于 TV_Spends 和 Print_Spends 计算.
Also, another condition is that if the Type column in Spends Table is CWB then the Spends of that Primary_Brand and Medium should NOT be used in TV_Spends and Print_Spends calculation.
Total_Spends 是该 Brand_Key 的 TV_Spends 和 Print_Spends 的总和市场
Total_Spends is sum of TV_Spends and Print_Spends for that Brand_Key & Market
支出表
| Primary_Brand_Key | 中 | 市场 | 类型 | 花费 |
|---|---|---|---|---|
| Kornet | 电视 | 英国 | NULL | 1000 |
| Kornet | 电视 | 波兰 | NULL | 2000 |
| Kornet | 打印 | 波兰 | NULL | 3000 |
| Kornet | 打印 | NULL | CWB | 7000 |
| Tamas | 电视 | 英国 | NULL | 9000 |
预期产出表
| Primary_Brand | 市场 | TV_Spends | Print_Spends | Total_Spends |
|---|---|---|---|---|
| Kornet | 英国 | 1000 | NULL | 1000 |
| Kornet | 波兰 | 2000 | 3000 | 5000 |
| Tamas | 英国 | 9000 | NULL | 9000 |
输出即将到来
| Primary_Brand | 市场 | TV_Spends | Print_Spends | Total_Spends |
|---|---|---|---|---|
| NULL | NULL | NULL | NULL | 1000 |
| NULL | NULL | NULL | NULL | 5000 |
| NULL | NULL | NULL | NULL | 9000 |
| NULL | NULL | NULL | NULL | NULL |
| NULL | NULL | NULL | 3000 | NULL |
| NULL | NULL | NULL | NULL | NULL |
| NULL | NULL | 1000 | NULL | NULL |
| NULL | NULL | 2000 | NULL | NULL |
| NULL | NULL | 9000 | NULL | NULL |
| NULL | 英国 | NULL | NULL | NULL |
| NULL | 波兰 | NULL | NULL | NULL |
| NULL | 英国 | NULL | NULL | NULL |
| Kornet | NULL | NULL | NULL | NULL |
| Kornet | NULL | NULL | NULL | NULL |
| Tamas | NULL | NULL | NULL | NULL |
我编写的 SQL 查询给出了 Output Coming 输出,但是输出应该是 Expected Output Table:-
SQL Query I have written which is giving the Output Coming output, However the Output should be Expected Output Table:-
declare @output_table_spends table
(
primary_brand_var nvarchar(255),
market_var nvarchar(255),
tv_spends decimal(11,2),
print_spends decimal(11,2)
total_spends_var decimal(11,2)
)
Insert into @output_table_spends (primary_brand_var)
select Primary_Brand_Key from
dbo.Spends
Insert into @output_table_spends (market_var)
select Market from
dbo.Spends
Insert into @output_table_spends (tv_spends)
select sum(Amount_Spent_INR)
from dbo.Spends
where medium='TV'
group by Market
Insert into @output_table_spends (print_spends)
select sum(Amount_Spent_INR)
from dbo.Spends
where medium='Print'
group by Market
Insert into @output_table_spends (total_spends_var)
select sum(tv_spends,print_spends)
from dbo.Spends
group by Market
select * from dbo.Spends
select distinct A.Primary_Brand_Key, A.Market,
B.tv_spends, B.print_spends, B.total_spends_var
from dbo.Spends A
inner join @output_table_spends B
on A.Primary_Brand_Key=B.primary_brand_var
group by A.Primary_Brand_Key, A.Market, B.tv_spends, B.print_spends, B.total_spends_var
推荐答案
如果我关注你想要的,你可以使用条件聚合:
If I'm following what you want, you can use conditional aggregation:
select primary_brand_key, market,
sum(case when medium = 'TV' and type <> 'CWB' then spends else 0 end) as tv,
sum(case when medium = 'print' and type <> 'CWB' then spends else 0 end) as print,
sum(spends)
from spends s
group by primary_brand_key, market;
您的问题表明 'CWB' 仅用于 tv 和 print 列.但是,如果您真的想要所有三个都使用它,请改用 where 子句.
Your question suggests that the 'CWB' is only needed for the tv and print columns. However, if you really want it for all three, then use a where clause instead.
这篇关于使用表变量获取输出的 SQL 查询加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用表变量获取输出的 SQL 查询加入
基础教程推荐
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
