Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MS SQL Server 2005 - Basic sql query

Posted on 2012-09-21
7
Medium Priority
?
725 Views
Last Modified: 2012-09-23
Hi and thanks,

Just looking for a basic query:

Example:
Query1:
Select
[IDNumber]
[Units]
From [dbo].[Table1]
GO

Query2:
Select
[IDNumber]
[loanNumber]
From [dbo].[Table2]
GO

So what I want to do is:
Select
[Units]
From [dbo].[Table1] Inner Join [dbo].[Table2]
Where [dbo].[Table1].[IDNumber] = [dbo].[Table2].[IDNumber] AND loanNumber = 50 to 59
GO

Already you can see the syntax errors?
just trying to learn SQL Query in SSMS.

please help and thanks
0
Comment
Question by:Amour22015
7 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 600 total points
ID: 38421951
SELECT units
FROM table1
   JOIN table2 ON table1.IDNumber = table2.IDNumber
WHERE loadnumber BETWEEN 50 and 59

Couple of questions:
(1)  Which table does units belong to?  table1.Units or table2.Units
(2)  The above assumes loanNumber is numeric.  If not, you wont be able to do a range line '50 to 59'
(3)  Above assumes dbo is the default schema
(4)  Not sure what the relevance is if your Query1 and Query2 example
0
 
LVL 1

Assisted Solution

by:akq-HH
akq-HH earned 400 total points
ID: 38421956
somthing like this .....

SELECT Units,loadNumber
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.IDNumber = t2.IDNumber
WHERE t2.loanNumber between 50 and 59
0
 
LVL 2

Expert Comment

by:Brainfeb
ID: 38421983
Try this ,

Select
[Units]
From [dbo].[Table1] Inner Join [dbo].[Table2]
On  [Table1].[IDNumber] = [Table2].[IDNumber]
where  loanNumber = 50 to 59

or you can try this too
 
  Select [Units]
  from  [dbo].[Table1] A ,[dbo].[Table2] B
  where A.[IDNumber]=[IDNumber]
  and B.loanNumber = 50 to 59
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 66

Expert Comment

by:Jim Horn
ID: 38421993
(5)  Also, where you have table1.IDNumber and table2.IDNumber, you'll have to make sure we're talking the same value.  For example, if Table 1 is for Orders, and Table2 is for Customers, you'll want to compare Orders.CustomerID to Customer.CustomerID, and not Orders.OrderID to Customers.CustomerID.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 38422060
BrainFeb,

you can not do filter in this way "loanNumber = 50 to 59"
0
 

Author Closing Comment

by:Amour22015
ID: 38422134
Thanks for helping.

Brainfeb,
Brichsoft is correct I already tried this:
"loanNumber = 50 to 59"
and had syntax errors
0
 
LVL 2

Expert Comment

by:Brainfeb
ID: 38427712
Sorry it was my mistake

you can give the condition like this BETWEEN 50 and 59

it was happened while doing the copy paste.Sorry once again.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question