Grouping in crystal report - crystal-reports-2010

Sorry for this newbie question.
I have 1 SQL Server table:
Column1 | Column2 | Column3
Row1 A 1 100
Row2 A 1 200
Row3 A 2 50
Row4 B 4 10
Row5 C 5 20
Here in this report i would like to get the output as:
Column1 | Column2 | Colum3
Row1 A 1 300
Row2 A 2 50
Row3 B 4 10
Row4 C 5 20
Thanks in advance.

Insert group on Column1
Insert group on Column2
Suppress all sections except group #2 header
Place Column1, Column2, and Sum(Column3) into the group #2 header

Related

Oracle - how to insert if not exists?

Example dB : https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=49af209811bce88aa67b42387f1bb5f6
I'd like to add insert this line
1002 9 1 UNKNOWN
Because of the line exists
1002 5 1 JIM
I was thinking about something like
select codeclient from STATS_CLIENT_TEST where CODEAXESTAT=5
and insert codeclient, 9,1,UNKNOWN.
but not sure how to do it? And simple query or a PL/SQL?
What's the best way to get it?
Thanks
Use an INSERT .. SELECT statement with a PARTITIONed outer join:
INSERT INTO stats_client_test (
codeclient, codeaxestat, codeelementstat, valeuraxestatistiqueclient
)
SELECT cc.codeclient,
s.codeaxestat,
s.codeelementstat,
'UNKNOWN'
FROM (SELECT DISTINCT codeclient FROM stats_client_test) cc
LEFT OUTER JOIN stats_client_test s
PARTITION BY (s.codeaxestat, s.codeelementstat)
ON (s.codeclient = cc.codeclient)
WHERE s.rowid IS NULL;
or a MERGE statement:
MERGE INTO stats_client_test dst
USING (
SELECT cc.codeclient,
s.codeaxestat,
s.codeelementstat,
s.ROWID AS rid
FROM (SELECT DISTINCT codeclient FROM stats_client_test) cc
LEFT OUTER JOIN stats_client_test s
PARTITION BY (s.codeaxestat, s.codeelementstat)
ON (s.codeclient = cc.codeclient)
) src
ON (dst.ROWID = src.rid)
WHEN NOT MATCHED THEN
INSERT (codeclient, codeaxestat, codeelementstat, valeuraxestatistiqueclient)
VALUES (src.codeclient, src.codeaxestat, src.codeelementstat, 'UNKNOWN');
db<>fiddle here
Here's one option: using the MINUS set operator, find missing codeclient values and then insert appropriate row(s).
Before:
SQL> select * From stats_client_Test order by codeaxestat, codeclient;
CODECLIENT CODEAXESTAT CODEELEMENTSTAT VALEURAXESTATISTIQUECLIENT
-------------------- ----------- --------------- ----------------------------------------
1000 5 1 JOHN
1001 5 1 ALICE
1002 5 1 JIM
1003 5 1 BOB
1000 9 1 MAN
1001 9 1 WOMAN
1002 9 1 unknown
1003 9 1 MAN
8 rows selected.
Query:
SQL> insert into stats_client_test
2 (codeclient, codeaxestat, codeelementstat, VALEURAXESTATISTIQUECLIENT)
3 select x.codeclient, 9, 1, 'unknown'
4 from (select codeclient from stats_client_Test
5 where codeaxestat = 5
6 minus
7 select codeclient from stats_client_Test
8 where codeaxestat = 9
9 ) x;
0 rows created.
After:
SQL> select * From stats_client_Test order by codeaxestat, codeclient;
CODECLIENT CODEAXESTAT CODEELEMENTSTAT VALEURAXESTATISTIQUECLIENT
-------------------- ----------- --------------- ----------------------------------------
1000 5 1 JOHN
1001 5 1 ALICE
1002 5 1 JIM
1003 5 1 BOB
1000 9 1 MAN
1001 9 1 WOMAN
1002 9 1 unknown --> here it is
1003 9 1 MAN
8 rows selected.
SQL>

Merge - Oracle and populate in two different tables based on count condition

I have a table name "TABLE1" with 4 columns COLUMN1, COLUMN2, FREQ, CNT
I have a result table name "RESULT1" with 4 columns COLUMN1, COLUMN2, FREQ
I have a result table name "RESULT2" with 4 columns COLUMN1, COLUMN2, FREQ
CREATE TABLE TABLE1 ( COLUMN1 VARCHAR2(4), COLUMN2 VARCHAR2(4), FREQ NUMBER, CNT NUMBER);
INSERT INTO TABLE1(COLUMN1, COLUMN2, FREQ) VALUES ('1234', 'ABCD', 1);
INSERT INTO TABLE1(COLUMN1, COLUMN2, FREQ) VALUES ('1234', 'ACBD', 1);
INSERT INTO TABLE1(COLUMN1, COLUMN2, FREQ) VALUES ('1234', 'ABDC', 1);
INSERT INTO TABLE1(COLUMN1, COLUMN2, FREQ) VALUES ('1342', 'DAFY', 1);
INSERT INTO TABLE1(COLUMN1, COLUMN2, FREQ) VALUES ('1423', 'CBAD', 1);
I want to update the CNT column in TABLE1 based on number of times COLUMN1 is repeated.
The result in TABLE1:
COLUMN1 COLUMN2 FREQ CNT
-----------------------------------
1234 ABCD 1 3
1234 ACBD 1 3
1234 ABDC 1 3
1342 DAFY 1 1
1423 CBAD 1 1
Once we get above result,
based on CNT value, if CNT =1 insert in RESULT1 table else CNT > 1 insert those records in RESULT2 table like
RESULT2 table
COLUMN1 COLUMN2 FREQ
-----------------------------------
1234 ABCD 1
1234 ACBD 1
1234 ABDC 1
RESULT1 table
COLUMN1 COLUMN2 FREQ
-----------------------------------
1342 DAFY 1
1423 CBAD 1
I tried using MERGE statement to populate but not able to get answer showing some syntax error.
how about the oracle insert all. lets you insert into multiple tables based on a condition.
insert All
When cnt > 1 THEN
into result1
else
into result2
select column1, column2, freq, count(*) over (partition by column1) as cnt from table1;
here is the sql fiddle. http://sqlfiddle.com/#!4/d6f20/5
From my point of view, a simple way is the best way.
This is the starting point:
SQL> select * from table1;
COLU COLU FREQ CNT
---- ---- ---------- ----------
1234 ABCD 1
1234 ACBD 1
1234 ABDC 1
1342 DAFY 1
1423 CBAD 1
Update TABLE1.CNT:
SQL> update table1 a set
2 a.cnt = (select count(*)
3 from table1 b
4 where b.column1 = a.column1
5 );
5 rows updated.
SQL> select * from table1;
COLU COLU FREQ CNT
---- ---- ---------- ----------
1234 ABCD 1 3
1234 ACBD 1 3
1234 ABDC 1 3
1342 DAFY 1 1
1423 CBAD 1 1
Rows whose cnt = 1 go into result1:
SQL> insert into result1 (column1, column2, freq)
2 select column1, column2, freq
3 from table1
4 where cnt = 1;
2 rows created.
SQL> select * from result1;
COLU COLU FREQ
---- ---- ----------
1342 DAFY 1
1423 CBAD 1
Rows whose cnt > 1 go into result2:
SQL> insert into result2 (column1, column2, freq)
2 select column1, column2, freq
3 from table1
4 where cnt > 1;
3 rows created.
SQL> select * from result2;
COLU COLU FREQ
---- ---- ----------
1234 ABCD 1
1234 ACBD 1
1234 ABDC 1
SQL>
The end.

Need oracle query to select the order by

Table contains value 1,2,3 but while display the values needs to show 2,1,3
Example
Table A
column1 column2 column3
1 Rat Animals
2 Parrot Bird
3 Lotus Flower
Need to display parrot first then Rat and Lotus which means 2,1,3
Expected Output:
column1 column2 column3
2 Parrot Bird
1 Rat Animal
3 Lotus Flower
Kindly help me out to fix the issue in order by query.
You could use CASE expression in the ORDER BY clause for the particular conditions, and let other rows retain their order.
Setup
SQL> CREATE TABLE t
2 (column1 int, column2 varchar2(6), column3 varchar2(7));
Table created.
SQL> INSERT ALL
2 INTO t (column1, column2, column3)
3 VALUES (1, 'Rat', 'Animals')
4 INTO t (column1, column2, column3)
5 VALUES (2, 'Parrot', 'Bird')
6 INTO t (column1, column2, column3)
7 VALUES (3, 'Lotus', 'Flower')
8 INTO t (column1, column2, column3)
9 VALUES (7, 'def', 'xyz')
10 INTO t (column1, column2, column3)
11 VALUES (4, 'abc', 'qwe')
12 SELECT * FROM dual;
5 rows created.
SQL> COMMIT;
Commit complete.
Table Data
SQL> SELECT * FROM t;
COLUMN1 COLUMN COLUMN3
---------- ------ -------
1 Rat Animals
2 Parrot Bird
3 Lotus Flower
7 def xyz
4 abc qwe
Required Query
SQL> SELECT * FROM t
2 ORDER BY
3 CASE column1
4 WHEN 1
5 THEN 2
6 WHEN 2
7 THEN 1
8 ELSE 3
9 END,
10 column1;
COLUMN1 COLUMN COLUMN3
---------- ------ -------
2 Parrot Bird
1 Rat Animals
3 Lotus Flower
4 abc qwe
7 def xyz
SQL>
So, you have your desired order as well as the other rows retain their order as specified.
That is a strange order requested, anyways try this -
SELECT Column1,Column2,Column3
FROM TableA
ORDER BY CASE WHEN Column1 = 2 THEN 1
WHEN Column1 = 1 THEN 2
ELSE 3
END

Group DataTable by Column1 & Concat Column2

I have DataTable with 2 column:
Column1 Column2
1039-F42F-87BF-723B-529E-6B76-DD92-5ED3 1
1039-F42F-87BF-723B-529E-6B76-DD92-5ED3 2
1039-F42F-87BF-723B-529E-6B76-DD92-5ED3 3
41F7-F8CB-F7A1-9AC5-6C72-3A08-361F-8803 1
9714-96BD-F411-5868-4DD1-A08D-C5B1-B872 1
9714-96BD-F411-5868-4DD1-A08D-C5B1-B872 2
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 1
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 2
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 3
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 4
How can I get another DataTable by Grouping Column1 and concat Column2 values?
LINQ would be a perfect choice, but how to do this?
Result must be DataTable below:
Column1 Column2
1039-F42F-87BF-723B-529E-6B76-DD92-5ED3 1,2,3
41F7-F8CB-F7A1-9AC5-6C72-3A08-361F-8803 1
9714-96BD-F411-5868-4DD1-A08D-C5B1-B872 1,2
9E0E-BC55-374B-5B57-FC04-C2D6-C621-95F7 1,2,3,4

Oracle pivot with dynamic data

I am new to the oracle database and I am trying to use PIVOT to convert rows into columns. I have following tables..
table 1
solid solname
--------------
1 xxxxxx
2 yyyyyyy
table2
id name abbrv
----------------------------------
1 test db tdb
2 Prdocuiton db pdb
table3
id solId
-------------
1 1
1 2
1 3
1 4
1 5
1 7
1 8
1 9
1 22
1 23
1 24
1 25
2 26
2 27
1 28
1 29
1 32
1 33
1 34
1 35
1 36
1 37
3 38
1 39
1 40
1 43
1 44
table 3 is mapper table for table 1 and table 3.
I need to create a view with the columns in table2 and extra column for each solname's. So the view looks like
id name abbrv xxxxxxx yyyyyyy
--------------------------------------------------
So is there a way to do this using PIVOT in oracle database?
For Dynamic SQL Pivoting you need to do something similar :
create or replace view sol_view
as
select
t1.solname,
t2.name,
count(t3.abbrv),
from
table1 t1,
table2 t2,
table3 t3
where
t1.solid = t3.solid
and t2.id = t3.id
group by
t1.solname,
t3.name
select * from table( pivot('select * from sol_view') )
Caveat: I have never tried this but understood the logic from here:
http://technology.amis.nl/2006/05/24/dynamic-sql-pivoting-stealing-antons-thunder/
For Static SQL Pivoting, try something roughly along these lines. Never tried or tested though:
with pivot_data as (
select t1.solname, t2.name, t3.abbrv
from table1 t1, table2 t2, table3 t3
where t1.solid = t3.solid
and t2.id = t3.id
)
select *
from pivot_data
pivot (
count(abbrv)
for solname
in ('xxxxxx','yyyyyyy')
);
It was not really defined what you want to store in the xxxx and yyyy columns, maybe 1/blank, Y/N, ... ? However, your query might look close to something like this:
SELECT * FROM (
SELECT *
FROM table3 t3
JOIN table2 t2 USING (id)
JOIN table1 t1 USING (solid)
) PIVOT (
COUNT(*) FOR (solname) IN (
('xxx') AS "XXX",
('yyy') AS "YYY"
)
)
You can find more information and additional references on My Blog
TableName - **tblItem**
Id ItemName RecipeName Quantity
1 Sugar mutter paneer 200
2 Tea mutter paneer 100
3 Tomato mutter paneer 500
4 Onion mutter paneer 300
5 Ginger mutter paneer 300
6 Capsicum mutter paneer 300
7 Sugar mutter paneer 200
8 Tea mutter paneer 100
9 Onion mutter paneer 500
10 Sugar mutter paneer 200
V_VALUES varchar2(4000);
sql_query varchar2(4000);
SELECT
LISTAGG(''''||ITEMNAME||'''',',')WITHIN GROUP (ORDER BY ITEMNAME)as ITEMNAME
INTO V_LIST
FROM(SELECT DISTINCT ITEMNAME FROM tblItem);
sql_query : = 'SELECT * FROM (
SELECT ItemName,RecipeName,Sum(Quantity) as Quantity from tblItem group by ItemName,RecipeName)
PIVOT (
sum(Quantity) for ItemName in (' ||V_LIST|| ')
)';
OPEN p_cursor
FOR sql_query;

Resources