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 查询加入
基础教程推荐
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- oracle区分大小写的原因? 2021-01-01
