Join 2 Complex Queries To 1
Jul 20, 2005
hi there
anyone had an idea to join following 2 queries to 1????
----- QUERY 1 ---------------------------------------------
SELECT TMS_CaseF_2.Name AS TCDomain_0,
TMS_CaseF_3.Name AS TCDomain_1,
TMS.CaseF.Name AS TCFolder_2,
TMS_CaseF_1.Name AS TCFolder_3,
TMS.TestCase.Name AS TCName_4,
TMS_TestCase_1.Name AS TCName_5,
TMS.LogFolder.Name AS PlannedLogFolder_6,
TMS.Log.Name AS PlannedLog_7,
TMS.CaseResult.RecordedCaseName AS TCRecordedName_8,
TMS.TestPlan.Name AS Plan_9
FROM
(
(
(
(
(
(
(
(
(
(TMS.Build INNER JOIN TMS.LogFolder ON TMS.Build.UID =
TMS.LogFolder.Build)
INNER JOIN TMS.Log ON TMS.LogFolder.UID = TMS.Log.LogFolder
)
INNER JOIN TMS.CaseResult ON TMS.Log.UID = TMS.CaseResult.Log
)
INNER JOIN TMS.TestCase ON TMS.CaseResult.TestCase =
TMS.TestCase.UID
)
LEFT JOIN TMS.CaseF ON TMS.TestCase.Parent = TMS.CaseF.UID)
LEFT JOIN TMS.TestCase AS TMS_TestCase_1 ON TMS.TestCase.Parent =
TMS_TestCase_1.UID
)
LEFT JOIN TMS.CaseF AS TMS_CaseF_1 ON TMS_TestCase_1.Parent =
TMS_CaseF_1.UID
)
LEFT JOIN TMS.CaseF AS TMS_CaseF_2 ON TMS_CaseF_1.Parent =
TMS_CaseF_2.UID
)
LEFT JOIN TMS.CaseF AS TMS_CaseF_3 ON TMS.CaseF.Parent =
TMS_CaseF_3.UID
)
INNER JOIN TMS.TestPlan ON TMS.TestCase.TestPlan = TMS.TestPlan.UID
)
WHERE (((TMS.LogFolder.Name) Like 'TR1%')
AND ((TMS.Build.Name)='Planning_VD10A'))
ORDER BY TMS.CaseF.Name,
TMS_CaseF_1.Name,
TMS.TestCase.Name,
TMS_TestCase_1.Name;
-------------------------------------------------------------
----- QUERY 2 ---------------------------------------------
SELECT TMS.CaseResult.RecordedCaseName
FROM ((TMS.Build INNER JOIN TMS.LogFolder ON TMS.Build.UID =
TMS.LogFolder.Build)
INNER JOIN TMS.Log ON TMS.LogFolder.UID = TMS.Log.LogFolder)
INNER JOIN TMS.CaseResult ON TMS.Log.UID = TMS.CaseResult.Log
WHERE (((TMS.LogFolder.Name) Like 'VD%')
AND ((TMS.Build.Name)='VD10A IT_APP'));
View 1 Replies
ADVERTISEMENT
Apr 6, 2008
Dear All,
I want to practise writing complex T sql queries in SQl Server 2005 which involve several joins, sub-queries in a single query.
This kind of queries are commonly required in Reporting scenarios where a single report may have to be prepared from 2,3 or more tables.
Presently I am able to write queries of moderate complexity.
Therefore I am looking for web tutorials, books which contains some solved examples as well as some Unsolved ones. The Unsolved ones should have the answers given so that one comes to know if he makes a mistake. The web tutorials can be from paid sites as well.
If any of you have a good collection of such queries then pls be kind enough to send it to me.I will give my email id for the purpose. If anybody can help in some other way pls let me know.
Your help will be highly appreciated.
Thanks .
View 3 Replies
View Related
Feb 21, 2007
SQL writes "Hi,
i am getting to trip with SQL Server and i find it is quite interesting.
please send me complex TSQL queries which would help me to test my SQL server expertise.
do let me know what are the URLs where i can find complex TSQL Queries
i appreciate pointers in right direction
Thanks in advance"
View 1 Replies
View Related
Oct 3, 2007
Alright just starting out in ASP.NET and it's making my head spin, but I think I'm getting it.My schema in brief : Images, Categories, and a table in the middle so we can have a many-to-many relationship between images and categories. My issue is searching. I can search by keyword, and limit it to categories. So, in pseudo sql.. SELECT ... WHERE keyword LIKE '%test%' and category_id in (1,5,20,66); I made a variable for the keyword no problem. But how can I get this dynamic list? Or, is there another way about going this problem?
View 6 Replies
View Related
May 23, 2008
Hello Friends. I have few doubts regarding the use of Sqldatasource server control. Is the control flexible enough to achieve these things:- 1.To perform complex queries using Sqldatasource. For eg:- using JOIN to retrieve/insert data from/into multiple tables.2.Performing cascading dropdownlist using sql database as datasource.3.Performing For loops eg:-populating the multiple items into the listbox from sqldatasource. and retrieving them at once.4.use ajax update panel.etc.any example/sample on how to achieve these task?thanks.jack.
View 3 Replies
View Related
Dec 11, 2007
I'm dealing with queries that manages a lot of data consolidation and I'm having to repeat large blocks of code to get the desired result.
I'm wondering if that is any way to make it simple.
My queries looks like this:
Code BlockSELECT
(A + B) AS C,
(D + E) AS F,
(G + H) AS I,
(A + B) * (D + E) AS total1,
(D + E) * (G + H) AS total2,
(A + B) * (G + H) AS total3,
(A + B) * (D + E) * (G + H) AS total
FROM table
Can I somehow declare each part of the expression and make it something like this?
Code Block
SELECT
(A + B) AS C,
(D + E) AS F,
(G + H) AS I,
C * F AS total1,
F * I AS total2,
C * I AS total3,
C * F * I AS total
FROM table
Maybe it seems silly, but each sub-expression, on the real life, includes a lot of CASEs and aggragate functions and my codes are getting complex and confuse.
Since I have lots of code pieces that I repeat more than once, I would like to make it simpler.
tks for any help
View 5 Replies
View Related
Jun 9, 2008
Hi All -
Any recommendations for good advanced t-sql books/articles? I find myself involved with writing increasingly more complex queries and after spending a few hours on some, and then searching on this site for potential answers/help, I am wondering if there might be some good books on creating more advanced/complex t-sql for real world scenarios.
Thanks
- will
View 3 Replies
View Related
Dec 13, 2007
I am not very good in queries. Could you please suggest me some web site/Tutorial/Artical where i can get Study Material for complex and real life queries. I know the syntexes, I just need to practice queries to enhance my skills
View 1 Replies
View Related
Sep 7, 2003
table_a has patient_id, tran_id and other fields a,b,c
table_b has patient_id,tran_id, key_id
and other fileds d,e,f
table_a patien_id + tran_id is unique
table_b patient_id + tran_id is not unique, could be duplicated.
I have to create a query which will retrieve fields from table a a,b,c and fields d,e from table b where
table a. patient_id + tran_id =
tableb.patient_id + tran_id and table_b.key_id is the min key_id for that patient_id + tran_id.
I should retrieve just one record.
How would I be able to do that?
Please help!!
Thanks
View 2 Replies
View Related
Aug 26, 2005
Hey all,
I have 3 tables: 1 table with Module information (1 quiz-like part of an Exam), 1 table with the relationships between Modules and Exams (ratios) and 1 table with User information, that contains the Exam number, Module number, and score. I am trying to get a full list of modules' names for 1 exam, and the user's score for those modules they taken. All this would pertain to 1 single Exam. I am only getting records returned for those modules that the user has taken; I want the full list with NULL values for the scores of modules the user has not taken. Hope that made sense, here is my attempt:
Code:
SELECT ExamModules.Name, ExamUsers.Score
FROM ExamToModule LEFT OUTER JOIN
ExamModules ON ExamToModule.ModuleID = ExamModules.ID LEFT OUTER JOIN
ExamUsers ON ExamToModule.ModuleID = ExamUsers.MID AND ExamToModule.ExamID = ExamUsers.EID
WHERE (ExamToModule.ExamID = 1) AND (ExamUsers.UserName = 'dizzle')
In this case the user is Dizzle and the Exam's ID (primary key) is 1. I've changed LEFT to RIGHT and FULL, they all return the same few records. Help?
View 4 Replies
View Related
Jun 27, 2007
I need a little help and I hope I can write a clear description of the problem. I have to tables, customers and custrate. The customer table simply has a custid of other misc info. The custrate contains an entry for each rate for any particular customer (1 customer to many rates). However the rates are suppossed to come in pairs. For example, if a customer has a rate id of 120 then the customer should also have a corresponding custrate record with a rate_id of 200. If the customer has a rate_id of 130, then the customer should have a corresponding custrate record containing rate_id 300, and so on and so forth. How do I find the customer id with are missing the corresponding rate? Here's an example or what I'm trying to explain.customerid | name1 | fred 2 | tom3 | eric 4 | fredcustratecust_id | rate_id1 | 1201 | 2002 | 1302 | 3003 | 1204 | 130I looking for the sql statement that would return customer id of 3 and 4, since those are the only 2 records that don't have a corresponding rate. I have hunch that it requires a intra table join, but maybe i'm totally wrong. Can anyone help me please? I would greatly appreciate it!
View 8 Replies
View Related
Oct 16, 2006
This is a very complex query and i have tried everything with no sucess.
I'm having 3 Tables,
Orders which is having fields like CustomerID, ManufactureID, MerchID :- These all ID Fields (around 6) are foreign key of Contacts and Address Tables.
Address Table is having AddrID(Primary Key), ShortName
Contacts is having ContID (Primary Key), AddrID (Foreign Key), PersonName
I want to retreieve info in a single query which can return a single row with following columns :-
OrderNo, CustomerName, ManufactureName, MerchName etc. (all 6 columns) by joining these 3 tables
can anybody help in this.
View 5 Replies
View Related
Apr 23, 2008
If i want to do another join with this select join query, how would I do this I tried many different way, but keep failing or not getting the right answer.
I want to Inner Join tbSucess with the query below. Means that only return the result if the history does exists in tbSucess.
And
In a seperate select query.
I, I want to get only the records which are not found in tbSuccess . So how would I join those two.
Select r.History, LastName, FirstName, Convert(varchar(10), Pay, 101) as Pay,
from #rpt r inner join #history h on r.History = h.History
order by r.History, pay
View 8 Replies
View Related
Mar 3, 2008
Hi guys, I'm still new around here and still a noob for sql. Can you give me some example for some joins from the easy ones to the most complex and all of it kinds, I heard that there's a lot of different kind of join in sql. You can give me some link or maybe some code examples for me, I appreciate all kinds of help . Thanks for all your help. Regards.
View 3 Replies
View Related
Nov 3, 2004
I have a couple tables that I am exporting data from.
I have one table (call it TABLE1) that contains abbreviations instead of actual words.
For example instead of having the full word "New York City", the column would just contain "NYC".
In another table (call it TABLE2) it has all the abbreviations and the actual words.
So in TABLE2 one column name is FieldName, another Abbreviation and another is Value.
So on a web site, to display that actual name I do something like this
Code:
SELECT CityBoxlabel AS City
FROM TABLE1 INNER JOIN
TABLE 2 AS CityBox ON ISNULL(TABLE1.City, 'NYC') = CityBox.[Value]
WHERE CityBox.FieldName = 'City'
This is working great except that some of these columns contain more than one abbreviation such as "NYC,WDC"
which would stand for "New York City" and "Washington DC"
The items that have more than one abbreviation are not being pulled across because my query is looking for an "NYC,WDC" in TABLE2 to INNER JOIN on but it won't be in there. But "NYC" and "WDC" by themselves are in there.
I tried messing with the INNER JOIN statement by saying something like
TABLE2 AS CityBox ON ISNULL(TABLE1.City, 'NYC') IN CityBox.[Value] INNER JOIN
But it wasn't allowing that.
I can write a program to do this, but I would rather keep it in T-SQL.
Hope this wasn't too confusing!
Any help is greatly appreciated!!
View 5 Replies
View Related
Apr 27, 2008
Table A
Registation_key discharge_dt
1001 7/15/07
1002 6/4/07
1003 7/30/071
Table B
REgistration_key ID end_dt value
1001 879 07/11/07 1
1001 888 07/15/07 2
1002 777 06/01/07 5
1002 778 06/01/07 2
1002 779 06/30/07 6
1003 234 07/31/07 3
1003 234 07/31/07 1
1003 235 08/01/07 7
I am trying to join Table A to Table B using the below log . Table A should have one unique mathincg record from Table B
option 1. Using registration_key and discharge_dt , looking for exact matching date( end_dt) in table B , if there is more than one record that matches then select lowest ID
option 2.if there is no record that matches option1 then, serch for the previous record with end_dt in table A less than end_dt table B. if there is more than one record then select lowest ID
option 3. if there is no record option 2 then search the next record that matches .. discharge_dt greater than end_Dt , if there is more than one record then select lowest Id
so basically, I am looking for an exact matching date in the same registration_key .. if the exact dt doesn't exist looking for the previous record and get the most closer dt and if there is no prvious record than look for next record,.
so the output should look like
Table B
Registration_key ID end_dt discharge_dt value
1001 888 07/15/07 7/15/07 2
1002 778 06/01/07 6/4/07 2
1003 234 07/31/07 8/1/07 7
How can I do this conditional join ?
View 6 Replies
View Related
Mar 23, 2015
I have Two tables @master and @child
Master Table :
MasterID EntryNumber BranchId IsstockIn
1 1 1 1
2 1 1 0
Child Table:
CEntryNumber CBranchID EntryQty
1 1 10
1 1 20
1 1 -5
1 1 -4
My Query:
Select SEC.EntryQty from Item.StockEntryChild SEC
where SEC.CEntryNo =
(
select SEM.EntryNumber from item.StockEntryMaster SEM
where SEC.CBranchID=SEM.BranchID and SEC.CEntryNo=SEM.EntryNumber and SEM.MasterID=1 and SEM.isStockIn=1
)
My Result:
EntryQty
10
20
-5
-4
Expected Result:
10
20
View 6 Replies
View Related
Feb 8, 2008
I have a very confusing/complicated query that I am trying to run and getting not the results that i want.
Essentially I have three tables (t1, t2, t3) and I want to select data from two of them, but there are conditions on the data where I need fields to match across pairs of tables.
When I run my select query I am getting far too many records - it's giving me all possible combinations, instead of the proper combinations that I want.
Select t1.*, t3.field2, t3.field3
FROM, t1, t2, t3WHERE t2.field4=t3.field4 AND t1.field5=x AND t1.field6=t2.field6
I suspect there is plenty wrong with this query - where should I start correcting it?
View 10 Replies
View Related
Aug 25, 2005
Here is the situation
Table 1 : tbl_documents
docIDdocName
1aaa
2bbb
3ccc
Table 2 : tbl_Rating
ratIDratingdocID
131
251
321
432
The queary I need is to display the result in this form. must be like this
docIDdocName Avaragerating
1aaa3
2bbb3
3ccc0
NOTE : For getting the average I used this queary “SELECT SUM(rating) As
RatingSum, COUNT(*) As RatingCount FROM tbl_Rating WHERE tbl_rating.docID =
tbl_documents.docID”
PLs help me ?
Thx
View 3 Replies
View Related
Feb 7, 2006
I had this (what seems to be a) simple question asked today and I'm afraid I didn't like my answer. Does anyone know the proper answer to this one:
Any ideas on how I can constrain a lookup or merge join based on the dimension row's effective and expired dates so three criteria are needed as follows:
1. DataStagingSource.ModifyDate < DataWarehouseDimension.RowExpiredDate AND
2. DataStagingSource.ModifyDate >= DataWarehouseDimension.RowEffectiveDate AND
3. DataStagingSource.NaturalKey = DataWarehouseDimension.NaturalKey
-- Brian
View 3 Replies
View Related
Sep 11, 2007
Hi,
I have a complex join filtering on a replicated sql server database which was working fine in previous versions of sql compact. The query is something like the following:
SELECT <published columns> FROM <filtered table> INNER JOIN <child table> ON <child table>.ID = <filtered table>.ID and <child table>.date > getdate()-30
After I upgraded to compact databse 3.5, for some weird reason whichever tables have both these Join filter and article filter together behaving improperly. If I insert any row in any of these table, the row is replicated properly to the server, but it does not send the new row to any other users. Again this thing works fine in older version. I have switched back tyo the old version of sql ce and again it's started working.
View 5 Replies
View Related
May 13, 2015
I have a few tables I am trying to join to create a report. Everything was working fine until I tried to add an aggregate Sum function to a column (MaxCap) in table ctfBarn.Â
select
x.*, y.division, y.department, y.location
,(right(z.SvcMgrName,len(z.SvcMgrName)-len(left(z.SvcMgrName,CHARINDEX(', ',z.SvcMgrName)-1))-2)+' '+
left(z.SvcMgrName,CHARINDEX(', ',z.SvcMgrName)-1))AS SvcMgrName
,(right(z.SrSvcName,len(z.SrSvcName)-len(left(z.SrSvcName,CHARINDEX(', ',z.SrSvcName)-1))-2)+' '+
[Code] .....
I think I probable need to include a group by but can't figure out the correct syntax.
View 12 Replies
View Related
Dec 6, 2005
Hello:
What is the difference between:
select * from table1, table2 where ......
And
select * from table1 Inner Join table2 ....
Is there a performance difference or any other difference?
thanks
View 3 Replies
View Related
Jun 18, 2004
Hi,
Can you give the equivalent query for the following Oracle query :
SELECT col2, col3 FROM submission,
lkup so
WHERE so.txt (+) = TRIM(col2)
ANDso.pid (+) = 'SO'
ANDso.cde (+) = 'RW'
In the above query a constant value is used in JOIN, so I am not aware of how to use the JOIN clause for the above constant value ( 'SO' , 'RW' )
Upto my knowledge about JOIN query equivalents :
Oracle query :-
select *from test1 a, test2 b where a.sno(+) = b.col
Equivalent SQL Server query :-
select *from test1 a JOIN test2 b on a.sno = b.col
Hope the above is correct. Here my doubt is, if in the above query, b.col is replaced by a constant value say 'abcd' , then what will be the table name used in right of the JOIN clause. Or any other equivalent for this is available ?
Eg :-
Oracle query :-
select *from test1 a, test2 b where a.sno(+) = 'abcd'
Thanks,
Sam
View 3 Replies
View Related
Jun 18, 2004
Hi,
Can you give the equivalent query for the following Oracle query :
SELECT col2, col3 FROM submission,
lkup so
WHERE so.txt (+) = TRIM(col2)
AND so.pid (+) = 'SO'
AND so.cde (+) = 'RW'
In the above query a constant value is used in JOIN, so I am not aware of how to use the JOIN clause for the above constant value ( 'SO' , 'RW' )
Upto my knowledge about JOIN query equivalents :
Oracle query :-
select *from test1 a, test2 b where a.sno(+) = b.col
Equivalent SQL Server query :-
select *from test1 a RIGHT JOIN test2 b on a.sno = b.col
Hope the above is correct. Here my doubt is, if in the above query, b.col is replaced by a constant value say 'abcd' , then what will be the table name used in right of the JOIN clause. Or any other equivalent for this is available ?
Eg :-
Oracle query :-
select *from test1 a, test2 b where a.sno(+) = 'abcd'
Thanks,
Sam
View 1 Replies
View Related
Oct 10, 2006
Hi,
First, my knowledge of MDX is very limited :o
I am wondering if it is possible to return the result of 2 different queries as one. Similarly, like using UNION in SQL. I looked into the MDX UNION but it works on sets.
Basically, I have 2 queries; one with territory sales and one with region sales. I would like to return the results of all the territories followed by the sales of the region in one go.
Thanks in advance.
View 5 Replies
View Related
Jul 9, 2013
I am trying to do an inner join on two sub-queries, and I can't figure out what I am doing wrong here. I keep getting syntax errors:
Code:
SELECT * FROM
(SELECT SSN
FROM [1099_PER]
INNER
JOIN ( SELECT SSN, name4, count(*) from [1099_PER]
group by SSN, name4
[Code] .....
View 3 Replies
View Related
Mar 3, 2008
I get a wo_ID and want to query company data. I have the following tables, work_orders (has wo_ID, wo_name and install_id), install_ids (has install_id, comp_id) and customers (comp_id, company_name). The query process goes like this: I get a wo_ID and I need to find the install_id that corresponds with that wo_ID in that same work_orders table. Then take that install_id and find out the comp_id that corresponds with that install_id from the install_ids table. Finally take that customer_id and link it to comp_id in the customers table to find out company data. I need to join it multiple times to get my final data. I have a simple one that works right now with one join and using the install_id, but I don't know how to expand it with multiple joins. Thanks.
View 4 Replies
View Related
Mar 21, 2004
I have a page that will require several hundred update queries to be sent to the database. How much of a performance increase will i get by joining them all into one statement and sending them as a batch instead of running them one by one?
Thanks.
View 5 Replies
View Related
Jul 20, 2005
I am having a problem with a query,I am not sure if i would use a join or a subquery to complete thisproblem.I have two queries, and i need to divide one by the other, but i cantseem to get anytype of join to work with them.Here is the situation.I have a projectDB table that has a list of different projects foreach employee to work on.Each project has an employee assigned to it.The start date is null until the employee starts to work on it.I want to find how many percent of all their projects that eachemployee is working on.In other words:I want to divide query A by query B to see how many percent ofprojects each employee is working on.Query A count of projects that are being worked because they have adate per employee:SELECT employee, COUNT(employee) AS cntFROM projectDBGROUP BY employee, project_start_dateHAVING (NOT (project_start_date IS NULL)) //notice the NOTQuery B: Total amount of project per employee:SELECT employee, COUNT(employee) AS cntFROM projectDBGROUP BY employee, project_start_dateAny ideas?
View 4 Replies
View Related
Jan 25, 2008
Hello all,
I have been using T-SQL for a while now although the majority of my work required relativley simple queries.
I just need to know is there a way to JOIN the results of several SELECT queries, maybe through the use of functions??
A reference to any online article would be most helpful.
Cheers,
Sean
View 6 Replies
View Related
Jul 24, 2015
I have recently started working on Sql server management studio. I have been using MS access in the past. To link results of 2 queries in MS access I would open the query wizard and it would show me the list of saved queries and then I could join them as regular tables. Im trying to look for this option in management studio. When I open query designer in management studio I am only given the option to add existing tables, how can I add existing queries?
View 2 Replies
View Related
Oct 24, 2007
Good morning!
Or good "whatever daytime you read this"!
SQL Server 2005 has this nice new feature Common table expression or CTE, which allows quite easy to define a "drill down" in recursive self join tables.
By recursive self join tables I mean this common example:
idPerson INT <--------|
idReportsTo INT ---------|
PersonName VARCHAR
A CTE to "go down" the tree from any entry point and find all subs to a parent entry is well documented. I managed to make myself a CTE and use it a lot!
What I find myself needing too often is:
a) Look up from a deep position and find the entry that is for example 3 steps above my reference in the branch
b) Look up from a deep position and find the one that is 2nd or 3rd level (absolute) from top of the tree in the branch
I did try quite some versions, but I cannot get it to work. Any idea how you do the "drill up" with a CTE or another SQL solution.
Of course performance is always needed, so I'd like to avoid the cursors I got it working with and use now. (It is not working good I admit...)
Cheers!
Ralf
View 7 Replies
View Related