how to write query in sql server in following conditions

Posted by RohiK under Sql Server on 11/10/2011 | Points: 10 | Views : 1479 | Status : [Member] | Replies : 5
Hi,

I have table by name branchname and hits.
suppose i need data like

branchname1 branchname2 branchname3
hits hits hits

how to do that?

means how to create branchname1,branchnae2,s hits calulation in select query?

plz answer me

Happy Programming!!
Rohi



Responses

Posted by: Vuyiswamb on: 11/10/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
IF YOU DESIGNED YOUR DATABASE CORRECTLY , YOU SHOULD BE ABLE TO DO THIS



SELECT BRANCH_NAME ,HITS
FROM BRANCHNAME B
INNER JOIN HITS H
ON B.BRANCHID = H.BRANCHID


Thank you for posting at Dotnetfunda
[Administrator]

RohiK, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: PandianS on: 11/11/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Can you pls give the Table structure along with sample data ?

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

RohiK, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.biswas on: 11/14/2011 [Member] Platinum | Points: 25

Up
0
Down
Try this

Declare @tblBranchname Table(BranchId int identity,BranchName Varchar(50))

Insert Into @tblBranchname
Select 'branchname1' Union All
Select 'branchname2' Union All
Select 'branchname3'

Declare @tblHits Table(BranchId int,Hits int)
Insert Into @tblHits
Select 1,10 Union All
Select 2,12 Union All
Select 1,5 Union All
Select 3,56 Union All
Select 2,5 Union All
Select 1,45

Select
b.BranchName
,TotalHits = Sum(h.Hits)
From @tblBranchname b
Join @tblHits h
On b.BranchId = h.BranchId
Group By b.BranchName


Result

BranchName	TotalHits

branchname1 60
branchname2 17
branchname3 56


Best Regards,
Niladri Biswas

RohiK, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 11/14/2011 [Member] Starter | Points: 25

Up
0
Down
Most simple Open -->Microsoft sql server management studio --->New query -->And write code "Use DatabaseName" then f5


then Top Menu Go to query -->Design Query Editor (ctrl + shift + Q) after pop add tables according to your choice select field . in the botttom you will find code in this way u will able to solve out you r problem foreever.

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

RohiK, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: RohiK on: 11/15/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Actually i need to join multiple table with this requirement.
I got this but I have use different logic.

I create new columns containg column names. and with the select case I fills data into it.

anyway.

thanks to all for reply. I got another ways to solve my problem


Happy Programming!!
Rohi

RohiK, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response