| Author |
Topic |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2006-07-28 : 11:56:47
|
| I need display the counts from table @AYT based on the parameters declare @FRMDSDT DATETIMEdeclare @TOSDT DATETIMEdeclare @X INTdeclare @PEID INTdeclare @OPT INTSET @FRMSDT ='2006-07-29 00:00:00.000' SET @TOSDT ='2006-07-30 00:00:00.000' SET @X =01SET @PEID =9497SET @OPT =01I hope that my question is clear.Below the sample data.Declare @Pin table (PID tinyint, PEID smallint, FMID smallint, TP char(1),SDT datetime)insert @Pinselect 120, 9497, 5107, 'A','2006-07-29 00:00:00.000' union allselect 121, NULL, 5107, 'B','2006-07-30 00:00:00.000'union allselect 122, NULL, 5107, 'B','2006-07-30 00:00:00.000' union allselect 123, NULL, 5107, 'B','2006-07-30 00:00:00.000' *(PID PrimaryKey)Declare @HIT table (PID tinyint, OPT tinyint )insert @HITselect 122,01declare @GX table (PID tinyint,x tinyint)insert @GXselect 120,01 Declare @AYT table (PID tinyint,AYTCD varchar(5))insert @AYT select 121,'15.15' UNION ALLselect 121,'15.14' UNION ALLselect 121,'15.13'Output: [AYTCD] [AYTCDCOUNT] ------- ----------- 15.15 1 15.14 1 15.13 1 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-07-28 : 12:05:55
|
quote: Originally posted by sqldba2k6 I need display the counts from table @AYT based on the parameters declare @FRMDSDT DATETIMEdeclare @TOSDT DATETIMEdeclare @X INTdeclare @PEID INTdeclare @OPT INTSET @FRMSDT ='2006-07-29 00:00:00.000' SET @TOSDT ='2006-07-30 00:00:00.000' SET @X =01SET @PEID =9497SET @OPT =01I hope that my question is clear.
Which Question ?Also what u asked for as Output is not seems to be related to more than 80% of the information u provided.!!Srinika |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-07-28 : 12:21:07
|
| [code]Select AYTCD, Count(AYTCD) as AYTCDCountfrom @AYT group by AYTCD[/code]Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2006-07-28 : 13:21:56
|
| Thanks for the response..I need to display the data counts from AYT table.. with the following input parameters @FRMDSDT : (@Pin table, column SDT)@TOSDT :(@Pin table, column SDT)@X :(@GX table, column x)@PEID :(@pin table,column PEID) @OPT :(@HIT table,column OPT)with values to the parametersSET @FRMSDT ='2006-07-29 00:00:00.000' SET @TOSDT ='2006-07-30 00:00:00.000' SET @X =01SET @PEID =9497SET @OPT =01I need the below output.[AYTCD] [AYTCDCOUNT]------- -----------15.15 1 15.14 115.13 1 I hope that my question is clear. |
 |
|
|
nosepicker
Constraint Violating Yak Guru
366 Posts |
Posted - 2006-07-28 : 14:37:09
|
| Based on the sample data you've provided, there is no logical way to relate the data to get the output you want. For example, for the @AYT table, you've only provided sample data for PID=121. But the PEID you provided is for 9497, which only relates to PID=120 in the @Pin table. Therefore, you can't relate those records in any natural way. You have either provided erroneous sample data, erroneous parameters, or you are trying to create relationships that don't logically exist. |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2006-07-28 : 15:08:25
|
| The sample data which provided is correct.If you look the data in @pin table fmid is unique for the pied and pid.In other way.I am trying to achieve the below results:AYTCD PID PEID OPT x 15.15 121 9497 01 0115.14 121 9497 01 0115.13 121 9497 01 01 |
 |
|
|
nosepicker
Constraint Violating Yak Guru
366 Posts |
Posted - 2006-07-28 : 17:54:33
|
| OK, I came up with this, which seems to work, but it required making relationships between data that was entirely unnatural and illogical (if this is really the data, it needs to be normalized badly):SELECT @ayt.AYTCD, B.* FROM @ayt JOIN (SELECT @pin.PID, A.maxPEID, A.maxOPT, A.maxX FROM @pin FULL JOIN (SELECT @pin.FMID, MAX(@pin.PID) AS maxPID, MAX(@pin.PEID) AS maxPEID, MAX(@hit.OPT) AS maxOPT, MAX(@gx.x) AS maxXFROM @pin LEFT JOIN @hit ON @pin.PID = @hit.PID AND @hit.OPT = CONVERT(int, @OPT) LEFT JOIN @gx ON @pin.PID = @gx.PID AND @gx.x = CONVERT(int, @X) WHERE @pin.SDT >= @FRMSDT AND @pin.SDT <= @TOSDT AND @pin.PEID = @PEID GROUP BY @pin.FMID) AS A ON @pin.FMID = A.FMID) AS B ON @ayt.PID = B.PID |
 |
|
|
samuelclay
Yak Posting Veteran
71 Posts |
Posted - 2006-07-28 : 18:04:03
|
Wow, sqldba2k6.. did you even try to run the code you provided? You changed the name of the first variable between declaring it and setting it.. And I'm not sure what you mean by saying the FMID is unique when every row has the same one.. unique usually means only one row would match...The row of data in the @pin table for PID=121 has a PEID of null, you would not get the results you display in your last post...selecting all the data joined together gives this:SELECT p.*, h.OPT, g.x, a.AYTCD FROM #pin p LEFT OUTER JOIN #HIT h ON p.pid = h.pid LEFT OUTER JOIN #GX g ON p.pid = g.pid LEFT OUTER JOIN #AYT a ON p.pid = a.pidPID PEID FMID TP SDT OPT x AYTCD ---- ------ ------ ---- ----------------------------- ---- ---- ----- 120 9497 5107 A 2006-07-29 00:00:00.000 NULL 1 NULL121 NULL 5107 B 2006-07-30 00:00:00.000 NULL NULL 15.15121 NULL 5107 B 2006-07-30 00:00:00.000 NULL NULL 15.14121 NULL 5107 B 2006-07-30 00:00:00.000 NULL NULL 15.13122 NULL 5107 B 2006-07-30 00:00:00.000 1 NULL NULL123 NULL 5107 B 2006-07-30 00:00:00.000 NULL NULL NULL There are no rows that match your parameters (@x=1, @PEID=9497, @OPT=1) |
 |
|
|
samuelclay
Yak Posting Veteran
71 Posts |
Posted - 2006-07-28 : 18:40:15
|
IF you assume the AYTCD table is the primary table, and the parameters are optional, you could go this route : SELECT a.AYTCD, Count(A.AYTCD) as AYTCDCountFROM #AYT a LEFT OUTER JOIN #HIT h ON a.pid = h.pid LEFT OUTER JOIN #GX g on a.pid = g.pid LEFT OUTER JOIN #PIN p on a.pid = p.pidwhere (SDT >= @FRMSDT and SDT <= @TOSDT)OR h.OPT = @OPTOR g.x = @XOR p.PEID = @PEIDgroup by a.AYTCD you end up with:AYTCD AYTCDCount ----- ----------- 15.13 115.14 115.15 1 |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2006-07-31 : 14:27:44
|
| Thanks Nosepicker.I got the desired when i removed or commented out the parameters condition in the query but when i tried with input parameters i am not able to get the results getting nulls for all max cols.Can you please help me in this issue...Thanks for your help in advance... |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2006-07-31 : 15:13:40
|
| Nosepicker!I am able to pull the data with the query but not with the filter resultsFor Ex:if i tried with @PEID =9497 i have to get only the results of 9497..I hope i am clear with my question |
 |
|
|
|