obiee count distinct by as part of total count distinct - oracle

I need create following "numeric distribution" report in OBIEE :
item name,
count(distinct item_id by item_name),
count(distinct item_id by item_name)/count(distinct item_id)
(third column should be percent of count distinct by item name / count distinct by total)
How to get total count distinct ? I'm stuck and i will be appreciate any help.
The result should be:
Item 1 , 10, 50%
Item 2 , 20, 100%
where total number of customers is 20, and 10 od them buy Item_1, and 20 of them buy Item_2

Click on Measure in Pivot table then click on 'show Data as'>Percent of>Column.
Thats it

Related

Fetch substring from right side

I am using oracle. i have a column "ITEM_FINAL" consisting of a string which is a combination of 2 records.
ITEM_FINAL = ITEM_ID + ITEM
I need to segregate ITEM_FINAL and put them into 2 different columns ITEM_ID and ITEM.
However the length of the ITEM_ID is not constant, it can be 1 or 10 or 999 (till 3 digits).
ITEM is constant and it will be 5 digits always.
Eg. ITEM_FINAL = 1256789 (combination of 12 + 56789) or 256789 (combination of 2 + 56789)
I can fetch ITEM details with help of substr
select substr(ITEM_FINAL,-5) from dual;
It will give the last 5 digits (56789) which is constant. How can i fetch the remaining string from 6th position till the start irrespective of length from right hand side for ITEM_ID column
select substr(ITEM_FINAL, 1, length(ITEM_FINAL)-5) ITEM_ID,
substr(ITEM_FINAL,-5) ITEM
from dual;

MS Access 2013: Top 10 Items sold in each country

I need write a query to get top 10 Items sold in each country
Database Type is MS Access 2013
Table Name: SoldItems
Fields
Country
Item
Query
This query will get me all items sold in each country ordered descending by count of sold items in each country, i only need top 10 items sold in each country
SELECT count(*) As CountOfItemsSold, Country, Item
from SoldItems
group by Country, Item
order by 2,1 desc
You will need to use a sub query that returns the Top 10 items grouped by country and use that as the basis for a filter. Something like:
SELECT S.Country, S.Item, COUNT(S.Item) AS CountOfItem
FROM SoldItems AS S
GROUP BY S.Country, S.Item
HAVING S.Item In (SELECT TOP 10 R.Item
FROM SoldItems AS R
GROUP BY R.Country, R.Item
HAVING R.Country=S.Country
ORDER BY COUNT(R.Item) DESC
)
ORDER BY S.Country, COUNT(S.Item) DESC;
Note that if there are items with an equal number of items sold, then you will get more than 10 records returned per country.
Regards,

Count Length and then Count those records.

I am trying to create a view that displays size (char) of LastName and the total number of records whose last name has that size. So far I have:
SELECT LENGTH(LastName) AS Name_Size
FROM Table
ORDER BY Name_Size;
I need to add something like
COUNT(LENGTH(LastName)) AS Students
This is giving me an error. Do I need to add a GROUP BY command? I need the view:
Name_Size Students
3 11
4 24
5 42
SELECT LENGTH(LastName) as Name_Size, COUNT(*) as Students
FROM Table
GROUP BY Name_Size
ORDER BY Name_Size;
You may have to change the group by and order by to LENGTH(LastName) as not all SQL engines let you reference an alias from the select statement in a clause on that same statement.
HTH,
Eric

How to do a limited query like MySQL (LIMIT 0, 10) on Dbase?

It is possible?
I know you can limit your queries with
SELECT TOP 10 name FROM customers ORDER BY name
but how can I get names from 50 to 100?
I need to make a pagination but I can't find a way to do it.
Thank you very much.
There is more simple way:
SELECT * FROM table1 WHERE RECNO()>50 AND RECNO()<=100
You can do two opposite sorted SELECT TOP queries.
see: http://thepcspy.com/read/paging_in_sql/
SELECT name FROM
(SELECT TOP 50 name FROM
(SELECT TOP 100 name FROM customers ORDER BY name ASC) AS a
ORDER BY name DESC
) AS b
ORDER BY name ASC
Here, 50 is page size and 100 - 50 (ie: 50) is start index.

Advance mysql query with order by

I use 2 tables to combine data to become like shown as below
SELECT name, price, MIN(price) AS minprice
FROM c, cp
WHERE c.id = cp.id
GROUP BY id
ORDER BY minprice = 0, minprice ASC
For Example:
id name price
1 apple 0
1 green apple 20
2 orange 10
3 strawberry 0
As the data result above the minprice of the group 1 is 0 But I don't want the min price take zero, but this is incorrenct if I give condition having minprice > 0 cause
I wanna my result become like this
2 orange 10
1 green apple 20
3 strawberry 0
Is it possible?
Here is the answer:
SELECT
(
SELECT name
FROM yourtable
WHERE price = _inner._MIN AND id = _inner.id LIMIT 1
)
AS _NAME,
_inner._MIN
FROM
(
SELECT id, IFNULL(MIN(NULLIF(price, 0)),0) AS _MIN
FROM yourtable
GROUP BY id
)
AS _inner
where yourtable is the name of your table.
MIN(NULLIF(price, 0)) allows you to calculate minimum value while not counting a zero.
IFNULL(<...>,0) here just means, that we need a real zero instead of NULL in result.
LIMIT 1 is on the case if we have an items with the same id and price but with different names. I think, you can freely remove this statement.

Resources