2016-07-13 92 views
-3

我有這些表:如何連接兩個表並計算值的出現次數?

CREATE TABLE [dbo].[Word] 
(
    [WordId]  UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL, 
    [Name]   VARCHAR (20)  NOT NULL, 
    [Ascii]   AS    (ascii([Name])) PERSISTED, 
    PRIMARY KEY CLUSTERED ([WordId] ASC) 
); 

CREATE TABLE [dbo].[WordForm] 
(
    [WordFormId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL, 
    [WordId]  UNIQUEIDENTIFIER NOT NULL, 
    [SourceId]  INT    DEFAULT ((1)) NOT NULL, 
    CONSTRAINT [PK_WordForm] PRIMARY KEY CLUSTERED ([WordFormId] ASC), 
); 

有沒有我可以連接這兩個表,並顯示計數爲每個不同的ASCII值,其中在第二個表SourceId具有1的值的方法嗎?

要返回類似:

a 25 
b 30 
c 22 
d 10 
+3

這兩種超級基本和簡單或非常不清楚。提供樣本數據 – Amit

回答

1

你的意思是這樣的:

SELECT W.Ascii, COUNT(*) AS [Count] FROM Word W 
INNER JOIN WordForm WF 
ON W.WordId = WF.WordId 
WHERE W.WordId = 1 
GROUP BY W.Ascii 
1

您可以使用內部連接上你的SELECT語句...

SELECT W.Ascii, COUNT(1) 
FROM 
    [dbo].[Word] W 
    INNER JOIN [dbo].[WordForm] WF 
     ON WF.WordId = W.WordId 
WHERE 
    WF.SourceId = 1 
GROUP BY 
    W.Ascii