久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用表變量獲取輸出的 SQL 查詢加入

SQL query to fetch output using table variables amp; join(使用表變量獲取輸出的 SQL 查詢加入)
本文介紹了使用表變量獲取輸出的 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_SpendsPrint_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_KeyTV_SpendsPrint_Spends 的總和市場

Total_Spends is sum of TV_Spends and Print_Spends for that Brand_Key & Market

支出表

<頭>
Primary_Brand_Key市場類型花費
Kornet電視英國NULL1000
Kornet電視波蘭NULL2000
Kornet打印波蘭NULL3000
Kornet打印NULLCWB7000
Tamas電視英國NULL9000

預期產出表

<頭>
Primary_Brand市場TV_SpendsPrint_SpendsTotal_Spends
Kornet英國1000NULL1000
Kornet波蘭200030005000
Tamas英國9000NULL9000

輸出即將到來

<頭>
Primary_Brand市場TV_SpendsPrint_SpendsTotal_Spends
NULLNULLNULLNULL1000
NULLNULLNULLNULL5000
NULLNULLNULLNULL9000
NULLNULLNULLNULLNULL
NULLNULLNULL3000NULL
NULLNULLNULLNULLNULL
NULLNULL1000NULLNULL
NULLNULL2000NULLNULL
NULLNULL9000NULLNULL
NULL英國NULLNULLNULL
NULL波蘭NULLNULLNULL
NULL英國NULLNULLNULL
KornetNULLNULLNULLNULL
KornetNULLNULLNULLNULL
TamasNULLNULLNULLNULL

我編寫的 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' 僅用于 tvprint 列.但是,如果您真的想要所有三個都使用它,請改用 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 查詢加入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产精品福利视频 | 日韩av在线播 | 亚洲视频一区在线观看 | 99精品网 | 国产japanhdxxxx麻豆 | 男女精品久久 | 亚洲欧美日韩电影 | 国产激情自拍视频 | 99久久精品免费看国产四区 | 二区成人| 在线视频一区二区 | 成人精品| 一区二区在线看 | 韩国欧洲一级毛片 | 久久亚洲一区二区三区四区 | 国产 日韩 欧美 制服 另类 | 黄色一级毛片 | aaaaaa大片免费看最大的 | 久久久久久中文字幕 | 国产精品黄视频 | av资源中文在线 | 国产成人99久久亚洲综合精品 | 日本成人中文字幕 | 久久9精品 | 粉嫩av在线 | 国内在线视频 | 国产一二三区精品视频 | 7777在线 | 国产精品日韩欧美 | 国产精品久久久久9999鸭 | 最新国产在线 | 99精品免费在线观看 | 奇米四色影视 | 精品亚洲永久免费精品 | 亚洲综合三区 | 日韩av成人 | 亚洲国产福利视频 | 国产真实精品久久二三区 | 精品国产一区二区三区免费 | 婷婷在线网站 | 色婷婷精品国产一区二区三区 |