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 |
svibuk
Yak Posting Veteran
62 Posts |
Posted - 2011-06-21 : 07:42:54
|
here is a the entire details of the table with the expected outputTable name= Billdeatilsbillno customer basicamt ta date location1005 200 101008 500 15 1003 800 202015 1000 102015 300 121008 200 181003 1500 151005 2000 16 2015 400 202010 500 501003 600 251000 300 20toral record count =12query for getting the below output1) count (disctinct billno) = 72) sum (basicamt + ta) of all 12 records3) for each bill i need i need count & sumeg billno =1005 count(billno) where billno =1005 shld give 2sum (basicamt + ta) where billno =1005 shld give 22264) details of each billeg billno =1005 1005 200 101005 2000 16 the below is the quer y butSELECT DISTINCT billno,(SELECT COUNT(DISTINCT billno) FROM billdetails) AS ccount,(SELECT sum(basicamt+ta) FROM billdetails) AS amt,(SELECT COUNT( billno) FROM billdetails where billno=1005) AS paycount,(SELECT sum(basicamt+ta) FROM billdetails where billno=1005) AS payamt, (SELECT * FROM billdetails where billno=1005)FROM billdetails |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-21 : 07:57:42
|
select billno, (SELECT COUNT(DISTINCT billno) FROM billdetails) AS totcount, (SELECT sum(basicamt+ta) FROM billdetails) AS totamt, sum(basicamt+ta) as billamt, count(*) as billcountfrom Billdeatilsgroup by billno==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
svibuk
Yak Posting Veteran
62 Posts |
Posted - 2011-06-21 : 08:06:06
|
uisng the below query i do get the half result i mean the counts r displayed correctly but i am not able to use SELECT * FROM billdetails where billno...i need other fields also to be displayed with each billnoquote: Originally posted by nigelrivett select billno, (SELECT COUNT(DISTINCT billno) FROM billdetails) AS totcount, (SELECT sum(basicamt+ta) FROM billdetails) AS totamt, sum(basicamt+ta) as billamt, count(*) as billcountfrom Billdeatilsgroup by billno==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
|
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-06-21 : 08:14:24
|
select b.*, a.*from Billdetails bjoin(select billno, (SELECT COUNT(DISTINCT billno) FROM billdetails) AS totcount, (SELECT sum(basicamt+ta) FROM billdetails) AS totamt, sum(basicamt+ta) as billamt, count(*) as billcountfrom Billdetailsgroup by billno) aon a.billno = b.billnoYou might want to review the way you are using this as it is decidedly odd.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
svibuk
Yak Posting Veteran
62 Posts |
Posted - 2011-06-21 : 08:26:09
|
thnaks i do get the fields as needed but will check out with teh excat output & let knwi face issue when forming the queries , need to knw to form itquote: Originally posted by nigelrivett select b.*, a.*from Billdetails bjoin(select billno, (SELECT COUNT(DISTINCT billno) FROM billdetails) AS totcount, (SELECT sum(basicamt+ta) FROM billdetails) AS totamt, sum(basicamt+ta) as billamt, count(*) as billcountfrom Billdetailsgroup by billno) aon a.billno = b.billnoYou might want to review the way you are using this as it is decidedly odd.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
|
|
|
svibuk
Yak Posting Veteran
62 Posts |
Posted - 2011-06-22 : 08:03:02
|
select DISTINCT billno, (SELECT COUNT(DISTINCT billno) FROM billdetails) AS totcount, (SELECT sum(basicamt+ta) FROM billdetails) AS totamt, sum(basicamt+ta) as billamt, count(*) as billcount,sum(basicamt) as basic,sum(ta) as at,from Billdeatilsgroup by billnoin the above qry each distinct billn has some info in other fields which i need eg date, branch etcthey pertain to each distinct billnobillno billcount -----1005 21008 21003 32015 32010 11000 1i get the info related to distinct bill same way i need to get the other info alsoby the earlier wuery which u gave usng join though i get all the records i am not able to get the details as per abovehope i am able to put it in right manner |
|
|
|
|
|
|
|