I have 2 tables in sql table_a and table_b this is the output: (1 to many relationship)
table_a table_b
id_no (pk) name id_no (fk) id_tabl(pk) order_code order_item
1 a 1 1 11 aple
1 a 1 2 12 orange
1 a 1 3 13 ice
2 b 2 4 12 orange
2 b 2 5 13 ice
3 c 3 6 13 ice
3 c 3 7 12 orange
3 c 3 8 11 aple
I want to display only 1 name with all his order_item.
How can I display it using iReport in the xml?
The output sample:
id_no name order_item
1 a aple
orange
ice
2 b orange
ice
3 c ice
orange
aple
Using only 2 (order_item) field pattern in every pages of my invoice
the other display will be displayed in invoice pages 2.
You should use Data Grouping.
You can read this article about data grouping.
Your can use the query like this:
SELECT table_a.id_no, table_a.name, table_b.order_item FROM table_a, table_b WHERE table_a.id_no=table_b.id_no ORDER BY table_a.name
Note: may be you need to add sort by table_a.id_no column.
You should create the iReport's group for the name field
Note: may be you need to create two groups - for id_no and name fields.
You can use the Group and Details bands for drawing the data row. Or you can put all textField elements to the Detail band. In this case you should set false value to the isPrintRepeatedValues textField's property (for id_no and name fields).
Related
I have two tables department and category.In my query result listing same department multiple times.I want to group departments based on category table priority column.
Eg Data:
id dept_name dept_category priority
1 Cardio category 2 2
2 Ortho category 3 3
3 Ortho category 1 1
4 ENT category 1 1
5 Ortho category 2 2
I wannt the reslt like:
id dept_name dept_category priority
1 Cardio category 2 2
3 Ortho category 1 1
4 ENT category 1 1
How can i fetch the resul like this.In my category table cantain priority 1,2,3 then 1 is highest priority.I need to group department based on highest priority.
We can use DISTINCT ON with Postgres:
SELECT DISTINCT ON (dept_name) *
FROM yourTable
ORDER BY dept_name, priority;
I can't explain my problem in English well. So I write my problem in a personal way.
user_id name surname
1 john great
2 mary white
3 joseph alann
event_id official_id assistant_id date
1 1 2 2017-12-19
2 1 3 2017-12-20
3 2 3 2017-12-21
I want to get names at the same time when I query an event. I tried:
SELECT * FROM event a, user b WHERE a.official_id=b.user_id AND a.assistant_id=b.user_id
When I use "OR" instead of "AND" gives me cartesian result. I want the result like:
event_id off_id off_name asst_id asst_name date
1 1 john 2 mary 2017-12-19
2 1 john 3 joseph 2017-12-20
3 2 mary 3 joseph 2017-12-21
In my Table 1, It may have AND have a null entry in the address column to corresponding record OR not have a matching entry in Table 2.
I want to present all the records in Table 1 but also present corresponding entries from Table 2. My RESULT is what I am trying to achieve.
Table 1
ID First Last
1 John Smith
2 Bob Long
3 Bill Davis
4 Sam Bird
5 Tom Fenton
6 Mary Willis
Table 2
RefID ID Address
1 1 123 Main
2 2 555 Center
3 3 626 Smith
4 4 412 Walnut
5 1
6 2 555 Center
7 3
8 4 412 Walnut
Result
Id First Last Address
1 John Smith 123 Main
2 Bob Long 555 Center
3 Bill Davis 626 Smith
4 Sam Bird 412 Walnut
5 Tom Fenton
6 Mary Willis
You need an outer join for this:
SELECT * FROM Table1 t1 LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.RefID
How do you join those two tables? If table 2 have more than 1 matched address, how do you want display them? Please clarify in your question.
Here is a query based on my assumptions.
SELECT
ID, First, Last,
Address = (SELECT MAX(Address) FROM Table2 t2 WHERE t1.ID = t2.ID)
FROM Table1 t1
I am having a problem with my query. I have 2 tables:
Table 1 is AutoCompany it has fields company and CodeCar. CodeCar can be 3 or 4 depending on the type of car that company has.
table 1: AutoCompany
company| CodeCar|
jora 3
jora 4
jora 3
ghita 3
ghita 3
ghita 4
gheorghe 4
gheorghe 3
gheorghe 3
Table 2 CodeCarCompanies has the codes:
car | codeCar
mers 3
vW 4
I need to select the companies with the count of the occurance of the 2 codeCars
resulting in something like this:
company | MERS| VW
jora 2 1
ghita 2 1
gheorghe 2 1
My attempt so far:
SELECT COUNT(dbo.AutoComany) AS MERS, dbo.Company, COUNT(dbo.AutoComany.
[CodeCar]) AS VW,
FROM dbo.AutoComany FULL OUTER JOIN
dbo.AutoComany ON dbo.АВТОМОБ.КодПредпр = AutoCompany.company
WHERE (dbo.CodeCarComapnies.[CodeCar] = 3)
GROUP BY dbo..company, dbo.CodeCarComapnies.[CodeCar]
HAVING (dbo.CodeCarComapnies.[CodeCar] = 4)
In MS Access, I think you want:
SELECT codecarcomapnies.car,
Count(autocompany.codecar) AS CountOfCodeCar
FROM autocompany
INNER JOIN codecarcomapnies
ON autocompany.codecar = codecarcomapnies.codecar
WHERE autocompany.codecar IN ( 3, 4 )
GROUP BY codecarcomapnies.car;
The above was built using the MS Access query design window and the Sum Σ button
Edit re Comment
SELECT Sum(IIf([autocompany].[codecar]=3,1,0)) AS mers,
Sum(IIf([autocompany].[codecar]=4,1,0)) AS vw
FROM autocompany
Or
TRANSFORM Count(autocompany.CodeCar) AS CountOfCodeCar
SELECT "Total" AS Total
FROM autocompany
INNER JOIN CodeCarComapnies
ON autocompany.CodeCar = CodeCarComapnies.codeCar
WHERE autocompany.CodeCar In (3,4)
GROUP BY "Total"
PIVOT CodeCarComapnies.car
I would like a query that will show a sum of columns with a default value for missing data. For example assume I have a table as follows:
type_lookup:
id name
-----------
1 self
2 manager
3 peer
And a table as follows
data:
id type_lookup_id value
--------------------------
1 1 1
2 1 4
3 2 9
4 2 1
5 2 9
6 1 5
7 2 6
8 1 2
9 1 1
After running a query I would like a result set as follows:
type_lookup_id value
----------------------
1 13
2 25
3 0
I would like all rows in type_lookup table to be included in the result set - even if they don't appear in the data table.
It's a bit hard to read your data layout, but something like the following should do the trick:
SELECT tl.type_lookup_id, tl.name, sum(da.type_lookup_id) how_much
from type_lookup tl
left outer join data da
on da.type_lookup_id = tl.type_lookup_id
group by tl.type_lookup_id, tl.name
order by tl.type_lookup_id
[EDIT]
...subsequently edited by changing count() to sum().