Showing posts with label joins. Show all posts
Showing posts with label joins. Show all posts

Wednesday, March 28, 2012

outher joins formats

Hi

I work ith sql server 2000 and i need know the diferent
of joins in format not ansi ( with * ) and joins in format
ansi ( with 'outher join on' ).

Two format work equal ?

What is de correct format ?

Thank you.

R.ragaza@.ozu.es (raulgz) wrote in message news:<9b551742.0403040208.51bb41d1@.posting.google.com>...
> Hi
> I work ith sql server 2000 and i need know the diferent
> of joins in format not ansi ( with * ) and joins in format
> ansi ( with 'outher join on' ).
>
> Two format work equal ?
>
> What is de correct format ?
>
> Thank you.
>
> R.

You should always use the ANSI syntax, because the 'old style' joins
are unclear, and some queries cannot be written at all with them. In
addition, Microsoft may stop supporting the old syntax in a future
version of MSSQL.

In an outer join, you need to separate the join condition (in the JOIN
clause) from the filter conditions (in the WHERE clause) to make sure
you get consistent results. In the old style join, all the conditions
are together in the WHERE clause, so the database engine has to
'guess' at what you meant, and the results may not be what you want.

This KB article shows the difference:

http://support.microsoft.com/defaul...0&Product=sql2k

Simon|||Read more about this by downloading SQL books online free from
http://www.microsoft.com/sql

INNER JOIN FORMATS (only identical rows)

select *
from tablea, tableb
where tablea.id = tableb.id

select *
from tablea inner join tableb
on tablea.id = tableb.id

RIGHT OUTER JOIN FORMATS (all rows on right, shows null on left where no
match)

select *
from tablea, tableb
where tablea.id =* tableb.id

SELECT *
from tablea right outer join tableb
on tablea.id = tableb.id

LEFT OUTER JOIN FORMATS (all rows on left, shows null on right where no
match)

select *
from tablea, tableb
where tablea.id *= tableb.id

SELECT *
from tablea left outer join tableb
on tablea.id = tableb.id

FULL OUTER JOIN FORMATS ( shows null where no match)

Select *
from tablea, tableb
where tablea.id FULL OUTER JOIN tableb.id

Select *
from tablea, tableb
where tablea.id FULL JOIN tableb.id

CROSS JOINS (cross product, all possible combinations)

select *
from tablea, tableb

select *
from tablea cross join tableb

****************************************
Andy S.
MCSE NT/2000, MCDBA SQL 7/2000
andymcdba1@.NOMORESPAM.yahoo.com

Please remove NOMORESPAM before replying.

This posting is provided "as is" with
no warranties and confers no rights.

****************************************

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Outer Joins in Report Model

Hi,
I already posted that in the MSDN technical forums... But I hope that here
are some other experts perhaps knowing what to do...
Just imagine you have two tables, i.e. customers and notes. You can have one
or more notes per customer. Your database has "clean" primary and foreing
keys defined for that scenario. You build a report model on it. The roles
are detected correctly, so there is a "optional many" cardinality on the one
side and a "one" cardinality on the other. However if you use this in a
report and grab the SQL using the SQL Profiler you'll see that an inner join
is used instead of an outer join (because you defined "OPTIONAL many" in the
model).
So what is this?
Please mark the correct answer:
( ) My fault (explain below)
( ) Bug
( ) Feature (explain below)
_______________________________________________________
Thanks ;-)
ThomasIt's pretty ambiguous whether an INNER JOIN or OUTER JOIN should be used,
without knowing the subject matter being reported.
Consider the case of a referral bonus report, where any employee can make a
referral. That's an optional condition, but you would only want those
employees who made the referral to appear. So there has to be some
discretion left to the report developer.
--
Cheers,
'(' Jeff A. Stucker
\
Senior Consultant
www.rapidigm.com
"Thomas Pagel" <thomas.pagel@.community.nospam> wrote in message
news:O4cMFut5FHA.2888@.tk2msftngp13.phx.gbl...
> Hi,
> I already posted that in the MSDN technical forums... But I hope that here
> are some other experts perhaps knowing what to do...
> Just imagine you have two tables, i.e. customers and notes. You can have
> one or more notes per customer. Your database has "clean" primary and
> foreing keys defined for that scenario. You build a report model on it.
> The roles are detected correctly, so there is a "optional many"
> cardinality on the one side and a "one" cardinality on the other. However
> if you use this in a report and grab the SQL using the SQL Profiler you'll
> see that an inner join is used instead of an outer join (because you
> defined "OPTIONAL many" in the model).
> So what is this?
> Please mark the correct answer:
> ( ) My fault (explain below)
> ( ) Bug
> ( ) Feature (explain below)
> _______________________________________________________
>
> Thanks ;-)
>
> Thomas
>|||Jeff,
but why do you have cardinality attributes like "optional many"? I think
that defines exactly what you want! And why isn't that used? And your case
would be handled by a simple filter...
Thanks,
Thomas
"Jeff A. Stucker" <jeff@.mobilize.net> wrote in message
news:%23sIqudv5FHA.1276@.TK2MSFTNGP10.phx.gbl...
> It's pretty ambiguous whether an INNER JOIN or OUTER JOIN should be used,
> without knowing the subject matter being reported.
> Consider the case of a referral bonus report, where any employee can make
> a referral. That's an optional condition, but you would only want those
> employees who made the referral to appear. So there has to be some
> discretion left to the report developer.
> --
> Cheers,
> '(' Jeff A. Stucker
> \
> Senior Consultant
> www.rapidigm.com
> "Thomas Pagel" <thomas.pagel@.community.nospam> wrote in message
> news:O4cMFut5FHA.2888@.tk2msftngp13.phx.gbl...
>> Hi,
>> I already posted that in the MSDN technical forums... But I hope that
>> here are some other experts perhaps knowing what to do...
>> Just imagine you have two tables, i.e. customers and notes. You can have
>> one or more notes per customer. Your database has "clean" primary and
>> foreing keys defined for that scenario. You build a report model on it.
>> The roles are detected correctly, so there is a "optional many"
>> cardinality on the one side and a "one" cardinality on the other. However
>> if you use this in a report and grab the SQL using the SQL Profiler
>> you'll see that an inner join is used instead of an outer join (because
>> you defined "OPTIONAL many" in the model).
>> So what is this?
>> Please mark the correct answer:
>> ( ) My fault (explain below)
>> ( ) Bug
>> ( ) Feature (explain below)
>> _______________________________________________________
>>
>> Thanks ;-)
>>
>> Thomas
>|||Hi Thomas,
Welcome to MSDN Managed Newsgorup!
I understood you concern is why inner join is used instead of outer join
since "optional many" defined. If I have misunderstood your concern, please
feel free to point it out.
Yes, I have tested it on my side and I believe this is a "by design"
feature of Report Model.
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================This posting is provided "AS IS" with no warranties, and confers no rights.|||Michael,
thanks for the reply... That's really bad news and I really don't understand
what the use of the cardinally attribute is if it's not used...
Thomas
"Michael Cheng [MSFT]" <v-mingqc@.online.microsoft.com> wrote in message
news:tu4xGR26FHA.1236@.TK2MSFTNGXA02.phx.gbl...
> Hi Thomas,
> Welcome to MSDN Managed Newsgorup!
> I understood you concern is why inner join is used instead of outer join
> since "optional many" defined. If I have misunderstood your concern,
> please
> feel free to point it out.
> Yes, I have tested it on my side and I believe this is a "by design"
> feature of Report Model.
>
> Sincerely yours,
> Michael Cheng
> Microsoft Online Partner Support
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> =====================================================> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>|||Hi Thomas,
I have submit this to our development team via internal channel. If there
is any update, I will reply here.
You are also encouraged to submit this feeling to the MSDN Product Feedback
Center below
http://lab.msdn.microsoft.com/productfeedback/default.aspx
Thanks so much for your feedback and question.
Sincerely yours,
Michael Cheng
Microsoft Online Partner Support
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================This posting is provided "AS IS" with no warranties, and confers no rights.

Outer joins fail since adding replication

I have an application that has been using a right outer join that worked as I
expected until I added merge replication. On the replicated database the
result is the same as an inner join. I could be doing something odd but it's
a pretty plain use and it still works on instances of the database without
replication. I thought I'd first ask if this is a known issue before creating
small standalone files for posting.
Thanks
Kohn,
I see no reason how this could possibly be an issue. Please can you post up
your files and I'll try to repro.
Cheers,
Paul Ibison SQL Server MVP, www.replicationanswers.com
(recommended sql server 2000 replication book:
http://www.nwsu.com/0974973602p.html)

Outer Joins and Substring Error

My problem exists on the last line - TOAD doesn't seem to like the two sets of parenthesis together. Am I supposed to put something in between? It's OK in the line above because the substring is on the other side.

SELECT distinct fp.active_fl, fp.empl_id, fp.proj_mgr_name, pm.project, fp.subctr_id, rd.CONT_CRNCY
FROM planner.rbws_proj_mgr pm, finance.fin_proj fp, finance.rbws_detl rd
WHERE SUBSTR(fp.proj_id, 1, 9) = pm.project (+)
AND pm.PROJECT = substr(rd.PROJ_ID,1,9) (+)Operative word being "think", I don't think that any version of TOAD can use Oracle outer join syntax in Microsoft SQL Server.

-PatP|||Oracle 8i or less no doubt

SELECT DISTINCT fp.active_fl
, fp.empl_id
, fp.proj_mgr_name
, pm.project
, fp.subctr_id
, rd.CONT_CRNCY
FROM planner.rbws_proj_mgr pm
, finance.fin_proj fp
, finance.rbws_detl rd
WHERE SUBSTR(fp.proj_id, 1, 9) *= pm.project
AND pm.PROJECT *= substr(rd.PROJ_ID,1,9)

Is that the old SQL Server syntax? I thankfully don't remember and have no desire to look it up...speaking of desiree...where is she?

Where is my Desire?

Outer Joins

I have a set of data which needs to be put in a table in addition to dates
(daily). There are some dates where a location could be closed and hence no
data values but i still want to see the date row in the table with location
and all other associated details but NULL for visit counts.
I have tried LEFT OUTER / RIGHT OUTER but failed. Is there an easy way to do
this ?
Thanks
AsimAsim wrote:
> I have a set of data which needs to be put in a table in addition to
> dates (daily). There are some dates where a location could be closed
> and hence no data values but i still want to see the date row in the
> table with location and all other associated details but NULL for
> visit counts.
> I have tried LEFT OUTER / RIGHT OUTER but failed. Is there an easy
> way to do this ?
> Thanks
> Asim
You need to post table DDL and sample data.
David Gugick
Quest Software
www.imceda.com
www.quest.com|||It is very hard to debug code that you cannot see :)|||It sounds like you want the output to include information (dates)
that are nowhere to be found in your data, as if you were to query
an empty sales table and expect a result with some dates.
If you want data in your result, the data has to be in the source
of the query. A calendar table might be what you want in this
case:
select
C.theDate,
A.Location,
A.otherStuff
from Calendar as C
left outer join AsimTable as A
on A.dt >= C.theDate
and A.dt < C.theDate + 1
See http://www.aspfaq.com/show.asp?id=2519
Steve Kass
Drew University
Asim wrote:

>I have a set of data which needs to be put in a table in addition to dates
>(daily). There are some dates where a location could be closed and hence no
>data values but i still want to see the date row in the table with location
>and all other associated details but NULL for visit counts.
>I have tried LEFT OUTER / RIGHT OUTER but failed. Is there an easy way to d
o
>this ?
>Thanks
>Asim
>|||Sorry guys!
Here is the code, the first temp table gets all the cost center, procedures
and etc and then the second table gets each date (D_DateMonth) .
DECLARE @.Procs TABLE
(ProcedureCode varchar(11), CostCtr varchar(16), CcName varchar(31),
StatCode varchar(11), LocationID varchar(30),
SubGrouping varchar(30), [Grouping] varchar(30), Service varchar(50), Campus
varchar(30), SiteGroup varchar(50))
INSERT INTO @.Procs
SELECT ProcedureCode, CostCtr, CcName, StatCode, LocationID, SubGrouping,
[Grouping], Service, Campus, SiteGroup
FROM D_Procedures
GROUP BY ProcedureCode, CostCtr, CcName, StatCode, LocationID,
SubGrouping, [Grouping], Service, Campus, SiteGroup
SELECT dbo.D_Date_month.[Date] as DOM, dbo.TitleCase(a.Service) as
Service,a.Campus as Hospital,dbo.TitleCase(a.SiteGroup) as Location,
a.CostCtr as CostCtr,dbo.TitleCase(a.CcName) as CcName,
SUM(CASE WHEN OC1_Txns.TransactionCount < 0 THEN -1 ELSE 1 END) as
Visits,NULL AS BudgetVisits,
NULL AS VisitVar, NULL AS LastFYVisits, NULL AS LastFYVar, NULL AS Charges,
NULL AS LastFYCharges,
NULL AS ChargesVar,Convert(smalldatetime, GetDate()) as RowUpdatedatetime
FROM dbo.D_Date_month LEFT OUTER JOIN OC1_Txns ON OC1_Txns.BatchDateTime
= dbo.D_Date_month.[Date]
INNER JOIN @.Procs a ON OC1_Txns.TransactionProcedure = a.ProcedureCode
WHERE (Left(a.StatCode,1) = 'V'
OR a.StatCode='FAMPLAN' OR a.StatCode='MIDWIFE' OR a.StatCode='ORTHOOTH')
AND (OC1_Txns.BatchDateTime >= '04/01/2005' AND OC1_Txns.BatchDateTime <
'04/03/2005')
GROUP BY dbo.D_Date_month.[Date],a.Service, a.Campus, a.SiteGroup,
a.CostCtr, a.CcName
ORDER BY dbo.D_Date_month.[Date],a.Service, a.Campus, a.SiteGroup,
a.CostCtr, a.CcName
"--CELKO--" wrote:

> It is very hard to debug code that you cannot see :)
>|||The C table has no dates.. The information is based on visit level in that
table and I need to build the daily visits grouped by service, location and
etc and even if there was no visit on a particular date for a service or
locaiton, i want to see that service or location with a null for visits but
the date.
"Steve Kass" wrote:

> It sounds like you want the output to include information (dates)
> that are nowhere to be found in your data, as if you were to query
> an empty sales table and expect a result with some dates.
> If you want data in your result, the data has to be in the source
> of the query. A calendar table might be what you want in this
> case:
> select
> C.theDate,
> A.Location,
> A.otherStuff
> from Calendar as C
> left outer join AsimTable as A
> on A.dt >= C.theDate
> and A.dt < C.theDate + 1
> See http://www.aspfaq.com/show.asp?id=2519
> Steve Kass
> Drew University
> Asim wrote:
>
>|||Why not fold the first query into the second one instead of creating a
physical table? Why are you doiing formatting in the query instead of
enforcing this in the DDL? Why do data elements keep changing names
from table to table?
My quick guess is that it might look like this:
SELECT D.foobar_date, A.service, A.campus, A.site_group), A.cost_ctr,
A.cc_name,
SUM(CASE WHEN T.transaction_count < 0
THEN -1 ELSE 1 END) AS visits,
CURRENT_TIMESTAMP
FROM D_Date_Month AS M
LEFT OUTER JOIN
Oc1_Txns AS T
ON T.batch_datetime = D.foobar_date
INNER JOIN
(SELECT DISTINCT procedure_code, cost_ctr, cc_name, stat_code,
location_id, subgrouping, grouping, service, campus, site_group
FROM D_Procedures) AS A
ON T.procedure_code = A.procedure_code
WHERE (LEFT(A.stat_code, 1) = 'V'
OR A.stat_code IN ('FAMPLAN', 'MIDWIFE', 'ORTHOOTH'))
AND T.batch_datetime BETWEEN '2005-04-01 00:00:00' AND '2005-04-03
23:59:59.9999'
GROUP BY D.foobar_date, A.service, A.campus, A.site_group, A.cost_ctr,
A.cc_name;|||On 27 May 2005 11:34:09 -0700, --CELKO-- wrote:
(snip)
>AND T.batch_datetime BETWEEN '2005-04-01 00:00:00' AND '2005-04-03
>23:59:59.9999'
Hi Joe,
This is a bad modification from the original code, for several reasons.
First, the constant '2005-04-03 23:59:59.9999' will not convert to a
datetime value, since you specify one decimal place too much.
Second, the format you used is not safe. It could be interpreted as
yyyy-mm-dd or yyyy-dd-mm, depending on regional settings. The only safe
formats in SQL Server are:
* yyyymmdd (date only - note: no interpunction)
* yyyy-mm-ddThh:mm:ss.ttt (date plus time - note the dashes in the date
part, the colons and decimal point in the time part and the capital T
that seperates the parts. Also note that the milliseconds (the .ttt
part) is optional).
Third, if you change it to '2005-04-03T23:59:59.999', it will be
converted to the fourth of april, midnight. You should use either
'2005-04-03T23:59:59.997' (if the column has datatype datetime) or
'2005-04-03T23:59:00' (if it is smalldatetime). And you must change it
if the column's datatype changes, or if MS changes the precision of
either the datetime or the smalldatetime datatype.
Of course, just using T.batch_datetime < '20050404' (almost the same as
the original code, only the date format is changed!) is much easier and
much safer.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

Outer Joins

I am trying to create an outer join between two tables in a query that
includes several other tables.

When I double-click on the Join line, it presents three join options:

1) ONLY records from table1 and table2 where join fields are equal
2) ALL values from table1 and ONLY records from table2 where join
fields are equal
3) ALL values from table2 and ONLY records from table1 where join
fields are equal

In my case, I want option 2 - all values from table1, and if there is
no match to table2, I want a blank to appear in the output.

When I select this option, I get the following error:

"Can't have outer joins if there are more than two tables in the
query."

How can I get around this, since there are other tables in my query?

Thanks.

Dennis Hancy
Eaton Corporation
Cleveland, OHThis sounds like a limitation of the GUI application you are using to create
the query. Use Query Analyzer instead. Perhaps you can paste your SQL query
into QA and edit it.

Alternatively, if you are creating a view, Enterprise Manager's view
designer doesn't suffer from this particular restriction but it does have
other limitations so Query Analyzer is probably the best choice if you're
willing and able to write the query yourself.

--
David Portas
SQL Server MVP
--

Friday, March 23, 2012

Outer Join and Inner Join

Hi
I have been intrigued a bit with the terminology of outer and inner join.
I have been using outer joins as a.orderid=b.orderid(+) which means, that even if table b doesnt have the columns corresponding to orderids in a, it would still return the request row.

I dont know what is the inner join ?? Why and how is it used, and what is the case where inner join comes in handy.
Thanx and Regards
AruneeshOriginally posted by aruneeshsalhotr
Hi
I have been intrigued a bit with the terminology of outer and inner join.
I have been using outer joins as a.orderid=b.orderid(+) which means, that even if table b doesnt have the columns corresponding to orderids in a, it would still return the request row.

I dont know what is the inner join ?? Why and how is it used, and what is the case where inner join comes in handy.
Thanx and Regards
Aruneesh
Inner join is just a regular join, without the (+).