久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产黄色一级片 | 91在线精品一区二区 | 91就要激情| 毛片综合 | 国产亚洲精品综合一区 | 日韩成人专区 | 日本一二三区高清 | 亚洲性视频 | 成人av观看 | 五月婷婷激情网 | 亚洲二区精品 | 特黄色一级毛片 | 亚洲精品乱码久久久久久按摩观 | 少妇黄色 | 国产精品欧美一区二区三区不卡 | 欧美一级一 | 欧美福利视频一区 | 日韩欧美福利视频 | 亚洲国产成人精品一区二区 | 国产精品久久视频 | 欧美黑人体内she精在线观看 | 久久久久久99 | 国产精品视频网 | 久久久久久久久久久成人 | 日韩在线观看视频一区 | 黄色网址免费在线观看 | 草久久 | 亚洲精品一二三 | 99热都是精品| 久久99视频这里只有精品 | av免费网址 | 欧美精品一区二区三区在线 | 少妇午夜一级艳片欧美精品 | 成人久久| 久久小视频 | 国产乱人伦精品一区二区 | 国内av在线 | 国产乱码精品一区二区三区中文 | 日韩欧美视频在线 | 91麻豆精品国产91久久久资源速度 | 免费视频一区 |