sql server - SQL query to fetch data from one table with different condition -


i beginner of sql developer , try make such queries.
have 1 table in data. want data per calculation.

table structure below:

create table datatable ( id numeric(18,0), debitnoteno varchar(20), totalamt numeric(18,0), status char(4), linenumber numeric(18,0)   )   insert datatable values(1,'db001',200,'c',1) insert datatable values(2,'db001',100,'c',2) insert datatable values(3,'db001',300,'c',2)  insert datatable values(4,'db002',500,'c',1) insert datatable values(5,'db002',100,'c',4)  insert datatable values(6,'db003',200,'s',2) insert datatable values(7,'db003',300,'s',4) insert datatable values(8,'db003',400,'s',5)  insert datatable values(9,'db003',200,'c',1) insert datatable values(10,'db003',100,'c',3) insert datatable values(11,'db003',700,'c',8) insert datatable values(12,'db003',100,'c',5)  insert datatable values(13,'db004',800,'e',1) insert datatable values(14,'db004',100,'e',5) insert datatable values(15,'db004',200,'e',6) 

i want output below:

            distinct col1        debitnotenocount   totallineitem    totalcount  totalflagc    3                   9               2300 totalflags    1                   3               900 totalflage    1                   3               1100 

now in output
column 1 fixed,
column 2 count of distinct debitnote no
column 3 count of total rows
column 4 sum of totalamt

my row fixed (three rows)

col1 condition ex.

totalflagc =  status = 'c' totalflags =  status = 's' 

i union think takes more time. please give me other solutions.

sql fiddle

no need union.

select concat('totalflag',status),         count(distinct debitnoteno),         count(linenumber) totallineitem,        sum(totalamt) totalcount   datatable group        status; 

sql fiddle


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -