| Author |
Topic |
|
kodumudisadha
Starting Member
33 Posts |
Posted - 2009-05-09 : 10:16:36
|
| hi all,in this below query for one cm_number multiple PLU_CPT will be there.for example cm_number plu_cpt amt--------- -------- ----a 1 100a 2 50a 3 200b 2 500b 3 600then below query will return 3 rows.i need to return like thisthat is for every cm_number i have to cross join the plu_cpt.here cm_number will go n number of records but plu_cpt will repeat between 1 and 10 only.cm_number plu_cpt count amt--------- -------- ----- ----a 1-2 2 150a 1-3 2 300a 2-3 2 250b 2-3 2 1100select storename,CM_NUMBER,cm_date,PLU_CPT,count(distinct CM_NUMBER),sum(plu_price)from dbo.POSSALESTRNDETAILS d,crmstore swhere d.storeid=s.storeid and cm_date between '04-apr-07' and '04-apr-07'group by storename,CM_NUMBER,cm_date,PLU_CPTi need query some thing like this.i dont knou how to group this so this is retriving like like 1,2,3...how to do please...helpcm_number,(case when PLU_CPT=1 then '1'when PLU_CPT=2 then '2'when PLU_CPT=3 then '3'when PLU_CPT=4 then '4'when PLU_CPT=5 then '5'when PLU_CPT=6 then '6'when PLU_CPT=7 then '7'when PLU_CPT=8 then '8'when PLU_CPT=9 then '9'when PLU_CPT=10 then '10'when PLU_CPT=1 and PLU_CPT=2 then '1-2'when PLU_CPT=1 and PLU_CPT=2 then '1-2'when PLU_CPT=1 and PLU_CPT=2 then '1-2'----) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-09 : 10:32:36
|
| [code]SELECT t.cm_number,cast(t.plu_cpt as varchar(2))+ '-' + cast(t1.plu_cpt AS varchar(2)) AS plu_cpt,2,t.amt+t1.amt AS amtFROM YourTable tCROSS APPLY (SELECT * FROM YourTable WHERE cm_number=t.cm_number AND plu_cpt<> t.plu_cpt )t1[/code] |
 |
|
|
kodumudisadha
Starting Member
33 Posts |
Posted - 2009-05-09 : 10:53:41
|
| hi,its showing the following errorSELECT t.cm_number,cast(t.plu_cpt as varchar(2))+ '-' + cast(t1.plu_cpt AS varchar(2)) AS plu_cpt,2,t.amt+t1.amt AS amtFROM POSSALESTRNDETAILS tCROSS APPLY (SELECT * FROM POSSALESTRNDETAILS WHERE cm_number=t.cm_number AND plu_cpt<> t.plu_cpt )t1Server: Msg 170, Level 15, State 1, Line 5Line 5: Incorrect syntax near 'APPLY'.Server: Msg 170, Level 15, State 1, Line 9Line 9: Incorrect syntax near 't1'. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-09 : 11:42:05
|
Cross apply is a feature of SQL Server 2005. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-09 : 11:43:57
|
quote: Originally posted by visakh16
CROSS APPLY (SELECT * FROM YourTable WHERE cm_number=t.cm_number AND plu_cpt <> t.plu_cpt )t1
AND plu_cpt > t.plu_cpt E 12°55'05.63"N 56°04'39.26" |
 |
|
|
kodumudisadha
Starting Member
33 Posts |
Posted - 2009-05-09 : 11:59:27
|
| hi friends,i am using 2000 version ,in this how to achieve this scenario ,please help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-09 : 12:31:15
|
| [code]SELECT t.cm_number,cast(t.plu_cpt as varchar(2))+ '-' + cast(t1.plu_cpt AS varchar(2)) AS plu_cpt,2,t.amt+t1.amt AS amtFROM YourTable tJOIN YourTable t1ON t1.cm_number=t.cm_numberAND t1.plu_cpt> t.plu_cpt[/code] |
 |
|
|
kodumudisadha
Starting Member
33 Posts |
Posted - 2009-05-09 : 13:12:18
|
| hi thanks for all,i need another help ,i want to join all plu_cpt for one cm_number that is one row for one cm_number like this how to do pls helpa 1-2-3 350b 2-3 1100 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-09 : 13:25:49
|
for that first create a UDF like thisCREATE FUNCTION ConcatValues(@cm_number char(1))RETURNS varchar(8000)ASBEGINDECLARE @Result varchar(8000)SELECT @Result=COALESCE(@Result,'') + CAST(plu_cpt as varchar(5)) + '-'FROM YourTableWHERE cm_number=@cm_numberRETURN @ResultENDthen use it like belowSELECT cm_number,dbo.ConcatValues(cm_number) AS plu_cpt,amtFROM(SELECT cm_number,SUM(amt) AS amtFROM YourTableGROUP BY cm_number)t |
 |
|
|
kodumudisadha
Starting Member
33 Posts |
Posted - 2009-05-09 : 23:11:48
|
| Dear visak,thank you very much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-10 : 02:12:21
|
you're welcome |
 |
|
|
kodumudisadha
Starting Member
33 Posts |
Posted - 2009-05-11 : 08:23:39
|
| hi, i need last one help in function ConcatValues i want to get which plu_cpt is more than 1,i want to ignore single plu_cpt for one cm_numberi tried SELECT @Result= COALESCE(@Result,'') + CAST( plu_cpt as varchar(5)) + '-'FROM (select distinct plu_cpt from POSSALESTRNDETAILSWHERE cm_number=@cm_number and cm_date=@cm_date and storeid=@storeid)t1group by plu_cpthaving count(plu_cpt)>1but its showing plu_cpt null for all records |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-11 : 10:32:34
|
do you mean this? make concat values like below and check if this is what you want.CREATE FUNCTION ConcatValues(@cm_number char(1))RETURNS varchar(8000)ASBEGINDECLARE @Result varchar(8000)SELECT @Result=COALESCE(@Result,'') + CAST(t.plu_cpt as varchar(5)) + '-'FROM YourTable tINNER JOIN (SELECT plu_cpt FROM YourTable GROUP BY plu_cpt HAVING COUNT(*) >1) t1ON t1.plu_cpt=t.plu_cptWHERE t.cm_number=@cm_numberRETURN @ResultEND |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-11 : 10:38:16
|
and just in case, what you want is to take plu_cnt having more than 1 occurance per cm_number value, then make it like belowCREATE FUNCTION ConcatValues(@cm_number char(1))RETURNS varchar(8000)ASBEGINDECLARE @Result varchar(8000)SELECT @Result=COALESCE(@Result,'') + CAST(t.plu_cpt as varchar(5)) + '-'FROM YourTable tINNER JOIN (SELECT plu_cpt,cm_number FROM YourTable GROUP BY plu_cpt,cm_number HAVING COUNT(*) >1) t1ON t1.plu_cpt=t.plu_cptAND t1.cm_number=t.cm_numberWHERE t.cm_number=@cm_numberRETURN @ResultEND |
 |
|
|
|