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

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

SQL query to fetch output using table variables amp; join(使用表變量獲取輸出的 SQL 查詢加入)
本文介紹了使用表變量獲取輸出的 SQL 查詢加入的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在編寫一個(gè) SQL 查詢來(lái)獲取一個(gè)表的數(shù)據(jù),并使用多個(gè)或嵌套 SQL 語(yǔ)句或使用表變量將其與更多數(shù)據(jù)連接起來(lái),然后再使用連接.

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 應(yīng)顯示電視的 Spends 和該特定市場(chǎng)牌.這同樣適用于 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.

另外一個(gè)條件是,如果Spends Table中的Type列是CWB,那么該Primary_Brand的Spendscode> 和 Medium 不應(yīng)用于 TV_SpendsPrint_Spends 計(jì)算.

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 的總和市場(chǎng)

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

支出表

<頭>
Primary_Brand_Key市場(chǎng)類型花費(fèi)
Kornet電視英國(guó)NULL1000
Kornet電視波蘭NULL2000
Kornet打印波蘭NULL3000
Kornet打印NULLCWB7000
Tamas電視英國(guó)NULL9000

預(yù)期產(chǎn)出表

<頭>
Primary_Brand市場(chǎng)TV_SpendsPrint_SpendsTotal_Spends
Kornet英國(guó)1000NULL1000
Kornet波蘭200030005000
Tamas英國(guó)9000NULL9000

輸出即將到來(lái)

<頭>
Primary_Brand市場(chǎng)TV_SpendsPrint_SpendsTotal_Spends
NULLNULLNULLNULL1000
NULLNULLNULLNULL5000
NULLNULLNULLNULL9000
NULLNULLNULLNULLNULL
NULLNULLNULL3000NULL
NULLNULLNULLNULLNULL
NULLNULL1000NULLNULL
NULLNULL2000NULLNULL
NULLNULL9000NULLNULL
NULL英國(guó)NULLNULLNULL
NULL波蘭NULLNULLNULL
NULL英國(guó)NULLNULLNULL
KornetNULLNULLNULLNULL
KornetNULLNULLNULLNULL
TamasNULLNULLNULLNULL

我編寫的 SQL 查詢給出了 Output Coming 輸出,但是輸出應(yīng)該是 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

推薦答案

如果我關(guān)注你想要的,你可以使用條件聚合:

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;

您的問(wèn)題表明 'CWB' 僅用于 tvprint 列.但是,如果您真的想要所有三個(gè)都使用它,請(qǐng)改用 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.

這篇關(guān)于使用表變量獲取輸出的 SQL 查詢加入的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
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?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(hào)(問(wèn)題和答案的組合))
主站蜘蛛池模板: 日韩免费视频一区二区 | 少妇av片 | 午夜激情一区 | 亚洲精品免费视频 | 视频一区二区在线 | 免费观看一级特黄欧美大片 | 欧美性吧| www日| 免费的色网站 | 久久久国产一区二区三区四区小说 | 国产探花 | 午夜精品一区二区三区在线视频 | 一区二区av | 久久久免费| 国内自拍视频在线观看 | 黄色三级毛片 | 欧美精品一区二区在线观看 | 色悠悠久 | 欧美视频一区 | 在线播放日韩 | 日韩成人精品在线观看 | 国产激情一区二区三区 | 91日韩在线 | 国际精品鲁一鲁一区二区小说 | 日韩欧美国产精品一区二区 | 天天操综合网站 | 国产乱码精品一品二品 | 一区二区高清不卡 | 欧美a区| 日韩精品一区二区在线 | 国外成人在线视频 | 九九热视频这里只有精品 | 精品亚洲一区二区三区四区五区 | 成人妇女免费播放久久久 | 亚洲美女天堂网 | 亚洲精品一二区 | 国产精品视频导航 | 国产 日韩 欧美 制服 另类 | 色天天综合 | 成人h电影在线观看 | 色就是色欧美 |