如何连接表,以便我在一个表中得到结果

时间:2017-04-27 06:30:56

标签: sql sql-server

客户:

staffID | fname | lname

inStorePay:

inStorePaymnet | hourlyPayment | staffId

如何连接表,以便将结果放在一个表中?

Select hourlyPayment, (Count(hourlyPayment)* 100 / (Select Count(*) From    
  inStorePay)) as rate

  From inStorePay

  Group By  hourlyPayment

   SELECT S.fname,S.lname, I.hourlyPayment

   FROM Staff S, inStorePay I

   WHERE  S.staffID ='S0984'

2 个答案:

答案 0 :(得分:0)

希望此代码可以帮助您

--Here Dbo.[InStorePay](StaffID) column depends on  Dbo.[Customer](StaffID) ,Now you can join above tables  Using StaffID column
    CREATE TABLE [dbo].[Customer](
        StaffID INT IDENTITY, 
        Fname VARCHAR(200),
        Lname VARCHAR(200)
    PRIMARY KEY CLUSTERED 
    (
        [staffID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO


    CREATE TABLE [dbo].[InStorePay](
        InStorePayId INT IDENTITY,
        StaffID INT,
        InStorePaymnet Money,
        HourlyPayment  Money
    PRIMARY KEY CLUSTERED 
    (
        [InStorePayId] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO

    ALTER TABLE [InStorePay] ADD CONSTRAINT Fk_InStorePay_Customer FOREIGN Key (StaffID) REFERENCES Dbo.[Customer](StaffID)

答案 1 :(得分:0)

我希望这是你的要求

SELECT S.fname,S.lname, I.hourlyPayment,(Count(HourlyPayment)* 100 / (Select Count(*) From
#InStorePay)) as rate
FROM #Customer S
INNER JOIN #InStorePay I ON S.StaffID = I.StaffID
WHERE S.staffID = 2
Group By S.fname,S.lname, I.hourlyPayment

上述查询的结果将是这样的

enter image description here