QlikView Count with multiple Condition - qlikview

I have a problem with Count and multiple condition:
I have two fields, Product_Type and color
color has two Data {black,white}, I want to Count Product_Type in each Color and try this:
Count(Distinct(if(Color='black' or Color='White',Product_Type)))
but it doesn't work correctly,in each type it shows same number for each color.

Count only white color:
Count({<Color={'White'}>} Distinct Product_Type)
Count only black color:
Count({<Color={'Black'}>} Distinct Product_Type)
Count white AND black colors:
Count({<Color={'White','Black'}>} Distinct Product_Type)

Related

SQL Aggregation function to get concrete value

I need help with an aggregation functionality.
what I want to know is if it is possible to extract a concrete value from a grouped query on one of the columns I return, like this
STORE
fruit
color
stock
apple
red
30
apple
green
5
banana
yellow
40
berry
red
5
pear
green
5
SELECT SUM(stock), [?] FROM store GROUP BY fruit
[?] -> i need to take a concrete value, for example RED. but the SUM must have 35 in apples.
can this be done without a subquery?
Thanks
I expect this results
Column A
Column B
35
red
in this case the query does not make sense but for my personal case it does. I try to use STRING_AGG to take the data and make explode in my code, but its not the best way i think
I think you're looking for the GROUP BY clause. Try this:
SELECT SUM(stock), color
FROM store
GROUP BY color
This will return a list of all colors, and the sum of the stock for each color.
I'm not entirely clear what you mean by a "concrete value" (singlular) as there are potentially two or more values... and you did mention STRING_AGG(). Also, you omitted the "fruit" from the query, which made things a bit confusing. Nonetheless, this will get either one Color value or all color values using STRING_AGG() OR MAX() and without a sub-query:
-- the WITH is just a way to get your data into the query
;
WITH AdriansData AS
(SELECT * FROM (VALUES('apple', 'red', 30),
('apple', 'green', 5),
('banana', 'yellow', 40),
('berry', 'red', 5),
('pear', 'green', 5)
) AS X (fruit, color, stock)
)
SELECT fruit,
SUM(stock),
STRING_AGG(color, ', ') AS Colors,
MAX(color) AS JustOneColor
FROM AdriansData
GROUP BY fruit

Default comparison operand in SELECT WHERE condition

I have a table CategoryColours, if a category is not found, it should return the colors from the default category "*".
Example if the table contains these lines:
Category Color
* white
* black
1 red
1 blue
1 green
1 black
If I search the category "1", the query should get the 4 colors.
If I search the category "2", which has no records in the table, the query should get the 2 colors from the category "*".
Is it possible to use OpenSQL to get the exact list that I need in a single statement?
I tried with CASE and subqueries (EXIST) but I didn't manage.
It's not a stopper for my code, since I can just check if my category has records first or select my category + the default always and then remove the default if the other has records.
I think you can use UNION. I didn't try code, it can include type errors.
SELECT
category,
color
FROM CategoryColours
WHERE category = lv_category
UNION
SELECT
category,
color
FROM CategoryColours
WHERE category eq '*'
AND NOT EXISTS ( SELECT color
FROM CategoryColours
WHERE category = lv_category
)
INTO TABLE #DATA(lt_itab).

Count Total Bar codes of Specific Color by fetching values from various fields when they match in SQL

I have a situation where I have 4 color columns(Color1, Color2, Color3, Color4) I need to add the values of bar codes present in all color columns when they match. Its bit complicated, I have the graphical representation here:
Color1 Color2 Color3 Color4 Barcodes
Red 1
Red 3
Red 4
Red 2
Expected Result: Total Barcodes where Color is Red=10
I am using SQL Server
Any Assistance in this would be really helpful.
EDIT: there are 320 colors in the table
I would unpivot and aggregate:
select sum(Barcodes)
from t cross apply
(values (color1), (color2), (color3), (color4)) v(color)
where color = 'Red';
If you want this for each color:
select color, sum(Barcodes)
from t cross apply
(values (color1), (color2), (color3), (color4)) v(color)
group by color;

Sum of distinct items values Oracle SQL

I got data like this:
ITEM COLOR VOL
1 RED 3
2 BLUE 3
3 RED 3
4 GREEN 12
5 BLUE 3
6 GREEN 12
and I want to have the total sum of each color,
mean RED + BLUE + GREEN = 3+3+12 = 18
P.S I can't do it in sub-query since it is a part of a big query already. I am looking for a way could do it in select clause.something like
select sum(distinct(COLOR) VOL) from myTable group by COLOR
Thanks a lot!
Sum the sum of distinct, as grouped by color
select sum(sum(distinct VOL))
from MyTable
group by COLOR
Tested locally and here
One approach uses a CTE or subquery to find the mean volumes for each color. Then take the sum of all mean volumes, for all colors.
WITH cte AS (
SELECT COLOR, AVG(VOL) AS VOL -- or MIN(VOL), or MAX(VOL)
FROM yourTable
GROUP BY COLOR
)
SELECT SUM(t.VOL)
FROM cte t
sum(max(vol)) from ... group by color
will work, but it's not clear why you should need such a thing. Likely this sum can be computed (much) earlier in your query, not right at the end.
Proof of concept (on a standard Oracle schema):
SQL> select sum(max(sal)) from scott.emp group by deptno;
SUM(MAX(SAL))
-------------
10850
1 row selected.

SQL - Select top item from a grouping of two columns

I have a list of numbers attached to two separate columns, and I want to just return the first "match" of the two columns to get that data. I got close with this answer, but it only works with one field. I need it to work with a combination of fields. About ten second before I was ready to post.
Here's an example table "Item":
Item Color Area
Boat Red 1
Boat Red 2
Boat Blue 4
Boat Blue 5
Car Red 3
Car Red 4
Car Blue 10
Car Blue 31
And the result set returned should be:
Item Color Area
Boat Red 1
Boat Blue 4
Car Red 3
Car Blue 10
A much simpler way to do this:
select Item,
Color,
min(Area) as Area
from Item
group by Item
Color
Just use the MIN function with a GROUP BY.
SELECT Item, Color, MIN(area) AS Area
FROM Item
GROUP BY Item, Color
Output:
Item Color Area
Boat Blue 4
Boat Red 1
Car Blue 10
Car Red 3
SQL Fiddle: http://sqlfiddle.com/#!9/46a154/1/0
SQL tables represent unordered sets. Hence, there is no "first" of anything without a column specifying the ordering.
For your example results, the simplest query is:
select item, color, min(area) as area
from item i
group by item, color;
About ten seconds before I was ready to post the question, I realized the answer:
WITH summary AS (
SELECT i.item + ':' + i.color,
a.area,
ROW_NUMBER() OVER(PARTITION BY i.item + ':' + i.color,
ORDER BY i.item + ':' + i.color DESC) AS rk
FROM Item i
group by (i.item + ':' + i.color, i.Area)
SELECT s.*
FROM summary s
WHERE s.rk = 1
It's as simple as combining the two composite key fields into one field and grouping based on that. This might be a bit hackish so if anyone wants to suggest a better option I'm all for it.