I have a SSRS report with dataset as follows:
Record 1 Item 001 Qty 10 Category A
Record 2 Item 002 Qty 10 Category A
Record 3 Item 003 Qty 10 Category B
Record 4 Item 004 Qty 10 Category B
Record 5 Item 005 Qty 10 Category B
I Add Group by Category (group G1) and hide rows of Item 001, Item 002, and so on. So the report displayed 2 rows as below:
Category A Qty 20 Category row 1
Category B Qty 30 Category row 2
To limit maximum rows per page, I add parent group (group G2). And with G2 group expression of "Int((RowNumber(Nothing)-1)/10)", but it did not count/return 10 rows of category, but still count 10 rows of Item 001, 002, ...
I tried to change my G2 group expression to "=Int((RunningValue(Fields!Category.Value,CountDistinct, "T1")-1)/10)",
but it gave me error "Unable to issue because there is no layout file" when running the report.
Please advise, so that I can limit rows per one page, if the visible rows are the Category rows (not the Item rows).
Thank you.
Related
I am fairly new to SQL and I am struggling with generating a table with multiple aggregates based on certain filters.
My temporary able has 3 columns where an individual is unique by Category ID AND Customer ID (i.e. there can be multiple identical Customer IDs across categories).
Category ID
Customer ID
Number of Purchases
X
A
4
X
B
10
X
C
2
Y
A
2
Y
E
6
Z
A
10
Z
C
5
Z
D
7
The output table I am looking for is basically 4 columns: grouped by category ID identifying total number of customers who have at least 3 purchases, customers who have at least 5 purchases as well as total purchases across all customers within that category. The output table would look like this:
Category_ID
Total_Customers_3
Total_Customers_5
Total_Purchases
X
2
1
16
Y
1
1
8
Z
3
3
22
When I input the following code, I get the correct total purchases column per category but the total number of customers who had at least 3 purchases per category is incorrect as the aggregates are identical across all categories.
My code:
SELECT table.categoryID AS Category_ID
(SELECT COUNT (table.customerID)
FROM table
WHERE table.purchases >=3) AS Total_Customers_3,
(SELECT COUNT (table.customerID)
FROM table
WHERE table.purchases >=5) AS Total_Customers_5,
SUM (table.purchases) AS Total_Purchases,
FROM table
GROUP BY Category_ID, Total_Customers_3, Total_Customers_5,
(P.S When I try to just GROUP BY Category_ID, I get an ERROR that syntax is incorrect because I have sub-queries in my SELECT statement.
The incorrect table (i.e. same number of total customers meeting the condition across all categories) I get looks like:
Category_ID
Total_Customers_3
Total_Customers_5
Total_Purchases
X
2
1
16
Y
2
1
8
Z
2
1
22
Update: I should have included this in the original question but I am actually looking to generate 2 columns in the same about total number of customers based on 2 conditions - one for customers with at least 3 purchases and another column for customers with at least 5 customers.
Aggregate by category, and the use conditional aggregation to get the counts and totals.
SELECT
Category_ID,
SUM(purchases >= 3) Total_Customers_3,
SUM(CASE WHEN purchases >= 3 THEN purchases ELSE 0 END) AS Num_Purchases_3,
SUM(purchases >= 5) Total_Customers_5,
SUM(CASE WHEN purchases >= 5 THEN purchases ELSE 0 END) AS Num_Purchases_5
FROM yourTable
GROUP BY Category_ID;
Demo
I have 'products' table and related 'variations' table, one product can have one or more variations. 'variations' table has 'status' column, its value can be 0 or 1. I want to get the number of products (COUNT()) which have at least one variation of status 1. How to make a query that would do that?
[EDIT]
Ok, I thought that if I simplify the question I will get away with the table structure, but, here we go (only columns relevant to the question and some mock data):
It's actually 3 linked tables:
table 1: 'products'
id
name
1
t-shirt
2
shoes
3
shorts
table 2: variations
id
product_id
1
1
2
1
3
2
4
2
5
3
6
3
7
3
table 3: stock
variation_id
quantity
status [0 or 1]
1
10
1
2
15
1
3
0
0
4
0
0
5
0
0
6
3
1
7
0
0
So, with this data, I want to know how many products there are that have at least 1 of its 'variations' of 'status' 1 - in this example it would be 2 (product 1 and 3 have some variations with status 1, product 2 does not).
You just need SUM all the quantity GROUP BY products.id with criteria is stock.status equal 1.
SELECT id, name, SUM(quantity) AS total_quantity
FROM Products pr
LEFT JOIN Variations va ON pr.id = va.product_id
LEFT JOIN Stock st ON st.variation_id = va.id
WHERE st.status = 1
GROUP BY pr.id
Join two tables and apply where filter on status column
select count(*) as cnt
from
products p
join variations v
on p.product_id = v.product_id
where status = 1
An example of my Progress database, opdetail table
invoice invline article size qty
----------------------------------------
905155 1 Shoe 10 5
905155 2 Slipper 3 2
905155 2 Slipper 4 6
905155 2 Slipper 5 1
905156 1 Boot 10 1
905156 1 Boot 11 1
905157 1 Slipper 5 4
905157 2 Shoe 8 6
a simple SQL select statement, run from the OpenEdge editor returns just what I need, a list of invoices with their total quantities:-
SELECT invoice, sum(qty) FROM opdetail GROUP BY qty ORDER BY invoice ASC
905155 14
905156 2
905157 10
HOWEVER:-
When run from an ASP page via DSN I have to list both fields in the GROUP BY otherwise progress returns a GROUP BY error
SELECT invoice, sum(qty) FROM opdetail GROUP BY qty, invoice ORDER BY invoice ASC
905155 5
905155 9
905156 2
905157 4
905157 6
Its not summarizing the qty, and seems to be taking into account the line number even though the line number plays no part in my sql statement. Can anyone throw any light on this or how I can do a sum of the total qty taking into account the line number? Thanks!
You are using qty in the aggregate function and then using on the group by this makes no sense and you should group by on some other column something as
SELECT
invoice,
sum(qty) FROM opdetail
GROUP BY invoice ORDER BY invoice ASC
I have a table containing jobs to be invoiced. Each row contains two columns, 'value' and 'group'. Like this
ID Value Group
1 2000.00 1
2 2000.00 1
3 1000.00 0
4 1000.00 0
What I need to do is combine the values in Rows 1 and 2 (because they have the same group number), then return rows 3 and 4 as normal (so, not grouped together):
4000 //Rows 1 and 2 combined
1000 //Row 3 returned as whole value
1000 //Row 4 returned as whole value
I've tried to use GROUP BY in the query, so something like
SELECT SUM(Value) AS totalValue FROM table GROUP BY Group
However this returns
4000
2000 //Row 3 and 4 combined
It's combining Row 3 and 4 because they share the same Group number, its grouping them together.
My problem is, I don't want them grouped together. I want them to return separately as they have a value of zero. Is there any way for me to do this?
SELECT Group AS Unique_ID, SUM(Value) AS totalValue FROM table WHERE Group>0 GROUP BY Group
UNION
SELECT id AS Unique_ID, Value As totalValue FROM table WHERE Group=0
E.g: data table with these records:
data_id data category
1 hello 1
2 world 1
3 john 1
4 kevien 2
5 apple 2
6 bannana 3
7 desk 4
And how can I count the categories total number in one query in MySQL!?
[UPDATE]
How about count total number in special categories.Such as only category 1 and 3??
Thank you very much!!
In response to the question in the comment, include the where clause to restrict it:
SELECT category, count(*) from table
where category in (1, 3)
group by category
select category, count(*)
from theTable
group by category