SQL Server : count items in different columns and group them - sql

Looked through a few posts and haven't found an workable answer to this yet.
Example of a table I have:
Item 1 | Item 2 | Item 3 | Item 4 |
-----------------------------------------------------
Hamburger Fries Soda Salad
Fries Hamburger Soda
Salad Soda Soda
Hamburger Fries
Then I'm trying to count and group them so they show up like this.
4 Soda
3 Hamburger
3 Fries
2 Salad

Unpivot the data and do the count
select Items,count(1)
from yourtable
cross apply(values (Item1),(Item2),(Item3),(Item4)) CA (items)
Group by Items

Related

How to assign equal revenue weight to every location of a company in a table? Google Big Query

I am working on a problem where I have the following table:
+----------+ | +------+ | +------------+
company_id | country | total revenue
1 Russia 1200
2 Croatia 1200
2 Italy 1200
3 USA 1200
3 UK 1200
3 Italy 1200
There are 3 companies in this table, but company '2' and company '3' have offices in 2 and 3 countries respectively. All companies pay 1200 per month, and because company 2 has 2 offices it shows as if they paid 1200 per month 2 times, and because company 3 has 3 offices it shows as if it paid 1200 per month 3 times. Instead, I would like revenue to be equally distributed based on how many times company_id appears in the table. company_id will only appear more than once for every additional country in which a company is based.
Assuming each company always pays 1,200 per month, my desired output is:
+----------+ | +------+ | +------------+
company_id | country | total revenue
1 Russia 1200
2 Croatia 600
2 Italy 600
3 USA 400
3 UK 400
3 Italy 400
Being new to SQL, I was thinking this can maybe be done through CASE WHEN statement, but I only learned to use CASE WHEN when I want to output a string depending on a condition. Here, I am trying to assign equal revenue weight to each company's country, depending on in how many countries a company is based in.
Thank you in advance for you help!
Below is for BigQuery Standard SQL
#standardSQL
SELECT company_id, country,
total_revenue / (COUNT(1) OVER(PARTITION BY company_id)) AS total_revenue
FROM `project.dataset.table`
If to apply to sample data from your question - output is
Row company_id country total_revenue
1 1 Russia 1200.0
2 2 Croatia 600.0
3 2 Italy 600.0
4 3 USA 400.0
5 3 UK 400.0
6 3 Italy 400.0

Create hierarchical data from 2 tables

I have two tables like this:
ProdId ProdName
1 Mike
2 Carol
3 John
4 Marc
OrdId OrdName ProdId
100 Pizza 1
101 Subs 1
102 Calzone 2
102 Sausage 2
102 Beef 2
105 Pork 4
106 Coke 4
I need a query to populate a result like this. The hierarchy should be parent and their child and again parent followed by child.
MarkerId MarkerName MarkerParentId
1 Mike NULL
100 Pizza 1
101 Subs 1
2 Carol NULL
102 Calzone 2
103 Sausage 2
104 Beef 2
3 John NULL
4 Marc NULL
105 Pork 4
106 Coke 4
Please help . Thanks.
Has nothing to do with recursion. Basically you can get what you want with UNION
SELECT ProdId as MarkerId, ProdName as MarkerName, NULL as MarkerParentId from t1
UNION ALL
SELECT OrdId as MarkerId, OrdName as MarkerName, ProdId MarkerParentId from t2
ORDER BY MarkerId, MarkerParentId

Change the order of a table according to preferences given in another table

In SQL Server, I have a table A as below
Name Food PreferenceOrder
------------------------------
Jon Burger 1
Jon Coke 2
Jon Pizza 3
Jon Fries 4
Sam Pizza 1
Sam Coke 2
I have another table B that can override the preference order above
Name Food PreferredOverFood
--------------------------------
Jon Pizza Fries
Jon Coke Burger
Jon Fries Coke
Sam Coke Pizza
Basically here, Food should come before PreferredOverFood (Pizza > Fries)
Now, I want to reorder table A according to Table B preferences, so the result should be like
Name Food PreferenceOrder
------------------------------
Jon Burger 4
Jon Pizza 1
Jon Fries 3
Jon Coke 2
Sam Pizza 2
Sam Coke 1
I tried by using cursors, so I created a dynamic cursor, with each fetch I am updating Table B with table A preference, but since we are updating things row by row its not considering rows that violate the previous preferences, so I am getting Fries before Pizza (since Fries > Coke is run and it forgot about first preference (Pizza > Fries)).
So dynamic cursor is not working, (its not refreshing the result set after update). Can I use CTE or something to do like above. (Can also have circular dependencies, but not too worried about it for now)
Thanks

Joining tables with different column name but same value in sqlite

I'm using SQLite to work with my database
I have two different tables, with key columns that have different names but the same value.
As such:
shoes
Identification | Name | Shoe size
1 Bob 10
2 John 12
payment
PaymentID | Price | Year
1 20 2013
2 38 2015
I need
Identification(or PaymentID, no matter) | Name | Shoe size | Price | Year
1 Bob 10 20 2013
2 John 12 38 2015
I've been searching, and trying to understand the tutorials to no avail. I guess im just too stupid
select s.identification, s.name, s.`shoe size`, p.price, p.year
from shoes s
join payment p on p.paymentid = s.identification

Grouping & Aggregation about Character linking[in SQL HANA DATABASE]

My data frame like this:
> mydata
time id product
1 201301 1 apple
2 201302 1 htc
3 201302 1 apple
4 201302 2 htc
5 201302 2 apple
How get the following result?
> result
time id product
1 201301 1 apple
2 201302 1 apple&htc
3 201302 2 apple&htc
I have tried ddply()function like this:
ddply(mydata,.(time,id),summarise,paste0(product,"&",product))
But it doesn't work as my expectation.Thanks for your answers.
And,my point is, how to realize in SQL in SAP HANA DATABASE? Thanks a lot!
you can try this:
ddply(tab, .(time, id), summarise, product = paste(product, collapse="&"))
hth