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

SQL:計算兩個不同表中的兩個不同列

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

問題描述

我正在嘗試獲取兩個不同表的資源列的不同計數,然后顯示每個項目 ID 的比較.現在,此查詢為我提供了兩個表的相同計數值.

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

推薦答案

當然你會得到同樣的計數,你正在計算同一個表的列(它是由一個連接產生的,授予,但它仍然是一個長方形的桌子).

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).

您想要做的是使用子查詢.首先獲取每個項目 id 的列表(從一個表中,或解析兩個相關表的聯合,但這是數據庫規范化不良的標志),然后獨立查詢這些表的計數:

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

這篇關于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 在线| 午夜三区 | 国产乱码精品一品二品 | 精品成人av | 国产成人精品在线 | 国产日韩欧美精品 | 亚洲一区二区免费电影 | 精品视频在线一区 | 久久久久久久国产 | 国产婷婷色一区二区三区 | 国产一级片 | 日韩成人免费av | 久久成人18免费网站 | 国产在线一区二区三区 | 黄色片在线观看网址 | 99久久精品国产毛片 | 青娱乐国产 | 亚洲精品中文在线观看 | 久久久久国产一区二区三区 | 中国美女撒尿txxxxx视频 | 欧美一区二区三区在线看 | 日韩免费一区二区 | 羞羞色在线观看 | av网站观看| 国产精品免费视频一区 | 成人二区 | 国产精品一区一区三区 | 亚洲v日韩v综合v精品v | 日韩有码一区 | 亚洲视频免费在线观看 | 久久一区二区三区电影 | 一级做a爰片性色毛片视频停止 | 成人在线观看免费爱爱 | 成人国产一区二区三区精品麻豆 | 亚洲免费人成在线视频观看 | 色综合99 | 久久国内精品 | 日韩一二区在线观看 | 欧美精品一区二区在线观看 | 一区二区手机在线 |