I encounter a question about crystal report how to group by ?
I have a table named Part
Part_ID Parent_Part_ID
B A
c A
A NULL
C B
D B
E C
F C
A is a top part, B,C,D not only sub part but also are parent part, E,F are lowest sub part.
Now ,I need to show From Parent_part to Part with Levels,Like This
How Can I show this format data in Crystal report
I try use hierachical grouping options ,but the result is not i want
I need the result is:
A
B
C (shold be show even the part have two parent part)
E
F
D
C
E
F
SOLVED!
Tried menu voice called hierachical grouping options and specified the field that links each record to his parent.
The result should look similar to this:
A
B
C
E
F
D
C
E
F
But Crystal Reports Hierarchies take each record just once, and don't care if same element should be under two parents.
So i made a query-hack, creating hierarchy directly in the data using a JOIN on the same table based on Parent Item
DECLARE #t TABLE (Part_ID varchar(1), Parent_Part_ID varchar(1) );
insert into #t
SELECT 'B' , 'A'
UNION SELECT 'C' , 'A'
UNION SELECT 'A' , NULL
UNION SELECT 'C' , 'B'
UNION SELECT 'D' , 'B'
UNION SELECT 'E' , 'C'
UNION SELECT 'F' , 'C'
SELECT
t1.Part_ID as t1,
t2.Part_ID as t2,
t3.Part_ID as t3,
t4.Part_ID as t4
FROM #t t1
LEFT JOIN #t t2 on t1.Part_ID = t2.Parent_Part_ID
LEFT JOIN #t t3 on t2.Part_ID = t3.Parent_Part_ID
LEFT JOIN #t t4 on t3.Part_ID = t4.Parent_Part_ID
WHERE t1.Parent_Part_ID is null
and removed hierarchical grouping options, just created 3 groups on t1, t2, t3 using header for each group and using details for t4.
The result is, as needed,
A
B
C
E
F
D
C
E
F
Here the .rpt if someone needs it.
Related
In my JPA model there are 3 tables A, B, C.
My query is:
SELECT a FROM A a
WHERE EXISTS (
SELECT c from C c LEFT JOIN B b"
ON c = b.c AND b.a = a
WHERE c.date BETWEEN CURRENT_TIMESTAMP AND :pUntil AND b.a IS NULL
)
Background is that I want all entities of A that do not have an entry in b that is linked to an event C in the future.
The problem is that I get Column 'T0.ID' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or ...
EDIT
: Think of it as A is a user table, C are events, and B stores the registrations of users for events. I want to get all users, which have not registered for all future events until parameter pUntil.
Although I agree with Neil, I worked around this issue by changing my query. Here's the new query:
SELECT a FROM A a
WHERE EXISTS (
SELECT c from C c
WHERE c.date BETWEEN CURRENT_TIMESTAMP AND :pUntil
AND NOT EXISTS (
SELECT b from B b
WHERE b.c= c and b.a = a
)
)
select id,proc_name,p_date,p_no,p_count
from (
(Select id,proc_name,p_date,p_no from aa) x
join
(select id,count(p_no) p_count from aa group by mrn) y
on x.id=y.id
) a
FROM (select id,proc_name,p_date,p_no from zz) aa
getting the error code 42601 at the position of FROM (in upper case).
A SELECT statement can only have one FROM clause.
Instead of using multiple FROM clauses, you should JOIN the tables.
There is no need to have two SELECTs from table aa, you can do that with a single SELECT using window functions:
SELECT id, proc_name, p_date, p_no,
count(p_no) OVER (PARTITION BY mrn) p_count
FROM aa;
You didn't tell over which columns you want to join aa and zz, but your statement could look something like this:
SELECT a.id, a.proc_name, a.p_date, a.p_no, a.p_count
FROM
(SELECT id, proc_name, p_date, p_no,
count(p_no) OVER (PARTITION BY mrn) p_count
FROM aa) a
JOIN zz
ON <join condition for a and zz>;
I have a Table which has 2 fields say A,B. Suppose A has values a1,a2.
Corresponding records for a1 in B are 1,2,3,x,y,z.
Corresponding records for a2 in B are 1,2,3,4,d,e,f
I need a a query to be written in DB2, so that it will fetch the common records in B for each record in A (a1 and a2).
So here the output would be :
A B
a1 1
a1 2
a1 3
a2 1
a2 2
a2 3
Can someone please help on this?
Try something like:
SELECT A, B
FROM Table t1
WHERE (SELECT COUNT(*) FROM Table t2 WHERE t2.B = t1.B)
= (SELECT COUNT(DISTINCT t3.A) FROM Table t3)
ORDER BY A, B
This might not be 100% accurate as I can't test it out in DB2 so you might have to tweak the query a little bit to make it work.
with t(num) as (select count(distinct A) from table)
select t1.A, t1.B
from table t1, table t2, t
where t1.B = t2.B
group by t1.A, t1.B, num
having count(*) = num
Basically, the idea is to join the same table with column B and filter out just the ones that match exactly the same number of times as the number of elements in column A, which indicates that it is a common record out of all the A values.
From this table:
Select * into #tmp from (
select 'a' A, 'b' B, NULL C union all
select 'aa' A, 'ab' B, 1 C union all
select 'aaa' A, 'bbb' B, 2 C ) x
I'd like to get this result:
A B Val
a b 1
aa ab 1
aaa bbb 2
That is, take the non-null min value and replace the NULL.
I suppose I could join the table to a filtered version of itself where no nulls appear. But that seems overkill. I thought this might be able to be done in the MIN Aggregate clause itself.
Any ideas would be helpful, thanks!
declare #null int
select #null = MIN(c) from #tmp
select A,
B,
ISNULL(c,#null) as val1
from #tmp
or
select A,
B,
ISNULL(c,(select MIN(c) from #tmp)) as val1
from #tmp
EDIT: I wrote "You want something like ISNULL(c, MIN(c)) but that's not possible."
But I'm wrong, it is possible. I was missing something in my syntax, so #kiki47's answer is exactly what you are asking for.
I wouldn't phrase it as "I suppose I could join the table to a filtered version of itself where no nulls appear," but more or less you can get the min and the use it.
In one go:
WITH cte AS (
SELECT MIN(c) minVal FROM #tmp WHERE c IS NOT NULL
)
SELECT a, b, ISNULL(c, cte.minVal)
FROM #tmp
CROSS JOIN cte
or maybe simpler (but may optimize to the same thing):
DECLARE #minVal INTEGER
SELECT #minVal = MIN(c) FROM #tmp WHERE c IS NOT NULL
SELECT a, b, ISNULL(c, #minVal) FROM #tmp
In my procedure I have two tables with the same data. I go through my first table through a cursor. Which compares with the second table that I find much the same data. What if, for example in my table_1 I have ten in my data and I have 12 data table2 how to detect missing data in my two table_1 which is traversed by the cursor?
Thx.
Sounds very much like you'd be better off using the MINUS operator.
SELECT a, b, c
FROM table1
MINUS
SELECT a, b, c
FROM table2
This will show you all results that exist in table1 which are not present in table2. In order to show discrepancies both ways, you could do something like this:
SELECT z.*, 'In table1, not in table2' problem_description
FROM (
SELECT a, b, c
FROM table1
MINUS
SELECT a, b, c
FROM table2
) z
UNION ALL
SELECT z.*, 'In table2, not in table1' problem_description
FROM (
SELECT a, b, c
FROM table2
MINUS
SELECT a, b, c
FROM table1
) z
SQL Fiddle for this answer