Counting entries using a sub-query in SQL - sql

commercial
clicks
1
0
2
1
2
1
3
1
4
0
5
0
5
0
6
1
7
1
7
1
8
0
9
1
9
1
10
0
The table described above (the name of the table is a table) has 2 columns. The commercial column shows the ads shown to the user (the chronological order is irrelevant). The clicks column shows whether a user who saw the link clicked on it or not (0 - did not click, 1 - clicked).
I need to make a SQL query for how many advertisements there are upon clicking one, two clicks, three clicks, etc. (so for every number of clicks in the table), using a sub-query.
Thank you.
SELECT commercial, SUM(click)
FROM table
GROUP BY commercial
ORDER BY commercial ASC
commercial
clicks
1
0
2
2
3
1
4
0
5
0
6
1
7
2
8
0
9
2
10
0
I made this so far but then i get stuck on how to make a sub-query to calculate how many commercials are clicked once or 2 times.
My expected result is:
clicks
commercial
0
1,4,5,8,10
1
3,6
2
2,7,9
3
I'm also wondering if i could get this result
commercial
clicks
1
0
2
2
3
1
4
0
5
0
6
1
7
2
8
0
9
2
10
0
Without using the SUM and GROUP BY function, please let me know if thats possible.

You need to compute a further aggregation and use BigQuery STRING_AGG for string aggregation.
SELECT num_clicks,
STRING_AGG(CAST(commercial AS STRING)) AS commercials
FROM (SELECT commercial,
SUM(clicks) AS num_clicks
FROM tab
GROUP BY commercial) clicks_per_commercial
GROUP BY num_clicks
"Without using the SUM and GROUP BY function, please let me know if thats possible.": no, you can't bypass the summing aggregation step.

Select clicks, string_agg(commercial::varchar,',' order by commercial asc) as commercials
from table
group by clicks
order by clicks asc;

Related

increase rank based on particular value in column

I would appreciate some help for below issue. I have below table
id
items
1
Product
2
Tea
3
Coffee
4
Sugar
5
Product
6
Rice
7
Wheat
8
Product
9
Beans
10
Oil
I want output like below. Basically I want to increase the rank when item is 'Product'. May I know how can I do that? For data privacy and compliance purposes I have modified the data and column names
id
items
ranks
1
Product
1
2
Tea
1
3
Coffee
1
4
Sugar
1
5
Product
2
6
Rice
2
7
Wheat
2
8
Product
3
9
Beans
3
10
Oil
3
I have tried Lag and lead functions but unable to get expected output
Here is solution using a derived value of 1 or 0 to denote data boundaries SUM'ed up with the ROWS UNBOUNDED PRECEDING option, which is key here.
SELECT
id,
items,
SUM(CASE WHEN items='Product' THEN 1 ELSE 0 END) OVER (ORDER BY id ROWS UNBOUNDED PRECEDING) as ranks
FROM

CTE Hierarchy Question - 1 table, 2 columns

I'm new to CTEs, and I am slowly starting to understand the basics.
I have a table that essentially goes in this pattern:
CUSTOMER X
CUSTOMER Y
1
1
1
2
2
3
3
4
3
5
4
5
5
6
I was wondering if a CTE would help return the numbers 1 through 6 (CUSTOMER Y) if Number 1 in CUSTOMER X had a specific column flagged for relevancy.
Customer 1 would be considered the main customer, while 2 - 6 could be stores related to said customer.
My end goal would be propogating down this relevancy flag for customers 2 - 6 if customer 1 has it and I'm currently trying to figure out how to get that full list.
I'd want the CTE to return a distinct list of customers.
CUSTOMER
1
2
3
4
5
6

Selection that repeats a group of columns based on a separate column having a different value

Hello and thanks for having a look!
I'm trying to make a complex selection from a prior query and I'm having a hard time locating a solution, in part because I'm having a hard time describing what I'm after or what to search for. Here's the rub:
What I have ([table1]):
identifier
month
item 1
item 2
xyz-1
10
0
0
xyz-2
10
0
0
xyz-1
11
1
1
xyz-2
11
1
1
What I would like if possible:
identifier
item 1 - 10
item 2 - 10
item 1 - 11
item 2 - 11
xyz-1
0
0
1
1
xyz-2
0
0
1
1
With the goal being that I have a set of Items for every month in the year (above example showing only Oct and Nov). I feel like a Group By and Join solution is what I need, but I'm stuck after spending all day on this.
Any help is appreciated!
Update 1 - Close Solution:
Using a combination of suggestions from two contributors below, I was able to rewrite my original query that generated my starting table above.
I had been running this query:
TRANSFORM First([Points]) AS ItemPoints
SELECT identifier, month
FROM [source]
GROUP identifier, month
PIVOT name;
But this created a column for month which is pretty obvious in hindsight.
The solution was the following query:
TRANSFORM First([source].Points) AS ItemPoints
SELECT [source].identifier
FROM [itemNames], [source]
GROUP BY [source].identifier
ORDER BY ScoreMonth & [itemNames].ItemId
PIVOT ScoreMonth & [itemNames].ItemId;
Where [itemNames] is a query that returns a list of unique item names, the "item 1", "item 2" bit.
This resulted in the following table:
identifier
10item 1
10item 2
11item 1
11item 2
xyz-1
0
0
1
1
xyz-2
0
0
1
1
Which I can work with :)
Update 2 - Wasn't Solved
Soon after I posted that the solution was found, I realized that the values were incorrect because of the aggregate function on the Transform line. I'm looking into this again, and the solution presented below with the DlookUp() function.
Update 3 - Solution
I found that the problem with my Transform query was that the ORDER BY and PIVOT lines needed to reference the [source] table and not the [itemNames] table. I also changed the First() function to Min(), but either work :)
TRANSFORM Min([source].Points) AS ItemPoints
SELECT [source].identifier
FROM [source]
GROUP BY [source].identifier
ORDER BY ScoreMonth & '_' & [source].ItemId
PIVOT ScoreMonth & '_' & [source].ItemId;
What your are trying to do is called a cross-tab query. However your query has 2 column headings Month and Item. Month is invisible. Access and Excel only allow 1 column heading in a cross-tab query. So my solution is to manually generate the 2 column cross-tab.
So for the manual cross-tab we need to do a cross-join query to generate all the rows and columns we will need. Then we use a calculated-field and the dlookup function to insert the correct value from Table1 into the correct cell in the upcoming cross-tab querie's root query.
ItemNames is a table of all the item names, Months (here 2 months), Identifiers is all identifiers. Since we cant have 2 columns with the same name we are going to use ItemMonth as a work around.
Value: Nz(DLookUp("item1","Table1","identifier = '" & [identifier] & "' AND monthnumber = " & [MonthNumber]),0)
ItemMonth: [MonthNumber] & [ItemName]
'Table 1
ID identifier monthnumber item1 item2
3 xyz-1 10 0 0
4 xyz-2 10 0 0
5 xyz-1 11 1 1
6 xyz-2 11 1 1
'after cross join : query1 in picture
MonthNumber ItemName identifier Value ItemMonth
10 Item 1 xyz-1 0 10Item 1
11 Item 1 xyz-1 1 11Item 1
10 Item 2 xyz-1 0 10Item 2
11 Item 2 xyz-1 1 11Item 2
10 Item 1 xyz-2 0 10Item 1
11 Item 1 xyz-2 1 11Item 1
10 Item 2 xyz-2 0 10Item 2
11 Item 2 xyz-2 1 11Item 2
The cross-tab set up is as simple as it gets. The cross-tab is a type of totals query so pay attention to the summary functions:
'result of our cross-tab query but the column names are still wrong so we will fix that with a report
identifier 10Item 1 10Item 2 11Item 1 11Item 2
xyz-1 0 0 1 1
xyz-2 0 0 1 1
To generate the repot I just selected the cross-tab query and hit report. Then went into design mode and edited all the column labels. If you have to do this whole process frequently or have a whole lot of items use VBA to auto adjust the report labels.
Aside 1. In Access, cross-tab queries bug-out if they are based on even slightly complicated queries. If your cross-tab is based on a query and it doesn't work try turning the cross-tab's query into a table with a make-table query. Then base the cross-tab on the new table.
Aside 2. I suggest Looking at the cross-tab as only a way to view your data. Every time you add or subtract an item to this data that results in adding or subtracting 12 columns to the cross-tab. that means you have to adjust any reports and forms based on a cross-tab every time the items change. Way to much work.

DB Query matching ids and sum data on columns

Here is the info i have on my tables, what i need is to create a report based on certain dates and make a sum of every stock movement of the same id
Table one Table Two
Items Stocks
---------- ---------------------------
ID - NAME items_id - altas - bajas - created_at
1 White 4 5 0 8/10/2016
2 Black 2 1 5 8/10/2016
3 Red 3 3 2 8/11/2016
4 Blue 4 1 4 8/11/2016
2 10 2 8/12/2016
So based on a customer choice of dates (on this case lets say it selects all the data available on the table), i need to group them by items_id and then SUM all altas, and all bajas for that items_id, having the following at the end:
items_id altas bajas
1 0 0
2 11 7
3 3 2
4 6 4
Any help solving this?
Hope this will help:
Stock.select("sum(altas) as altas, sum(bajas) as bajas").group("item_id")

MDX: iif condition on the value of dimension

I have 1 Virtual cube consists of 2 cubes.
Example of fact table of 1st cube.
id object_id time_id date_id state
1 10 2 1 0
2 11 5 1 0
3 10 7 1 1
4 10 3 1 0
5 11 4 1 0
6 11 7 1 1
7 10 8 1 0
8 11 5 1 0
9 10 7 1 1
10 10 9 1 2
Where State: 0 - Ok, 1 - Down, 2 - Unknown
For this cube I have one measure StateCount it should count States for each object_id.
Here for example we have such result:
for 10 : 3 times Ok , 2 times Down, 1 time Unknown
for 11 : 3 times Ok , 1 time Down
Second cube looks like this:
id object_id time_id date_id status
1 10 2 1 0
2 11 5 1 0
3 10 7 1 1
4 10 3 1 1
5 11 4 1 1
Where Status: 0 - out, 1 - in. I keep this in StatusDim.
In this table I keep records that should not be count. If object have status 1 that means that I have exclude it from count.
If we intersect these tables and use StateCount we will receive this result:
for 10 : 2 times Ok , 1 times Down, 1 time Unknown
for 11 : 2 times Ok , 1 time Down
As far as i know, i must use calculated member with IIF condition. Currently I'm trying something like this.
WITH MEMBER [Measures].[StateTimeCountDown] AS(
iif(
[StatusDimDown.DowntimeHierarchy].[DowntimeStatus].CurrentMember.MemberValue
<> "in"
, [Measures].[StateTimeCount]
, null )
)
The multidimensional way to do this would be to make attributes from your state and status columns (hopefully with user understandable members, i. e. using "Ok" and not "0"). Then, you can just use a normal count measure on the fact tables, and slice by these attributes. No need for complex calculation definitions.