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

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

Related

SQL query that I have set up the algorithm but cannot write the code

I could not find keywords to describe in the title.
I have a problem and I just can explain with example, I have a table like this
user_id | transaction_id | bonus_id | created_at
1. 1 4 2021-05-01
1 3 65 2021-05-01
1 4 4 2021-05-02
1 1 5 2021-05-02
1. 3 76. 2021-05-03
1 2 5 2021-05-03
Due to a mistake I made in php here, transaction id 3 and bonus id 65 but the bonus id 4 that should be
I need to replace all transactions from transaction type 1 to the next transaction type 1 with the bonus id of the first transaction_type_1.
but of course I have to do this for every user. How can I do that?

SQL Query for a table

I’m looking for a little assistance. I have a table called equipment. One row is an order of some type of equipment.
Here are the fields:
num_id date player_id order_id active jersey comment
BIGINT DATE BIGINT BIGINT CHAR(1) CHAR(3) VARCHAR(1024)
11 2018-01-01 123 1 Y XL
11 2018-01-01 123 2 Y M Purple
11 2018-01-01 123 3 Y L White, Red
13 2018-01-11 456 1 N S Yellow, Light Blue
14 2018-02-01 789 1 Y M Orange, Black
15 2018-02-02 101 1 Y XL Shield
15 2018-02-02 101 2 Y XL Light Green, Grey
I need to write a query that shows one row for each month with the columns
Month
Total Orders
Total Products ordered
And one extra column for a total count of each size sold.
Is this easy? Any help would be appreciated.
EDIT: To answer people's questions below, SQL Server is the dbms. My apologies. As well, I am struggling as I don't know how to get the month from a date. And then adding the column for size counts has me baffled, but I haven't fully investigated that portion. I feel like the rest I have done individually, just never did it in one succinct query.
It looks weird here and I don't know how to add a table to stackoverflow, so I'll try to make it a little more visually appealing here:
The end goal I think would be like this:
Month Total Orders Total Products Ordered Size Count
January 1 3 S-0, M-1, L-1, XL-2
February 3 6 S–1, M–2, L–1, XL–3
Or this:
Month Total Orders Total Products Ordered S Count M Count L Count XL Count
January 1 3 0 1 1 2
February 3 6 1 2 1 3
You need PIVOT.
It basicly turns rows into columns, which exactly is your case.
https://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query

Increment a counter in a sql select query and sum by value?

I'm trying to do something a little different in a Pervasive DB.
I have a query result that comes out a little like this
part# Qty
----------------
A123 10
A123 3
B234 13
B234 43
A123 24
I can sort them with no problem. What I need is to iterate over the unique values and get a total count of them so I get something like this:
part# Qty Num OfNum
-----------------------------
A123 10 1 3
A123 3 2 3
B234 13 1 2
B234 43 2 2
A123 24 3 3
I am stuck on how to this in Pervasive.
Would something like this work for you:
SELECT t1.PartNumber, t1.Qty, (SELECT COUNT(PartNumber) FROM "myTable" WHERE PartNumber = t1.PartNumber) AS "OfNum"
FROM "myTable" AS t1
...replacing names as appropriate, of course.

Unconventional Unpivot and Join

I have looked around and I have not found anything exactly like this so I thought it might be helpful to everyone to ask. The long story short is that I have a column with a year followed by columns of months which contain numbers:
[year] | [month1] | [month2]
1999 4 2
Basically, I want to unpivot the month so I can name it something normal like "March" or "03" while keeping the year to the left and the number to the right:
[year] | [month] | [numbers]
1999 month1 4
1999 month2 2
Finally, I want to take that into Report Builder but I will leave that for later. Here is a copy of the data with which I am working.
I really hope this is descriptive enough because this have been bugging me for a couple of days.
CSYEAR CSOR01 CSOR02 CSOR03 CSOR04 CSOR05 CSOR06 CSOR07 CSOR08 CSOR09 CSOR10 CSOR11 CSOR12
1999 2 0 0 0 1 2 0 3 1 4 0 3
2000 4 1 3 3 2 2 2 2 4 1 4 4
1999 CSOR01 2
1999 CSOR02 0
1999 CSOR03 0
1999 CSOR04 0
1999 CSOR05 1
1999 CSOR06 2
1999 CSOR07 0
1999 CSOR08 3
1999 CSOR09 1
1999 CSOR10 4
1999 CSOR11 0
1999 CSOR12 3
2000 CSOR01 4
2000 CSOR02 1
2000 CSOR03 3
2000 CSOR04 3
2000 CSOR05 2
2000 CSOR06 2
2000 CSOR07 2
2000 CSOR08 2
2000 CSOR09 4
2000 CSOR10 1
2000 CSOR11 4
2000 CSOR12 4
Thank you all for any help you can recommend.
SELECT CSYEAR, MONTH, NUMBER
FROM myTable
UNPIVOT
(
NUMBER
for MONTH in (CSOR01, CSOR02, CSOR03, CSOR04, CSOR05, CSOR06, CSOR07, CSOR08, CSOR09, CSOR10, CSOR11, CSOR12)
) u;
SQLFiddle here
I don't have a high enough reputation score to comment on #PinnyM's excellent answer, but if you were looking for the name of the month you could modify it to do the following:
SELECT
CSYEAR,
MONTH,
DATENAME(Month, CAST(RIGHT(MONTH,2) AS VARCHAR(2)) + '/01/1900'),
NUMBER
FROM myTable
UNPIVOT
(
NUMBER
for MONTH in (CSOR01, CSOR02, CSOR03, CSOR04, CSOR05, CSOR06, CSOR07, CSOR08, CSOR09, CSOR10, CSOR11, CSOR12)
) u;
SQL Fiddle

Creating a new column based on data from an existing column

Consider a system to track repairs. I have a table with customer data and a common id key. I have a second table with a column to show which type of repair part was used on each id key and how many were used. Their definitions are below:
order_information
order_id | cust_last_name
465 Smith
899 Williams
512 Johnson
345 Fenton
122 Bowles
944 Cooper
parts_usage
order_id | part_type | part_quantity
465 Part 1 5
465 Part 2 4
899 Part 1 2
899 Part 2 8
899 Part 3 6
512 Part 3 1
345 Part 2 4
345 Part 3 5
122 Part 2 3
944 Part 1 2
I'd like to run a query for reporting that will return the part's pieces broken out like so:
order_id | Part 1 | Part 2 | Part 3 | Total
465 5 4 9
899 2 8 6 16
512 1 1
345 4 5 9
122 3 3
944 2 2
Is it possible to do this with a query so that my reports can show how many of each part was used on each repair ticket?
As you can see, each order_id can have multiple part types and unique quantities. I want to break the different part types (I have 3 total) into 3 separate columns with their totals listed by order_id.
select order_id, [Part 1], [Part 2], [Part 3], Total
from
(
select oi.order_id
, part_type
, part_quantity
, Total = sum(part_quantity) over (partition by oi.order_id)
from order_information oi
inner join parts_usage pu on oi.order_id = pu.order_id
) as parts
pivot
(
sum(part_quantity) for part_type in ([Part 1], [Part 2], [Part 3])
) as pvt
order by order_id
This works for me.
I have ordered the resultset by order_id as well; there doesn't appear to be a specific order in your example results but it is mentioned in the question details.
You can see the key is to combine the PIVOT with a SUM aggregate window function.
SQL Fiddle with demo.