Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Query syntax help

 
   Database Forums (Home) -> MSEQ RSS
Next:  Help: changing file options open/close tab  
Author Message
Newbie

External


Since: Mar 16, 2007
Posts: 2



(Msg. 1) Posted: Thu Nov 15, 2007 11:57 am
Post subject: Query syntax help
Archived from groups: microsoft>public>sqlserver>mseq, others (more info?)

I am trying to write a query that returns all suppliers within a given range
that either do not have any insurance (appear only in Suppliers table) or
Suppliers where the insurance has expired from a given date

Eg 4 Suppliers
Supplier1 - no insurance
Supplier2 - insurance expired
Supplier3 - insurance current
Supplier4 - not in range

The Supplier range is 'where AccRef like '^SC%'
The Expiry Date is less than or equal to '20071130'

The result set would include Supplier1 because it is not in the
InsuranceDetails table at all and Supplier2 because the insurance has
expired.

How can I do this in one query?

I have included some SQL for your info

Thanks

A

CREATE TABLE [dbo].[Suppliers](

[AccRef] [nvarchar](Cool NOT NULL,

[AccName] [nvarchar](30) NOT NULL

) ON [PRIMARY]



CREATE TABLE [dbo].[InsuranceDetails](

[AccRef] [nvarchar](Cool NOT NULL,

[DateExpire] [datetime] NOT NULL

) ON [PRIMARY]

INSERT INTO dbo.Suppliers ([AccRef], [AccName])
SELECT '^SC100' As Expr1, 'Supplier1' as Expr2
INSERT INTO dbo.Suppliers ([AccRef], [AccName])
SELECT '^SC200' AS Expr1, 'Supplier2' as Expr2
INSERT INTO dbo.Suppliers ([AccRef], [AccName])
SELECT '^SC300' AS Expr1, 'Supplier3' as Expr2
INSERT INTO dbo.Suppliers ([AccRef], [AccName])
SELECT '10000' AS Expr1, 'Supplier4' as Expr2


INSERT INTO dbo.InsuranceDetails ([AccRef], [DateExpire])SELECT '^SC300' AS
Expr1, '20080331' as Expr2
INSERT INTO dbo.InsuranceDetails ([AccRef], [DateExpire])SELECT '^SC200' AS
Expr1, '20071101' as Expr2

 >> Stay informed about: Query syntax help 
Back to top
Login to vote
Anith Sen

External


Since: Feb 17, 2004
Posts: 280



(Msg. 2) Posted: Thu Nov 15, 2007 11:57 am
Post subject: Re: Query syntax help [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

There are a variety of ways you can do this. Here is one:

SELECT s1.accref, s1.AccName
FROM suppliers s1
WHERE s1.accref LIKE '^SC%'
AND COALESCE(
( SELECT i1.dateexpire
FROM InsuranceDetails i1
WHERE i1.accref = s1.accref ), '19000101' )
<= '20071130' ;

--
Anith

 >> Stay informed about: Query syntax help 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Syntax for query to another SQL-Server-Instance - Hi, what is the correct syntax to execute a query to a table on another SQL-Server-Instance. For example: dbo.table on Server1 with Instance2 (Server1\Instance2) Thanks in advance F. L.

Join syntax? - I'm not sure if this is a join problem or even if it can be done. The english part of the query will be requesting cagegoires="Cat2" and return the matching records in table1, plus return all the other categories that belong to the set in ta...

Get user syntax - I am building a view for a report and one of the requirements is to grab the user who is running the report. What would this syntax look like? I thought it was getuser(), but no luck there. Thanks, Ryan

SQL Syntax Question - I have the following syntax: "select itemnmbr, locncode, (qtyonhnd-atyalloc) as "QTY Available Main" from iv00102 " The results look like; Itemnmbr locncode qty available main 121 944-16 ...

Quarterly Data Syntax - I am trying to build a query that will return what quarter the data is in. How would I begin to build this logic? In my table, all I have is a single date of the transactions. My date column looks like this; "2014-02-01 00:00:00.000". T...
   Database Forums (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]