久久久久久久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福利网| 亚洲一区二区av | 久久国产精品视频 | 亚洲欧洲国产视频 | 成人免费淫片aa视频免费 | 91精品久久久久久久久中文字幕 | 成人精品视频免费 | 黄色毛片免费 | 久久av一区二区三区 | 91精品国产综合久久久密闭 | 久久久精| 91视在线国内在线播放酒店 | 国产综合久久久 | 精品乱子伦一区二区三区 | 日本亚洲一区 | 色婷婷久久| 综合久 | 欧美一区二区三区视频在线观看 | 国产高清视频在线播放 | 欧美精品区 | 亚洲欧美日韩成人在线 | 一区二区三区av | 91精品国产日韩91久久久久久 | 黄色大片免费网站 | 综合国产 | 91在线精品视频 | 亚洲一区二区网站 | 国产一区二区三区欧美 | 国产探花 | 午夜视频免费在线观看 | 欧美激情综合五月色丁香小说 | 久久精品国产一区二区电影 | 久久91精品久久久久久9鸭 |