Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2006-07-13 : 14:51:03
|
| Table:SINSID SEID FMID TP--- ---- --- ---126 4000 5107 A115 NULL 5107 B116 NULL 5107 BTable:HITRNRSNP SID ------ ---- 08 116i want the below output.plese help me in writing the queryRNRSNP SID SEID ------ ---- ----- 08 116 4000 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-07-13 : 14:56:34
|
| Could you explain the expected result set? 4000 is in the 126 row, so how does it get associated with the 116 row?Tara Kizeraka tduggan |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-07-13 : 15:25:46
|
| [code]-- prepare test datadeclare @sin table (SID tinyint, SEID smallint, FMID smallint, TP char(1))insert @sinselect 126, 4000, 5107, 'A' union allselect 115, NULL, 5107, 'B' union allselect 116, NULL, 5107, 'B'declare @HIT table (RNRSNP char(2), SID tinyint)insert @HITselect '08', 116-- Do the workSELECT h.RNRSNP, h.SID, z.SEIDFROM ( SELECT s.SID, w.SEID FROM ( SELECT FMID, MAX(SEID) SEID FROM @SIN GROUP BY FMID ) w INNER JOIN @sin s ON s.FMID = w.FMID ) zINNER JOIN @HIT h ON h.SID = z.SID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
humanpuck
Yak Posting Veteran
94 Posts |
Posted - 2006-07-13 : 15:30:06
|
quote: Originally posted by tkizer Could you explain the expected result set? 4000 is in the 126 row, so how does it get associated with the 116 row?Tara Kizeraka tduggan
I agree this is what baffled me...why bother writing a query against the tbls if you just want a specific result set, create a tbl and populate it accordingly. |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2006-07-13 : 21:29:54
|
| Thanks peter !!.I need to implement the same query logic in below stored procedure which i am not able to display the correct output based on SEID.Problem with below procedure when i passing the value SEID parameter the output result is showing 0.Select REARSNPD.DSC 'REARSNPD', IsNull(Counts.REARSNPD_COUNT, 0) As REARSNPD_COUNTFrom REARSNPDLEFT JOIN(SELECT B.DSC, COUNT(A.RSNP) AS REARSNPD_COUNT FROM HIT A RIGHT OUTER JOIN RRNP b ON b.RNPCD = A.RRNP LEFT JOIN CDHC E ON A.SID=E.SID LEFT OUTER JOIN EX C ON C.EXCD=E.BGLEX** INNER JOIN SIN D ON D.SID=A.SID WHERE (@EX=0 OR E.BGLEX=@EX) AND (@FROMDATE IS NULL OR D.SDT Between @FROMDATE And @TODATE) AND (@TODATE IS NULL OR D.SDT Between @FROMDATE And @TODATE) AND (@SEID=0 OR D.SE_ID=@SEID) AND (@ONLYPE=0 OR A.HLT=@ONLYPE) GROUP BY B.DSC) AS Counts On REARSNPD.DSC = Counts.DSC with the inner join in the above query ** (INNER JOIN SIN D ON D.SID=A.SID)it will fetch only matching records.since SEID is null for matching record is displaying 0.I want to display a count a as 1 for spid 116 since its has the value 08 for RNRSNP HIT table.when a parameter is passed with seid 4000.Relation:A FMID can have one A tp with seid and same FMID may have multiple tp B's in the table SIN.The values of SID B's may or may not exists in table HIT. if exists then it will be SID value with RNRSNP value.Need to identify the count based on the table hit. |
 |
|
|
|
|
|
|
|