Join: Conditionally Include Data From Sub Rows?
Nov 2, 2004
Having one invoice table (ord) and one 'person' table (actor) I would like to include the name of the person who is responsible for an invoice:
SELECT Ord.OrdNo, Ord.Selbuy, AC.Nm AS 'Responsible', AC.EmpNo
FROM Ord
LEFT OUTER JOIN Actor AS AC ON Ord.Selbuy = AC.EmpNo
WHERE ord.ordno = 23505
This works perfectly fine if Ord.Selbuy has a corresponding value in Actor:
|26914 |21|Yvonne| 21|
or if there is no corresponding value in Actor:
|26914 |21|NULL| NULL|
But what if Ord.Selbuy=0? Then I end up with 3285 rows from Actor! This happens because Actor.EmpNo=0 is allowed. Persons which have never been employed or used to be employed gets Actor.EmpNo=0.
Can I create a SELECT statement which only returns data from the INVOICE row if Ord.Selbuy=0?
|26914 |21|NULL| NULL|
p.s. I'm not able to change table structure/behavior of update procedures, because the tables/code belong to a "bought from the shelf"-business system.
View 2 Replies
ADVERTISEMENT
Jul 26, 2007
I just spent about 30mins searching through the forums for this and saw several posts, but I didn't find a straight answer that seems like it would work for my report. How can I add only 2 data rows to a group (to hide them via the group's visible properties) and keep the rest of the rows outside of the group, but still in the same column (vertical area), as shown:
This is the view of all rows:
Cat1
Cat2
Product A
Data 1
#
#
Data 2
#
#
Data 3
#
#
Data 4
#
#
Data 5
#
#
Data 6
#
#
Product B
Data 1
#
#
Data 2
#
#
Data 3
#
#
Data 4
#
#
Data 5
#
#
Data 6
#
#
And this is the view I'm seeking in some cases:
Cat1
Cat2
Product A
Data 1
#
#
Data 4
#
#
Data 5
#
#
Data 6
#
#
Product B
Data 1
#
#
Data 4
#
#
Data 5
#
#
Data 6
#
#
View 4 Replies
View Related
Aug 26, 2007
I am building a Table Report where I need to "count" the number of cells in a column conditionally.
I have a column of data where the values will be "Orange", "Apple", "Banana", NULL
The pseudocode would be something like this:
iCountOfOranges as Integer
iCountOfApples as Integer
iCountOfBananas as Integer
IF Cell.Value = "Orange" THEN
iCountOfOranges = iCountOfOranges + 1
ELSE IF Cell.Value = "Apple" THEN
iCountOfApples = iCountOfApples + 1
ELSE IF Cell.Value = "Banana" THEN
iCountOfBananas = iCountOfBananas + 1
The 3 count values would then be displayed in 3 footer rows at the bottom of the table.
Thanks
View 4 Replies
View Related
Aug 8, 2012
This should be a simple solution, but it has been a long time since I've done any query writing (mostly in Oracle) and I am stumped, so here goes:
We are in the process of converting Access database to MSSQL with web form front ends.
I have a table, all columns are nullable, and want users to be able to query from a form, which has a field for each column and defaults to a % wild card for the entered value.
I want the users to be able to put any string in any field, and have it return each row that matches that, including rows with null values in the other columns, but not the column with the entered criteria.
Here is a sample of the data:
Code:
SQL> select * from test;
COL1 COL2 COL3 COL4
----- ----- ----- -----
this is a test
this is not test
this is not
this is test too
is test too
is too
is too
7 rows selected.
Now, if I have this SQL run, it will return only rows that have no nulls in any columns:
Code:
select
col1,
col2,
col3,
col4from test
where
col1 like'th%'
and col2 like '%'
and col3 like '%'
and col4 like '%';
COL1 COL2 COL3 COL4
----- ----- ----- -----
this is a test
this is not test
this is test too
Now, if I use an OR clause for each column, this mostly works, but the trouble is it will also return rows with null values for the field that has criteria entered in it:
Code:
select
col1,
col2,
col3,
col4from test
where
(col1 like'th%' OR col1 is null)
and (col2 like '%' OR col2 is null)
and (col3 like '%' OR col3 is null)
and (col4 like '%' OR col4 is null);
COL1 COL2 COL3 COL4
----- ----- ----- -----
this is a test
this is not test
this is not
this is test too
is test too
is too
is too
The idea is to only select the first 4 rows in the above example.
I was playing with ISNULL in the select clause, but all it does is substitute a string for a null, and I think CASE will do the same thing.
Is there a way I can write this query so it will return rows with NULL values in any column, except the one(column) that has user entered criteria in it?
View 9 Replies
View Related
May 22, 2015
I have a table sorted by vendor, then item, then Status date with a QC Pass date and Fail date.
I need a SQL 2008 view that counts how many consecutive times a Vendor/Item has passed QC.
When it fails, the count resets to zero, then begins incrementing again.
I need to know how to generate the last column (Count).
I have tried using a ROW_Number() OVER(Partition BY, Order By...) command in the view, but I cannot seem to make it work right.
VendorItemStatusDatePassDate Faildate Count
10056322010-05-092010-05-091
10056322012-12-152012-12-152
10056322013-05-252013-05-253
10056322014-11-172014-11-174
[Code] ....
View 5 Replies
View Related
May 11, 2008
I want to retrieve some data in my gridview using a sqldatasource. Here's the idea. My Gridview contains events. A user can subscribe into one of them. When he subscribes, the event must be removed in the gridview. So when there is a subscription from that certain user for that event, it may not appear. Here's my not working code..SELECT Event.EventID,Event.name, Event.LocationID, Event.Date, Event.StatusFROM Shift INNER JOIN Event ON Shift.EventID = Event.EventID INNER JOIN Subscription ON Shift.ShiftID = Subscription.ShiftIDWHERE (Subscription.UserID <> @UserID) Greetz
View 1 Replies
View Related
Jun 2, 2014
Usually it is better to include the columns in the index that are in where clause, select list and join.I am thinking that the columns in the selected list is better to keep as index columns and the columns that are in the where clause is better to keep in key columns.Where do we use join column is it better to create as main key column or included column.
View 4 Replies
View Related
Mar 19, 2007
Hi all,
I am really new to SSIS, so may be this is a really simple question, but I couldnt find an answer yet.
I need to build a package that
1) counts the rows from a view
2) if rowcount >0 extracts the data into a file
I tryed to do this using a Row Count Transformation in the data flow, but after putting the count in a variable I am not able to perform the "conditional" phase two.
I mean that I want to check the value of the variable, but cannot figure out how to conditionally execute the flat file extraction.
Using Row Count, I have to build 2 data flow tasks.
Is there a way to do this in a single data flow?
May be using an Execute SQL Task instead of row count?
Any suggestions will ge greately appreciated
IgorB
View 6 Replies
View Related
Jul 20, 2005
I have about 150 people I would like to send e-mail automatically. Eachperson would get a unique form letter that includes username andpassword I have stored in a SQL table. Is this possible? Helpappreciated. Thanks.Frank*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!
View 1 Replies
View Related
Jun 13, 2007
Hi: I have SQL Server 2005 Express edition and I am trying to generate a script that someone can take and import into the full version of SQL Server. Using the Generate Scripts Option, I have been able to generate scripts for the various SQL statements that I created - but cannot get it so that the data is included. I'm new to this and would appreciate any help. I have a populated database that I would also like to transfer to the new server.Any help greatly appreciated.Roger Swetnam
View 2 Replies
View Related
Oct 2, 2015
Based on a table like below I have created a report so that I can compare number of items in the main warehouse (LOCATION1) and the outlets (LOCATION2 and LOCATION3).
___________________________________
| ID | PRODUCT_INDEX | LOCATION | VALUE |
___________________________________
| 1 | INDEX1 | LOCATION1 | 1 |
___________________________________
| 2 | INDEX1 | LOCATION2 | 1 |
___________________________________
| 3 | INDEX1 | LOCATION3 | 0 |
___________________________________
| 4 | INDEX2 | LOCATION1 | 0 |
___________________________________
| 5 | INDEX2 | LOCATION2 | 0 |
___________________________________
| 6 | INDEX2 | LOCATION3 | 1 |
___________________________________
| 7 | INDEX3 | LOCATION1 | 1 |
___________________________________
| 8 | INDEX3 | LOCATION2 | 0 |
___________________________________
| 9 | INDEX3 | LOCATION3 | 1 |
___________________________________
The way I present data in my Report is as such. I want to show items that are available in the warehouse that should be moved to the outlets.
select
a.PRODUCT_INDEX
, a.LOCATION1(VALUE)
, b.LOCATION2(VALUE)
, c.LOCATION3(VALUE)
from
[Code] .....
__________________________________________________________________
| PRODUCT_INDEX | LOCATION1 (VALUE) | LOCATION2 (VALUE) | LOCATION3 (VALUE)|
__________________________________________________________________
| INDEX1 | 0 | 1 | 0 |
__________________________________________________________________
| INDEX2 | 1 | 0 | 1 |
__________________________________________________________________
| INDEX3 | 1 | 0 | 1 |
__________________________________________________________________
I have added some parameters in my report to filter out products that are not available in warehouse (LOCATION1) and this works great.
select * from VIEW where 'LOCATION1(VALUE)' > 0 and ('LOCATION2(VALUE)' = 0 or 'LOCATION3(VALUE)' = 0)
__________________________________________________________________
| PRODUCT_INDEX | LOCATION1 (VALUE) | LOCATION2 (VALUE) | LOCATION3 (VALUE)|
__________________________________________________________________
| INDEX1 | 1 | 1 | 0 |
__________________________________________________________________
| INDEX3 | 1 | 0 | 1 |
__________________________________________________________________
Now the issue starts when I add a parameter to my report for user to choose which outlets (LOCATIONs) he wants in the equation. I know how to make a column disappear based on parameter value but how to take it out of equation? At the moment when user selects only LOCATION2 and not LOCATION3 then data is not filtered correctly:
__________________________________________________
| JOIN_ON_VALUES | LOCATION1 (VALUE) | LOCATION2 (VALUE) |
__________________________________________________
| INDEX1 | 1 | 1 |
__________________________________________________
| INDEX3 | 1 | 0 |
__________________________________________________
Ideally I would like a user to select random outlets (warehouse would be static on the report) and compare one or multiple and only show records that are 0 in the outlets.
View 2 Replies
View Related
Jun 28, 2006
Hi,
I have a common requirement in numerous SSIS processes to take my main input data set and to remove all rows from it that match a second input data set on a given key and output this as the main output. I also want to output (as a second output) all the rows from the main input data set that did match on the given key. However, I don't want to merge in data from the second input, nor am I interested in rows from the second input data set that have no match in the main input.
E.g. If I have the following data:
Main input:
Key Name
--- ----
1 Steve
2 Jamie
3 Donald
Second Input
Key DontCareAboutThisField1
--- -----------------------
1 ...
3 ...
4 ...
Then I would like the following output:
Main Output
Key Name
--- ----
2 Jamie
Second Output
Key Name
--- ----
1 Steve
3 Donald
Can I do this with a standard transform, or will I have to write my own? Any help on this would be greatly appreciated!
Thanks in advance,
Lawrie
View 1 Replies
View Related
May 14, 1999
Hi,
I forgot the syntax to to a self-join to find all rows that have
duplicate values. Could someone post the answer to the following example:
Column1
-----
x
y
z
x
After the select statment runs "x" would be the output.
Thanks,
Ken
View 2 Replies
View Related
Aug 27, 2001
I have two tables. Table 1 contains a distinct ID(3,4,5). Table 2 contains multiple ID's(3,3,3,3,3,4,4,4,5,5,5). I want to be able to use a join. If an ID is found in table 2, remove all entries of it. Any ideas? Thanks...
delete b.id
from table1 a join table2 b on a.id = b.id
View 1 Replies
View Related
May 22, 2008
Current ResultSet
------------------
ColA ColB
TEST1 111
TEST1 222
TEST2 333
TEST3 444
TEST3 555
I am currently outer-joiing two tables to retrive some data in the above format. My intent is to modify the query so I can retrive the data in below fashion.
Intended ResultSet
------------------
ColA ColB
TEST1 111,222
TEST2 333
TEST3 444,555
Can someone please assist with this? I am not sure if it is possible in a direct query or not... Any expert advice is appreciated.
View 1 Replies
View Related
Jun 21, 2014
This is my sql string. It counts all the rows in Questions table but it should only count the rows where id in Quizzes matches the quiz column in Questions table.
"select Quizzes.name, Quizzes.id, count(Questions.quiz) as total from Quizzes inner join Questions on Quizzes.id=Questions.quiz"
Why isnt it doing what I want it to do?
View 4 Replies
View Related
Jul 29, 2007
Hi, I would appreciate help on the following query I got stuck with;
I've got 2 tables "AccountList" and "PeriodBalance";
I need to return all the accounts form "AccountList" and their balance form "PeriodBalance";
The user will select a period for which the balances should be returned, if there is no balance for the period... 0 should be returned. "PeriodBalance" would not have an entry if there is no balance for the period.
There is only 3 possible periods (1, 2 or 3)
And there is only one balance per period per account;
TABEL 1: AccountList:
AccNo
100
200
300
TABLE 2: PeriodBalance:
AccNo PerID PerBal
100 1 1000
100 2 1750
100 3 1800
300 1 3200
300 3 3500
This is what is what I need returned by the query, assuming we are selecting PerID 2: (WHERE PerID=2)
AccNo PerID PerBal
100 2 1750
200 2 0
300 2 0
I've included ISNULL(PerID,4) = 4 in my WHERE clause...this worked for returning AccNo 200.
My problem is AccNo 300 in not NULL....it has values, just not for the selected PerID...so how do I get AccNo 300 to be included in my Query result?
This is my SQL Query:
SELECT AccountList.AccNo, PeriodBalance.PerID, PeriodBalance.PerBal
FROM AccountList LEFT OUTER JOIN
PeriodBalance ON AccountList.AccNo = PeriodBalance.AccNo
WHERE PeriodBalance.PerID = 2 OR ISNULL(PerID,4) = 4
Thanks in advance;
View 4 Replies
View Related
Mar 20, 2008
I got the following query:SELECT TOP (8) ext.extID, ext.Quote, ext.sourceTitle, ext.extRating, gf_game.gameID, gf_game.catID, gf_game.URL, gf_game.TitleFROM gf_game_ext AS ext INNER JOIN gf_game ON gf_game.gameID = ext.gameIDWHERE (ext.Approved = 1)ORDER BY ext.extID DESC which is e.g. producing this output: 6000 -some text- Title 90 1960 2 tom-cl tom cl5999 -some text- title 90 1960 2 tom-clcl asdf5998 -some text- title 90 1959 2 tom-cl-cl asdfWhat I'd like to do now is to filter out the duplicate GameIDs (= 1960) so that just one unique row with the gameid 1960 is remaining. If I put in a SELECT DINSTINCT TOP(8) it just counts for the table ext, but I need it to count for gf_game.gameID - is that possible?Thanks a lot!
View 9 Replies
View Related
Nov 1, 2005
Hi all,
I am trying to build a association table (t2) to store a list of users
have viewed an item in my records table (t1). My goal is to send the
UserID parameter to the query and return to the user a read / not read
marker from the query so I can handle the read ones differently in my
.net code. The problem is that I cannot work out how to return anything
but the read data to the client. So far my stored proc looks like this
DECLARE @UserID AS Int -- FOR TESTING
SET @UserID = 219 -- FOR TESTING
SELECT t1.strTitle, t1.MemoID, Count(t2.UserID) AS ReadCount,t2.UserID
FROM t1
LEFT OUTER JOIN
t2 ON t1.MemoID = t2.MemoID
WHERE t2.UserID = @UserID
GROUP BY t1.MemoID, t1.strTitle,t2.UserID
It works fine but only returns those records from t1 that are read. I
need to return the records with null values also! I may have built the
assoc table wrong and would really appreciate some pointers on what I
am doing wrong. (assoc table has rID, MemoID and UserID columns)
Please help!
Many thanks
View 2 Replies
View Related
Nov 3, 2005
SELECT
[tblSections].[pageTitle],
[tblSections].[sectionURL],
[tblSectionContents].[articleID],
[tblSectionContents].[fileID],
[tblSectionContents].[linkID],
[tblCopy].[copyText],
[tblFiles].[fileName],
[tblFiles].[fileCaption],
[tblGroupings].[grouping],
[tblLinks].[linkURL]
FROM [tblSections]
LEFT JOIN [tblSectionContents] ON [tblSectionContents].[sectionID] = [tblSections].[id]
LEFT JOIN [tblCopy] ON [tblSectionContents].[articleID] = [tblCopy].[id]
LEFT JOIN [tblFiles] ON [tblSectionContents].[fileID] = [tblFiles].[id]
LEFT JOIN [tblGroupings] ON [tblFiles].[groupingID] = [tblGroupings].[id]
LEFT JOIN [tblLinks] ON [tblSectionContents].[linkID] = [tblLinks].[id]
WHERE [tblSections].[id]=2
ORDER BY
[tblSectionContents].[articleID],
[tblSectionContents].[fileID],
[tblSectionContents].[linkID]
If I pass it the ID of a section that has files or copy or [stuff in other tables] attached, then I get a result set that makes sense.
But if I pass it a section ID that doesn't reference any other content tables (ie: the section just has a title and a link URL), I don't get anything back.
Shouldn't it should still get me the fields from the row in tblSections that matches the ID I'm passing it?
How can I make it so that it does?
Thanks :)
View 3 Replies
View Related
Jan 27, 2014
I am using three tables/view.
The first is a vendor table where I am pulling company/ID/name
I am using a left join to the other two tables on company/ID
The problem I am having is if the second table has 8 rows and the third table just has 1 row it will repeat 8 times.
How do I show all 8 from the second table but only the 1 from the third table?
You can see below that the voucher number and amount repeat
vendor_idvendor_namepo_nopo_amountvoucher_no amount_due
36999VITEK6012838 $174.00 2622666 $(1,791.00)
36999VITEK6016464 $822.90 2622666 $(1,791.00)
36999VITEK6017791 $876.00 2622666 $(1,791.00)
36999VITEK6025495 $600.00 2622666 $(1,791.00)
36999VITEK6029781 $930.00 2622666 $(1,791.00)
36999VITEK6034433 $3,264.00 2622666 $(1,791.00)
36999VITEK6037821 $2,715.00 2622666 $(1,791.00)
View 1 Replies
View Related
Oct 23, 2007
Hi,
I'm having a little trouble with the following code:
SELECT DISTINCT cd1.*, cd2.*
FROM Table1 cd1 LEFT JOIN Table2 cd2
ON cd1.RegNr=cd2.RegNr
WHERE cd1.RegNr = $RegNr
I want it to return the 2 rows that is present in the tables but it returns 4.
1262007-10-20 10:14:00
1262007-10-20 10:14:00
1262007-10-20 10:17:00
1262007-10-20 10:17:00
View 18 Replies
View Related
Feb 24, 2007
I have the following query:
select sq.*, p.numero, p.nombre
from paf p right outer join dbo.GetListOfSquaresForShippingLot(@lot) sq on sq.number = p.numero and sq.version = p.numero
The @lot parameter is declared at the top ( declare @lot int; set @lot = 1; ). GetListOfSquaresForShippingLot is a CLR TVF coded in C#. The TVF queries a XML field in the database and returns nodes as rows, and this is completed with information from a table.
If I run a query with the TVF only, it returns data; but if I try to join the TVF with a table, it returns empty, even when I'm expecting matches. I thought the problem was the data from the TVF was been streamed and that's why it could not be joined with the data from the table.
I tried to solve that problem by creating a T-SQL multiline TVF that is supposed to generate a temporary table. This didn't fix the problem.
What can I do? Does anybody know if I can force the TVF to render its data somewhere so the JOIN works? I was thinking a rowset function could help, but I just can't figure out how.
PLEASE HELP!!!!
Let me know if you want the code for the CLR TVF. This is the code for the T-SQL TVF:
CREATE FUNCTION [dbo].[GetTabListOfSquaresForShippingLot]
(
@ShippingLot int
)
RETURNS
@result TABLE
(
Number int, Version int, Position smallint,
SubModel smallint, Quantity smallint,
SquareId nvarchar(5),
ParentSquareId nvarchar(5),
IsSash smallint,
IsGlazingBead smallint,
Width float,
Height float,
GlassNumber smallint,
GlassWidth float,
GlassHeight float
)
AS
BEGIN
INSERT INTO @result
SELECT *
FROM dbo.GetListOfSquaresForShippingLot(@ShippingLot)
RETURN
END
View 6 Replies
View Related
Apr 24, 2008
Hi,
I have a problem with a Merge Join providing no output (when it should have 1890 rows). My Data Flow Task has 4 OLE Data Sources, 3 Multicasts, and 1 OLE Data Destination. I am experiencing the problem near the end of my data flow where two Multicasts create two parallel flows of data (see Level 1 below). I have two Merge Joins which join one leg from each multicast with a leg from the other multicast (see Level 2 below). Then the two remaining legs use a Merge to get my destination output (see Level 3 below).
I am experiencing my problem with the Merge Join (input A2, B2) --> (output C2) transformation. The Merge Join providing output C1 appropriately outputs 1890 rows, but C2 outputs 0 rows. Both Merge Joins are identical. The data is identically sorted prior to entering the problematic Merge Join and a DataViewer (Grid) verified that the data is appropriately entering in. Merge Join (input A2, B2) --> (output C2) has 667 rows as input A2 and 1890 rows as input B2 (using an inner join, just like the other merge join), but C2 baffles me with 0 rows of output (when it too should have 1890). I receive no Ouput errors and the execution completes showing all green.
Level 1
Multicast (output A1, A2) [667 rows]
Multicast (ouput B1, B2) [1890 rows]
Level 2
Merge Join (input A1, B1) --> (output C1) [1890 rows]
Merge Join (input A2, B2) --> (output C2) [0 rows]
Level 3
Merge (input C1, C2) --> (output D1) [1890 rows]*
I read about mysterious behavior with Merge Joins and have attempted modifying my EngineThreads property to values between 2 and 10, with no luck. Any help/ideas would be appreciated.
Thanks,
Devin
* Should be 3780 rows
View 4 Replies
View Related
Jun 25, 2007
Hi guys,
Just trying to select a set of Articles from a SQL Server Database. The Articles all have a Category ID which is stored in another table (as an Article could be in more than one Category). I want to select the Top 3 Articles in a Category. At the moment I have as my SQL;
"SELECT TOP 3 f.ArticleID, f.Heading, f.Summary, f.WrittenDate, f.ArticleURL FROM feedTable f LEFT JOIN Categories c ON f.ArticleID = c.ArticleID WHERE c.CategoryID=" + CategoryID + " AND c.ArticleID<>" + id + " ORDER BY c.CategoryID"
Which seems to work to an extent in that I do get three articles in the same Category appearing. However, there are sometimes duplicates appearing, so I need to incorporate a DISTINCT clause to the above. I'm not sure where to put this in though. Any ideas?
Thanks.
View 6 Replies
View Related
May 7, 2012
I am using SLQ Server 2008 R2. The database was designed by another company.
I have two tables: Client and Client_Location. In the Client table the pk is Client_ID. There is also a unique key: sys_Client_ID. Both the Client_ID and the sys_Client_ID fields exist as a foreign keys in the Client_Location table. However, the fields are not noted as unique in the Client_Location table. There are two fields in the Client_Location table that determine when the address was effective. They are from_dt and end_dt.
Multiple records have been loaded into the Client_Location table to track old as well as current addresses of clients.
I'm trying to run a report that will pull clients with a plan_id constraint from the Client table and join the Client_Location table to retrieve the current address of these clients.
My SQL is:
select distinct (a.client_id), a.cli_last AS Last_Name,
a.cli_first AS First_Name, a.cli_middle AS Mid_Init,
b.city AS City, b.county AS County, b.state AS State
from ECBH.dbo.tbl_Client a inner join ECBH.dbo.tbl_Client_Location b
on a.client_id = b.client_id
inner join ECBH.dbo.tbl_client_insurance c
on a.client_id = c.client_id
inner join ECBH_TEST.dbo.tbl_GEF_County d
on b.county = d.COUNTY_NAME
where c.plan_id = 4
order by a.cli_last, a.cli_first
Because multiple records exist in the Client_Location table, the result set has duplicates. How can I pull only the results where the from_dt is most recent?
View 5 Replies
View Related
Dec 3, 2013
Here is my query which returns multiple rows
SELECT
R.name, R.age,R.DOB,
ISNULL(D.Doc1,'NA') AS doc1,
ISNULL(C.Doc2,'NA') AS doc2
FROM
REQ R
inner join RES S ON R.Request_Id=S.Request_Id
inner join RES1 D ON D.Response_Id=S.Response_Id
inner join REQ1 C ON C.Request_Id=R.Request_Id
select * from RES1 where Response_Id = 111 -- return 3
select * from REQ1 where Request_Id = 222 --- returns 2
So at last inner join retuns 3*2 = 6 records , which is wrong here and i want to show 3 records in doc1 row and 2 records in doc 2 rows ...
View 5 Replies
View Related
Nov 14, 2015
Please refer to the below query. I want to filter inner join rows based on outer query column value (refer to bold text).
SELECT M.MouldId, SI.StockCode, MI.Cavity, MI.ShotCounter, CQ.SumOfCastedQty as CastedQty, MI.CounterStartup
FROM MouldItem MI
JOIN (SELECT JD.MouldId, JC.StockCode, SUM(JS.CastedQty) AS SumOfCastedQty
FROM JobCasting AS JS INNER JOIN JobCreationDet AS JD ON JS.JobDetId = JD.Uniid INNER JOIN JobCreation AS JC ON JD.JobIdx = JC.Uniid
[Code] ....
View 2 Replies
View Related
Aug 13, 2015
I have to join two tables and i need to fetch All records from @tab2 and only max date record from @tab1 that ID is present in Tab2
1.) @Tab1 have multiple records for each ID
2.) @Tab2 also have multiple records for each ID
3.) Kind of Lef Outer join those tables with ID and take all records from @tab2 and only Max of date from @tab1 and order by ID and Date
Note: @Tab1 always have lesser dates than @tab2 for each ID
Tables looks like as follows
declare @tab1 table (id varchar(3), effDt Date, rate int)
insert into @tab1 values ('101','2013-12-01',5)
insert into @tab1 values ('101','2013-12-02',2)
insert into @tab1 values ('101','2013-12-03',52)
[code]....
In the given ex, ID 103 should not come as it is not present in @tab2, ID 104 should come even it is not present in @tab1 as we ahve to use left outer join Result should like follows.
View 3 Replies
View Related
Nov 19, 2006
I've met a curiouse thing when using full outer join, that I got some rows belong to neither tables @_@
The SQL statement is like this:
SELECT PreviousMonth.InvoiceID, PreviousMonth.CategoryName, PreviousMonth.ProdName, PreviousMonth.Qty, ISNULL(CurrentMonth.SellTotal, 0)
AS SellTotal, ISNULL(PreviousMonth.SellTotal, 0) AS PriorSellTotal
FROM (SELECT SellTotal, InvoiceID, CategoryName, ProdName, Qty
FROM viewProdSales AS viewProdSales_1
WHERE (DateCreated >= @pStartDate) AND (DateCreated <= @pEndDate)) AS CurrentMonth
FULL OUTER JOIN
(SELECT SellTotal, InvoiceID, CategoryName, ProdName, Qty
FROM viewProdSales
WHERE (DateCreated >= @pPriorStartDate) AND (DateCreated < @pStartDate)) AS PreviousMonth
ON
CurrentMonth.InvoiceID = PreviousMonth.InvoiceID
The result include some added rows, if I query the 2 sub table seperately, neither contains such rows.
For example, if I pass 01/09/2006, 01/10/2006 and 31/10/2006 as pPriorStartDate, pStartDate and pEndDate, to query the sales statistics in Sept and Oct, I would get some reords in June or even earlier... Anyone know about this? Thanks in advance.
Summer
View 3 Replies
View Related
Dec 25, 2005
Hello,
I have a survey (30 questions) application in a SQL server db. The application uses several relational tables. The results are arranged so that each answer is on a seperate row:
user1 answer1user1 answer2user1 answer3user2 answer1user2 answer2user2 answer3
For statistical analysis I need to transfer the results to an Excel spreadsheet (for later use in SPSS). In the spreadsheet I need the results to appear so that each user will be on a single row with all of that user's answers on that single row (A column for each answer):
user1 answer1 answer2 answer3user2 answer1 answer2 answer3
How can this be done? How can all answers of a user appear on a single row
Thanx,Danny.
View 1 Replies
View Related
Apr 6, 2006
Environment:
Running this code on my PC via VS 2005
.Net version 2.0.50727 on the server (shown in IIS)
Code is in ASP.NET 2.0 and is a VB.NET Console application
SSIS 2005
Problem & Info:
I am bringing in an Excel file. I need to first strip out any non-detail rows such as the breaks you see with totals and what not. I should in the end have only detail rows left before I start moving them into my SQL Table. I'm not sure how to first strip this information out in SSIS specfically how down to the right component and how to actually code the component to do this based on my Excel file here: http://www.webfound.net/excelfile.xls
Then, I assume I just use a Flat File Source coponent or something to actually take the columns in the Excel and split into an OLE DB Datasource to shove each column into a corresponding column in my SQL Server Table. I have used a Flat File Source in the past to do so with a comma delimited txt file but never tried with an Excel.
Desired Help:
How to perform
1) stripping out all undesired rows
2) importing each column into sql table
View 1 Replies
View Related
Jan 26, 2008
I have 4 tables (SqlServer2000/2005). In the select query, I have FULL JOINED all the four tables A,B,C,D as I want all the data. The result is as sorted by DDATE desc:-
AID BID BNAME DDATE DAUTHOR
1 1 abcxyz 2008-01-20 23:42:21.610 c@d.com
1 1 abcxyz 2008-01-20 23:41:52.970 a@b.com
1 2 xyzabc 2008-01-21 00:17:14.360 c@d.com
1 2 xyzabc 2008-01-20 23:43:17.110 a@b.com
1 2 xyzabc 2008-01-20 23:42:43.937 a@b.com
1 2 xyzabc NULL NULL
2 3 pqrlmn NULL NULL
2 4 cdefgh NULL NULL
Now, I want unique rows from the above result set like :-
AID BID BNAME DDATE DAUTHOR
1 1 abcxyz 2008-01-20 23:42:21.610 c@d.com
1 2 xyzabc 2008-01-21 00:17:14.360 c@d.com
2 3 pqrlmn NULL NULL
2 4 cdefgh NULL NULL
I want to remove the duplicate rows and show only the unique rows but contains all the data from the first table A. I have to bind this result set to a nested GridView.
View 8 Replies
View Related