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

SQL:計(jì)算兩個(gè)不同表中的兩個(gè)不同列

SQL: Count two different columns from two different tables(SQL:計(jì)算兩個(gè)不同表中的兩個(gè)不同列)
本文介紹了SQL:計(jì)算兩個(gè)不同表中的兩個(gè)不同列的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試獲取兩個(gè)不同表的資源列的不同計(jì)數(shù),然后顯示每個(gè)項(xiàng)目 ID 的比較.現(xiàn)在,此查詢?yōu)槲姨峁┝藘蓚€(gè)表的相同計(jì)數(shù)值.

I am trying to get the distinct counts for the resource column of two different tables, then show the comparison for each project ID. Right now, this query gives me the same count values for both tables.

select 
    t1.PRJCT_ID,
    count(t1.RSRC_ID) as TBL1_RSRC_CNT,
    t2.PRJCT_ID,
    count(t2.RSRC_ID) as TBL2_RSRC_CNT
from
    DATA_TABLE_1 t1
LEFT OUTER JOIN 
    DATA_TABLE_2 t2 on t1.PRJCT_ID = t2.PRJCT_ID
GROUP BY
    t1.PRJCT_ID, t2.PRJCT_ID
order by 1

推薦答案

當(dāng)然你會(huì)得到同樣的計(jì)數(shù),你正在計(jì)算同一個(gè)表的列(它是由一個(gè)連接產(chǎn)生的,授予,但它仍然是一個(gè)長方形的桌子).

Of course you're going to get the same count like that, you're counting the columns of the same table (which is made by a join, granted, but it's still a rectangular table).

您想要做的是使用子查詢.首先獲取每個(gè)項(xiàng)目 id 的列表(從一個(gè)表中,或解析兩個(gè)相關(guān)表的聯(lián)合,但這是數(shù)據(jù)庫規(guī)范化不良的標(biāo)志),然后獨(dú)立查詢這些表的計(jì)數(shù):

What you want to do is use subqueries. First get a list of every project id (from a table, or an union of parsing both tables in question, but that's a sign of bad database normalization), then query the tables independently for their count:

select p.ID,
  (select count(*) from DATA_TABLE_1 t1 where t1.ID=p.ID) Count1,
  (select count(*) from DATA_TABLE_2 t2 where t2.ID=p.ID) Count2
from projects p

這篇關(guān)于SQL:計(jì)算兩個(gè)不同表中的兩個(gè)不同列的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(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)(問題和答案的組合))
主站蜘蛛池模板: 一区二区三区国产好 | 青草视频在线 | 91电影 | 91大神在线资源观看无广告 | 丁香婷婷久久久综合精品国产 | 米奇成人网 | 欧美色综合一区二区三区 | 欧美一级特黄aaa大片在线观看 | 欧美一二三四成人免费视频 | 日韩免费网 | 亚洲福利网 | 国产一区二区视频免费在线观看 | 狠狠爱免费视频 | 91精品国产91久久久久久吃药 | 日韩成人在线播放 | 免费一级片 | 九久久| 欧美精品v | 国产中文一区二区三区 | 成人欧美一区二区三区 | 91视频网址 | 91在线视频播放 | 狠狠久| 久久亚洲国产精品 | 男人的天堂在线视频 | 国产精品久久久久久久久久久久午夜片 | 成人久久18免费网站麻豆 | 久久久精品久久久 | 欧美日韩久久 | 一区二区三区在线观看视频 | 亚洲国产精品久久 | 精品中文字幕久久 | 久久久久久成人 | 久久99精品国产自在现线小黄鸭 | 天天操天天摸天天爽 | 国产精品久久久久久久久久 | 欧美在线视频不卡 | 久久新 | 久久国产日韩欧美 | 久久99精品久久久久久琪琪 | 国产免费一区二区三区 |