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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 forming a query as per the desired

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 output

Table name= Billdeatils

billno customer basicamt ta date location

1005 200 10
1008 500 15
1003 800 20
2015 1000 10
2015 300 12
1008 200 18
1003 1500 15
1005 2000 16
2015 400 20
2010 500 50
1003 600 25
1000 300 20

toral record count =12

query for getting the below output

1) count (disctinct billno) = 7
2) sum (basicamt + ta) of all 12 records

3) for each bill i need i need count & sum
eg billno =1005

count(billno) where billno =1005 shld give 2
sum (basicamt + ta) where billno =1005 shld give 2226


4) details of each bill
eg billno =1005

1005 200 10
1005 2000 16

the below is the quer y but

SELECT 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 billcount
from Billdeatils
group 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.
Go to Top of Page

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 billno

quote:
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 billcount
from Billdeatils
group 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.

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-21 : 08:14:24
select b.*, a.*
from Billdetails b
join
(
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 billcount
from Billdetails
group by billno
) a
on a.billno = b.billno

You 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.
Go to Top of Page

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 knw

i face issue when forming the queries , need to knw to form it

quote:
Originally posted by nigelrivett

select b.*, a.*
from Billdetails b
join
(
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 billcount
from Billdetails
group by billno
) a
on a.billno = b.billno

You 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.

Go to Top of Page

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 Billdeatils
group by billno

in the above qry each distinct billn has some info in other fields which i need eg date, branch etc
they pertain to each distinct billno
billno billcount -----
1005 2
1008 2
1003 3
2015 3
2010 1
1000 1
i get the info related to distinct bill same way i need to get the other info also

by the earlier wuery which u gave usng join though i get all the records i am not able to get the details as per above
hope i am able to put it in right manner
Go to Top of Page
   

- Advertisement -