【发布时间】:2021-06-04 22:09:30
【问题描述】:
我有一张表格,其中列出了我的志愿者和他们决定写明信片数量的列。我正在尝试返回一行,根据请求的数量创建一行,将每个志愿者分配给明信片。
例如,如果志愿者 A 申请了 3 张明信片,志愿者 B 申请了 1 张明信片,志愿者 C 申请了 2 张明信片,那么我希望我的查询返回如下内容:
| rowNumber | volunteer | postcardsAssigned |
|---|---|---|
| 1 | Volunteer A | 1 |
| 2 | Volunteer A | 2 |
| 3 | Volunteer A | 3 |
| 4 | Volunteer B | 4 |
| 5 | Volunteer C | 5 |
| 6 | Volunteer C | 6 |
在下面附上一张图片,显示我当前的查询结果以及我试图让它看起来像什么。
With postcards_vols as (SELECT What_s_your_name_ as Name
, What_s_your_email_ as Email
, What_s_your_phone_number_ as Phonenumber
, Mailing_Street_Address as StreetAddress
, Mailing_City as City
, Mailing_Zip_code as Zip
, Mailing_State as State
, How_many_cards_would_you_like_us_to_send_ as Postcards_Requested
, ROW_NUMBER() OVER() AS Request_Number
, Submitted_At as date_requested
FROM volunteer_program.postcard_volunteers)
SELECT *
, SUM (Postcards_requested) OVER (PARTITION BY request_number, date_requested ORDER BY date_requested DESC) AS addresses_assigned,
FROM
postcards_vols
[Current Query Output and Sample Desired Query Output][1]
[1]: https://i.stack.imgur.com/dGssK.png
* Names and address shown in the picture are fictitious
【问题讨论】:
标签: sql arrays google-bigquery