Need to create a Totals report from oracle table data - sql

I have a table in oracle similar to the the example table image that I need to have output the segregated totals as shown in the second example results image.
I need the sums of each item_type where A & B are just examples for a large number of possible items that can be added.
example table:
example results:
Any help would be appreciated. thanks

Use conditional aggregation:
SELECT
store,
SUM(CASE WHEN item_type = 'A' AND item_status = 'IN' THEN 1 ELSE 0 END) a_items_in,
SUM(CASE WHEN item_type = 'A' AND item_status = 'OUT' THEN 1 ELSE 0 END) a_items_out,
SUM(CASE WHEN item_type = 'B' AND item_status = 'IN' THEN 1 ELSE 0 END) b_items_in,
SUM(CASE WHEN item_type = 'B' AND item_status = 'OUT' THEN 1 ELSE 0 END) b_items_out
FROM mytable
GROUP BY store

Related

Column merge using sum in case Oracle APEX

I need help How can I merge the column into a single column, here is my code, is this method is correct. I want to get the count of the selected row in the table for the columns.
SELECT
CAT_MGR,
SUM ( case when CAT_MGR = 'A' THEN 1 else 0 end ) AS DESIGN,
sum (case when CAT_MGR = 'b' THEN 1 else 0 END) AS DESIGN,
sum (case when CAT_MGR = 'c' THEN 1 else 0 END) AS DESIGN
from Table_A
GROUP BY
CAT_MGR
Can you guys help me I'm a beginner at SQL.
Thank you in advance
If you want just one row in the resultset, then remove the group by clause. Then, if you want to count the three cat mgr together, you can use in:
select
sum(case when cat_mgr = 'a' then 1 else 0 end ) as design_a,
sum(case when cat_mgr = 'b' then 1 else 0 end ) as design_b,
sum(case when cat_mgr = 'c' then 1 else 0 end ) as design_c,
sum(case when cat_mgr in ('a', 'b', 'c') then 1 else 0 end ) as design
from Table_a
You just need to make addion like below in order to get one column "Design"
SELECT
CAT_MGR,
SUM (case when CAT_MGR = 'A' THEN 1 else 0 end )
+ sum (case when CAT_MGR = 'b' THEN 1 else 0 END)
+ sum (case when CAT_MGR = 'c' THEN 1 else 0 END)
AS DESIGN
from TJD_CORE_CATPB_TB
GROUP BY
CAT_MGR

Create a dataset that makes 0/1 flag any time a record is present

I've got a table that is designed to have 1 row per member per product usage per month. There are three products. Most people only use one product, but some us a combination of the others.
Ideally I'd like to have a result set that looks like:
However I'm only able to get it to show the same as it is now, but with 1's and 0's. So if a person used all three products, there would be three rows with 1's rather than one row with 3 1's.
Here's how the code looks right now:
WITH
dist_visit_typ_cte(visit_month, person_id, product_type) as
(select distinct visit_month, person_id, product_type
from product_transaction_table),
visit_count_cte (visit_month, person_id, prod_a_usage, prod_b_usage, prod_c_usage) as
(select distinct visit_month
,CASE WHEN product_type = 'A' then 1, else 0 end as prod_a_usage
,CASE WHEN product_type = 'B' then 1, else 0 end as prod_b_usage
,CASE WHEN product_type = 'C' then 1, else 0 end as prod_c_usage
from dist_visit_typ_cte
group by
visit_month
,CASE WHEN product_type = 'A' then 1, else 0
,CASE WHEN product_type = 'B' then 1, else 0
,CASE WHEN product_type = 'C' then 1, else 0
It's on an older version of SQL-Server (pre 2016).
You seem to be looking for conditional aggregation. The logic you want would look like:
select visit_month, person_id,
max(case when product_type = 'A' then 1 else 0 end) prod_a_usage,
max(case when product_type = 'B' then 1 else 0 end) prod_b_usage,
max(case when product_type = 'C' then 1 else 0 end) prod_c_usage
from dist_visit_typ_cte
group by visit_month, person_id

Introducing a simple count subquery to an already existing subquery

I am creating a simple image browser which is connected to an SQLite database. Within the browser, similar images are grouped into an event and each image is labelled with a few tags.
Someone helped me construct this very helpful query below. It contains 5 random tags as an example:
SELECT *
FROM
(SELECT
t.EventId,
SUM(CASE WHEN name = 'necktie' THEN 1 ELSE 0 END) as 'necktie',
SUM(CASE WHEN name = 'shirt' THEN 1 ELSE 0 END) as 'shirt',
SUM(CASE WHEN name = 'suit' THEN 1 ELSE 0 END) as 'suit',
SUM(CASE WHEN name = 'man' THEN 1 ELSE 0 END) as 'man',
SUM(CASE WHEN name = 'male' THEN 1 ELSE 0 END) as 'male'
FROM
TagsMSCV t
WHERE
name IN ('necktie', 'shirt', 'suit', 'man', 'male')
GROUP BY
t.EventId)
ORDER BY
COUNT(*) DESC
This returns how many of each tag (column is called 'name') show up in each event. However now I also need the size of the event (number of unique image id's in the event) which can be accomplished with the below query:
SELECT EventId, COUNT(DISTINCT ImageId)
FROM TagsMSCV
GROUP BY EventId
But I have no idea how to introduce this syntax into the subquery above? If I put it beside t.EventId, it only counts the image id's in event that are tagged with the 5 random tags which is not correct. I need the total unique image id's in the event.
First, your subquery is not necessary. Second, you can use conditional COUNT(DISTINCT):
SELECT t.EventId,
SUM(CASE WHEN name = 'necktie' THEN 1 ELSE 0 END) as necktie,
SUM(CASE WHEN name = 'shirt' THEN 1 ELSE 0 END) as shirt,
SUM(CASE WHEN name = 'suit' THEN 1 ELSE 0 END) as suit,
SUM(CASE WHEN name = 'man' THEN 1 ELSE 0 END) as man,
SUM(CASE WHEN name = 'male' THEN 1 ELSE 0 END) as male
COUNT(DISTINCT CASE WHEN name = 'necktie' THEN imageid END) as necktie_images,
COUNT(DISTINCT CASE WHEN name = 'shirt' THEN imageid END) as shirt_images,
COUNT(DISTINCT CASE WHEN name = 'suit' THEN imageid END) as suit_images,
COUNT(DISTINCT CASE WHEN name = 'man' THEN imageid END) as man_images,
COUNT(DISTINCT CASE WHEN name = 'male' THEN imageid END) as male_images
FROM TagsMSCV t
WHERE name IN ('necktie', 'shirt', 'suit', 'man', 'male')
GROUP BY t.EventId
EDIT:
If you want the total distinct images for an event, then no conditional logic is needed. Just use:
COUNT(DISTINCT imageid) as total_images,
And remove the WHERE clause.

What does a multiple count query in SQL return?

I have a product table and every product might be delivered, idle, shipping, preparing.
I want to show a list with the counts of products for each state, and I can see how to query for that here:
How to get multiple counts with one SQL query?
However, what does this query return, and how do I assign the return value to lets say, 4 integers, called deliveredCount, idleCount, shippingCount, preparingCount?
PS: For the record, I am using SQLite with OrmLite in Android with JAVA
EDIT: In this SO question people explain what Query to do when you want to get multiple counts, but they don't tell us what does that query return and in what format. For example:
SELECT a.distributor_id,
(SELECT COUNT(*) FROM myTable WHERE level='personal' and distributor_id = a.distributor_id) as PersonalCount,
(SELECT COUNT(*) FROM myTable WHERE level='exec' and distributor_id = a.distributor_id) as ExecCount,
(SELECT COUNT(*) FROM myTable WHERE distributor_id = a.distributor_id) as TotalCount
FROM myTable a ;
What is the return type of this and what is the format?
PS2: Someone was really quick to downvote my question because it lacked sufficient information. Then I edited it, but the downvote still remains :(
Hard to say for sure but sounds like you need to use a version of the top answer in the link you have provided.
Something like;
SELECT ProductID,
COUNT(*) AS Total,
SUM(CASE WHEN pStatus = 'delivered' THEN 1 ELSE 0 END) DeliveredCount,
SUM(CASE WHEN pStatus = 'idle' THEN 1 ELSE 0 END) IdleCount,
SUM(CASE WHEN pStatus = 'shipping' THEN 1 ELSE 0 END) ShippingCount,
SUM(CASE WHEN pStatus = 'preparing' THEN 1 ELSE 0 END) PreparingCount
FROM ProductTable
GROUP BY ProductID
This will return something like;
ProductID | DeliveredCount | IdleCount | ...
1 | 250 | 3250 | ...
You might want to try this.
SELECT
SUM(CASE WHEN Prod = 'delivered' THEN 1 ELSE 0 END) as deliveredCount,
SUM(CASE WHEN Prod = 'idle' THEN 1 ELSE 0 END) as idleCount,
SUM(CASE WHEN Prod = 'shipping' THEN 1 ELSE 0 END) as shippingCount,
SUM(CASE WHEN Prod = 'preparing' THEN 1 ELSE 0 END) as preparingCount
FROM Product
select
concat(state, "Count"),
count(*)
from product
group by state
Which would return 4 rows (assuming four unique values of state):
fooCount | 15
etc

methods to separate multiple count(*)s by external field

I didn't know exactly what to call this question, so apologies if the title is confusing. I'm trying to generate a summary of user activity for a particular website. This query returns the total number of hits per type of page:
select
count(case when internal_handle = 'content' then 1 else null end) as CONTENT,
count(case when data = '/webapps/assessment/take/launch.jsp' then 1 else null end) as ASSESSMENTS,
count(case when internal_handle = 'discussion_board_entry' then 1 else null end) as DISCUSSIONS,
count(case when data = '/webapps/blackboard/execute/uploadAssignment' then 1 else null end) as BB_ASSIGNMENTS,
count(case when data = '/webapps/turn-plgnhndl-BBLEARN/links/submit.jsp' then 1 else null end) as TII_ASSIGNMENTS,
count(case when data = '/webapps/osv-kaltura-BBLEARN/jsp/courseGallery.jsp' then 1 else null end) as COURSE_GALLERY,
count(case when data = '/webapps/osc-BasicLTI-BBLEARN/frame.jsp' then 1 else null end) as ECHO_360,
count(case when internal_handle = 'check_grade' then 1 else null end) as MY_GRADES
from BBLEARN.ACTIVITY_ACCUMULATOR
where course_pk1 = (select pk1 from BBLEARN.course_main where course_id = '2014FA.BOS.PPB.445.A')
and user_pk1 in (select users_pk1 from BBLEARN.course_users where role = 'S' and crsmain_pk1 = course_pk1);
So the result is like:
CONTENT,ASSESSMENTS,DISCUSSIONS,BB_ASSIGNMENTS,TII_ASSIGNMENTS,COURSE_GALLERY,ECHO_360,MY_GRADES
5787,954,335,0,0,0,837,222
My goal, though, is to have it separated by hits per user. For instance, I'd want a result like:
USER_PK1,CONTENT,ASSESSMENTS,DISCUSSIONS,BB_ASSIGNMENTS,TII_ASSIGNMENTS,COURSE_GALLERY,ECHO_360,MY_GRADES
USER_A,250,79,41,0,0,0,66,7
USER_B,144,89,82,0,0,0,24,0
USER_C,174,45,23,0,0,0,58,1
--etcetera
I can imagine running the above query iteratively, but I'm not familiar enough with Oracle to do it effectively. How might I go about doing this? Or is there a better way to get the fields I'm looking for?
Thanks in advance!
Just add a group by:
select user_pk1,
count(case when internal_handle = 'content' then 1 else null end) as CONTENT,
count(case when data = '/webapps/assessment/take/launch.jsp' then 1 else null end) as ASSESSMENTS,
count(case when internal_handle = 'discussion_board_entry' then 1 else null end) as DISCUSSIONS,
count(case when data = '/webapps/blackboard/execute/uploadAssignment' then 1 else null end) as BB_ASSIGNMENTS,
count(case when data = '/webapps/turn-plgnhndl-BBLEARN/links/submit.jsp' then 1 else null end) as TII_ASSIGNMENTS,
count(case when data = '/webapps/osv-kaltura-BBLEARN/jsp/courseGallery.jsp' then 1 else null end) as COURSE_GALLERY,
count(case when data = '/webapps/osc-BasicLTI-BBLEARN/frame.jsp' then 1 else null end) as ECHO_360,
count(case when internal_handle = 'check_grade' then 1 else null end) as MY_GRADES
from BBLEARN.ACTIVITY_ACCUMULATOR
where course_pk1 = (select pk1 from BBLEARN.course_main where course_id = '2014FA.BOS.PPB.445.A') and
user_pk1 in (select users_pk1 from BBLEARN.course_users where role = 'S' and crsmain_pk1 = course_pk1)
group by user_pk1;
If user_pk1 isn't what you mean by "user", then you might have to join in another table.