crystal reports-count - crystal-reports

Hai Friends
this is my query
SELECT
COUNT(CASE ISNULL(GAM_STATUS, ' ')
WHEN '1' THEN '1'
END) + COUNT(CASE ISNULL(GAM_STATUS, ' ')
WHEN '2' THEN '2'
END) + COUNT(CASE ISNULL(GAM_STATUS, ' ')
WHEN '3' THEN '3'
END) ACTIVE_REC,
COUNT(CASE ISNULL(GAM_STATUS, ' ')
WHEN '5' THEN '5'
END) DELETED,
COUNT(CASE ISNULL(GAM_STATUS, ' ')
WHEN '4' THEN '4'
END) SOLD
FROM GLAS_ASSET_MASTER_T
WHERE GAM_COMP_CODE = '1' and gam_dept_code between '01' and '03'
output is
active_rec deleted sold
50 20 25
same should come in the crystal reports how can i count the records
in the crystal reports according to the conditions.

A common method is something like the following :
Formula name : #Active_Rec
Formula text : If {GLAS_ASSET_MASTER_T.GAM_STATUS} IN [1,2,3] Then 1 Else 0
Formula name : #Deleted_Rec
Formula text : If {GLAS_ASSET_MASTER_T.GAM_STATUS} = 5 Then 1 Else 0
Formula name : #SoldRec
Formula text : If {GLAS_ASSET_MASTER_T.GAM_STATUS} = 4 Then 1 Else 0
Place those formula in the report's Details section, and add Summary Fields for them to your Report Footer.
And of course your record selection formula would be
{GLAS_ASSET_MASTER_T.GAM_COMP_CODE} = '1' and {GLAS_ASSET_MASTER_T.gam_dept_code} between '01' and '03'

Related

GROUP BY with CASE expression IN PostgreSQL

This is my query:
SELECT userid,
case when extract(day from date )=1 then string_agg(status,'') end "1" ,
case when extract(day from date )=2 then string_agg(status,'') end "2" ,
.
.
.
case when extract(day from date )=31 then string_agg(status,'') end "31"
from attendance.attendance group by userid;
But I have the next error:
ERROR: column "attendance.date" must appear in the GROUP BY clause or be used in an aggregate function
When group by userid and date the output:
userid 1 2 3 ... 31
1 P
1 A
1 P ...
1 P
I want to group by userid like this:
userid 1 2 3 ... 31
1 P A P ... P
2 P P P ... P
etc.
Any ideas? I'll very appreciate.
You'll want to put the string_agg around the CASE expression:
SELECT userid,
string_agg(case when extract(day from date)=1 then status end) "1" ,
string_agg(case when extract(day from date)=2 then status end) "2" ,
.
.
.
string_agg(case when extract(day from date)=31 then status end) "31"
FROM attendance.attendance
GROUP BY userid;
A nicer way to write this query would be using a FILTER for the aggregate:
SELECT userid,
string_agg(status) FILTER (WHERE extract(day from date)=1) "1" ,
string_agg(status) FILTER (WHERE extract(day from date)=2) "2" ,
.
.
.
string_agg(status) FILTER (WHERE extract(day from date)=31) "31"
FROM attendance.attendance
GROUP BY userid;

postgres remove blank values from grouping

I have a rollup SQL statement that I'm trying to covert over from Oracle into PostGresSql. Overall the results look correct except I'm getting a blank value in the grouping column and I'm not sure how to get rid of it.
Right now I have:
SELECT
COALESCE(CASE WHEN GROUPING(COUNTY) = 1 THEN 'TOTAL' else county::text END) as COUNTY
,COUNT(CASE WHEN STV_TO_GAS THEN 1 END) as STOVE_TO_GAS_SUM
,COUNT(CASE WHEN FIRE_TO_GAS THEN 1 END) as FIRE_TO_GAS_SUM
,COUNT(CASE WHEN PELLET_TO_GAS THEN 1 END) as PELLET_TO_GAS_SUM
,COUNT(CASE WHEN STV_TO_ELECTRIC THEN 1 END) as STOVE_TO_ELECTRIC_SUM
,COUNT(CASE WHEN FIRE_TO_ELECTRIC THEN 1 END) as FIRE_TO_ELECTRIC_SUM
,COUNT(CASE WHEN PELLET_TO_ELECTRIC THEN 1 END) as PELLET_TO_ELECTRIC_SUM
,COUNT(CASE WHEN MERIDIAN THEN 1 END) as WITHIN_MERIDIAN_SUM
,count(CASE WHEN hb357.stv_to_gas then 1 END) +
count(CASE WHEN hb357.fire_to_gas then 1 END) +
count(CASE WHEN hb357.pellet_to_gas then 1 END) +
count(CASE WHEN hb357.stv_to_electric then 1 END) +
count(CASE WHEN hb357.fire_to_electric then 1 END) +
count(CASE WHEN hb357.pellet_to_electric then 1 END) +
count(CASE WHEN hb357.meridian then 1 END ) AS county_totals
FROM woodburn.HB357
WHERE app_status IN ('pending','approved')
AND (COUNTY IS NOT NULL OR trim(COUNTY) <> '')
GROUP BY rollup (county)
but its still returning a blank value in the county column
I've also trued changing the first case statement to
COALESCE(CASE
WHEN GROUPING(COUNTY) = 1 THEN 'TOTAL'
WHEN TRIM(COUNTY) != '' AND COUNTY IS NOT NULL then county
END) as COUNTY
but its returning a null row for county
FINAL SOLUTION
After trying all things suggested I essentially implemented what Jorge Campos recommended by doing the following:
SELECT
COALESCE(CASE
WHEN GROUPING(COUNTY) = 1 THEN 'TOTAL'
WHEN TRIM(COUNTY) != '' then county
END) as COUNTY
,COUNT(CASE WHEN STV_TO_GAS THEN 1 END) as STOVE_TO_GAS_SUM
,COUNT(CASE WHEN FIRE_TO_GAS THEN 1 END) as FIRE_TO_GAS_SUM
,COUNT(CASE WHEN PELLET_TO_GAS THEN 1 END) as PELLET_TO_GAS_SUM
,COUNT(CASE WHEN STV_TO_ELECTRIC THEN 1 END) as STOVE_TO_ELECTRIC_SUM
,COUNT(CASE WHEN FIRE_TO_ELECTRIC THEN 1 END) as FIRE_TO_ELECTRIC_SUM
,COUNT(CASE WHEN PELLET_TO_ELECTRIC THEN 1 END) as PELLET_TO_ELECTRIC_SUM
,COUNT(CASE WHEN MERIDIAN THEN 1 END) as WITHIN_MERIDIAN_SUM
,count(CASE WHEN hb357.stv_to_gas then 1 END) +
count(CASE WHEN hb357.fire_to_gas then 1 END) +
count(CASE WHEN hb357.pellet_to_gas then 1 END) +
count(CASE WHEN hb357.stv_to_electric then 1 END) +
count(CASE WHEN hb357.fire_to_electric then 1 END) +
count(CASE WHEN hb357.pellet_to_electric then 1 END) +
count(CASE WHEN hb357.meridian then 1 END ) AS county_totals
FROM woodburn.HB357
WHERE app_status IN ('pending','approved')
AND COUNTY IS NOT NULL
AND TRIM(COUNTY) != ''
GROUP BY rollup (county)

Postgres Count Function for monthly transactions, counting distinct only

I am counting the size of my teams within a department. All employees have an employee ID beginning with "E" and then a designating number (i.e. "0", "1", etc) to denote which team they are on.
I have the following query in Postgres to count the size of the teams, but the problem is that with this query, I get a lot of rows that are empty, because some months are duplicated. For example, the row containing "May/2016" may be duplicated 3 times, with only 1 row containing the actual team counts.
select to_char("Date", 'Mon/YYYY') as "Date",
sum(case when l_part LIKE 'E0%%' then count end) as "ACCOUNTING",
sum(case when l_part LIKE 'E1%%' then count end) as "SW",
sum(case when l_part LIKE 'E2%%' then count end) as "SUPPORT",
sum(case when l_part LIKE 'E3%%' then count end) as "CALLCENTER",
sum(case when l_part LIKE 'E4%%' then count end) as "ADMIN",
sum(case when l_part LIKE 'E5%%' then count end) as "MARKETING",
sum(case when l_part LIKE 'E9%%' then count end) as "MANAGEMENT"
from (
select left("Type",4)as l_part, count(*),"Date" from
"Transactions" group by "Date",l_part
) p group by "Date"
order by min("Date");
If I can just get the count down to one row per month/yyyy, and order by the date that would be helpful and less confusing. Any tweaks to my attempt appreciated.
Here is what populates, as an example using September 2015:
This is what I get:
DATE | ACCOUNTING | SW | SUPPORT | CALLCENTER | ADMIN | MARKETING |
Sep/15| | | | | | |
Sep/15| | | | | | |
Sep/15| 1 | 2 | 1 | 5 | 5 | 3 |
I suspect your issue is the GROUP BY clause, which I think is solved using DATE_TRUNC(). Not sure if you need the where clause.
SELECT
to_char(DATE_TRUNC('month',"Date"), 'Mon/YYYY') as "Date"
, SUM(CASE WHEN left("Type",4) LIKE 'E0%%' THEN 1 END) AS "ACCOUNTING"
, SUM(CASE WHEN left("Type",4) LIKE 'E1%%' THEN 1 END) AS "SW"
, SUM(CASE WHEN left("Type",4) LIKE 'E2%%' THEN 1 END) AS "SUPPORT"
, SUM(CASE WHEN left("Type",4) LIKE 'E3%%' THEN 1 END) AS "CALLCENTER"
, SUM(CASE WHEN left("Type",4) LIKE 'E4%%' THEN 1 END) AS "ADMIN"
, SUM(CASE WHEN left("Type",4) LIKE 'E5%%' THEN 1 END) AS "MARKETING"
, SUM(CASE WHEN left("Type",4) LIKE 'E9%%' THEN 1 END) AS "MANAGEMENT"
FROM "Transactions"
WHERE "Date" IS NOT NULL
GROUP BY
DATE_TRUNC('month',"Date")
ORDER BY
DATE_TRUNC('month',"Date")
btw: Instead of SUM() an alternatve using COUNT() would be:
SELECT
to_char(DATE_TRUNC('month',"Date"), 'Mon/YYYY') as "Date"
, COUNT(CASE WHEN left("Type",4) LIKE 'E0%%' THEN 1 END) AS "ACCOUNTING"
, COUNT(CASE WHEN left("Type",4) LIKE 'E1%%' THEN 1 END) AS "SW"
, COUNT(CASE WHEN left("Type",4) LIKE 'E2%%' THEN 1 END) AS "SUPPORT"
, COUNT(CASE WHEN left("Type",4) LIKE 'E3%%' THEN 1 END) AS "CALLCENTER"
, COUNT(CASE WHEN left("Type",4) LIKE 'E4%%' THEN 1 END) AS "ADMIN"
, COUNT(CASE WHEN left("Type",4) LIKE 'E5%%' THEN 1 END) AS "MARKETING"
, COUNT(CASE WHEN left("Type",4) LIKE 'E9%%' THEN 1 END) AS "MANAGEMENT"
COUNT() increments by one for any NON-NULL value it encounters.

I need to group name in a row

I have 12 rows for year and name, but I want 1 row for year and name with 12 register in (1 or null).
select id_agente, nombre, ene, feb, mar, abr, may, jun, jul, ago, sep, oct, nov, dic
from (
(select
a.id_agente id_agente,
a.NOMBRE nombre,
case
when to_char (to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '01' then 1 end ene,
case
when to_char (to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '02' then 1 end feb,
..................
)
group by nombre, id_agente, ene, feb, mar, abr, may, jun, jul, ago, sep, oct, nov, dic
order by nombre, id_agente, ene, feb, mar, abr, may, jun, jul, ago, sep, oct, nov, dic;
I hope you help me, thank you very much!
There are several solutions to this common problem.
If You are using Oracle 11g or newer, You can use PIVOT.
It is quite tricky to use.
You can write PL/SQL function that will return 'table' with all the data You want in any format You want.
You can Select one row with all months numbers from dual, then join them with Your query (preferably using WITH statement) 12 times (one for each month).
You can also realize that You do not need it in one row, because probably - You don't. :) maybe You can modify result where You get it from database, and then create one column for each month. Or maybe You can display it as a table and so on...
I think there are some other solutions as well, but I suggest You go for number 4.
I remember having similar problem quite often with SQL in the beginning, but they just fade away now.
I suppose You are approaching Your problem from wrong angle.
One approach, although verbose, is to define each aggregate column with a case statement as the argument to summarize. Then the "group by" and "order by" clauses only need to be given "id_agente" and "nombre". I've used this approach several times with Oracle data.
The below query is untested, and it makes some assumptions, but it should at least be a good start.
select a.id_agente id_agente,
a.NOMBRE nombre,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '01' then 1 else 0 end) ene,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '02' then 1 else 0 end) feb,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '03' then 1 else 0 end) mar,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '04' then 1 else 0 end) abr,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '05' then 1 else 0 end) may,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '06' then 1 else 0 end) jun,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '07' then 1 else 0 end) jul,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '08' then 1 else 0 end) ago,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '09' then 1 else 0 end) sep,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '10' then 1 else 0 end) oct,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '11' then 1 else 0 end) nov,
sum(case when to_char(to_date(dec.FECHA_RECOGIDA_ORIGEN,'dd/mm/yyyy'),'mm') = '12' then 1 else 0 end) dic
from dec
group by id_agente,
nombre
order by id_agente,
nombre;
However, using a pivot query, like Dark Anavger mentioned, would likely be the most formal approach if your output format needs to be a single row with twelve summary columns.
Good luck!

Union Statement that can be changed to Case statement

Good Day Everyone!
well i have this kind of code and it kinda ugly,
a friend of mine told me i can implement Case Statements in here, but i do not know how or how would i implement, the code is long so if you could just help me to optimize my code i would appreciate it greatly!
PS. please be gentle to me, im new in T-sql :)
Thank yoU!
SELECT
SUM(CYJEWELRY) 'CY_Jewelry'
,SUM(CYAPPLICANCE) 'CY_Appliance'
,SUM(CYCELLPHONE) 'CY_Cellphone'
,SUM(PYJEWELRY) 'PY_Jewelry'
,SUM(PYAPPLIANCE) 'PY_Appliance'
,SUM(PYCELLPHONE) 'PY_Cellphone'
FROM
(
---TOTAL NUNG A FORMAT 0,0,0,0,0,0
--------------CURRENT YEAR JEWELRY
SELECT COUNT (*) AS CYJEWELRY,0 AS CYAPPLICANCE,0 AS CYCELLPHONE,0 AS PYJEWELRY,0 AS PYAPPLIANCE,0 AS PYCELLPHONE
FROM #TEMPTABLE1
WHERE (fld_StorageGroupID >= 3 and fld_StorageGroupID <= 14)
UNION
-----------CURRENT YEAR APPLIANCE
SELECT 0,COUNT(*),0,0,0,0
FROM #TEMPTABLE1
WHERE fld_StorageGroupID = 1
UNION
------------CURRENT YEAR CELLPHONE
SELECT 0,0,COUNT(*),0,0,0
FROM #TEMPTABLE1
WHERE fld_StorageGroupID = 2
UNION
---------------LAST YEAR JEWELRY
SELECT 0,0,0,COUNT(*),0,0
FROM #TEMPTABLE2
WHERE (fld_StorageGroupID >= 3 and fld_StorageGroupID <= 14)
UNION
-----------------------LAST YEAR APPLIANCE
SELECT 0,0,0,0,COUNT (*),0
FROM #TEMPTABLE2
WHERE fld_StorageGroupID = 1
UNION
-------------------------LAST YEAR CELLPHONE
SELECT 0,0,0,0,0,COUNT(*)
FROM #TEMPTABLE2
WHERE fld_StorageGroupID = 2
)A
Assuming your data is bit like this Sql Fiddle Example, try this for the sub query using SUM() and CASE.
SELECT SUM(CASE WHEN fld_StorageGroupID >= 3 and fld_StorageGroupID <= 14 ELSE 0 END) Col1And4,
SUM(CASE WHEN fld_StorageGroupID = 1 THEN 1 ELSE 0 END) Col2And5,
SUM(CASE WHEN fld_StorageGroupID = 2 THEN 1 ELSE 0 END) Col3And6
FROM #TEMPTABLE1
GROUP BY fld_StorageGroupID
Since you are applying the same filter for last 3 columns in the subquery, I have done only first 3 columns here.
EDIT:
I think this is better than above (Note: no need to use SUM() in the main query).
Fiddle Example with data
select col1_4 CY_Jewelry,
col2_5 CY_Appliance,
col3_6 CY_Cellphone,
col1_4 PY_Jewelry,
col2_5 PY_Appliance,
col3_6 PY_Cellphone
from (
select sum(case when id>= 3 and id <= 14 then 1 else 0 end) col1_4,
sum(case when id = 2 then 1 else 0 end) col2_5,
sum(case when id = 3 then 1 else 0 end) col3_6
from t
--group by id
) X