I have a table that has multiple rows for a distinct CARD_ID listing different roles assigned to that CARD_ID. I'd like to have a query that creates a single row for each distinct CARD_ID that has multiple columns listing the different roles. See image for example of current table. Duplicates are highlighted.
So, I'd like one row for CARD_IDs 1-10, with columns in each row for Cardholder, Reconciler, and Approver.
If a particular CARD_ID doesn't have one of those roles, I'm ok with that field being null or having some other type of indicator.
One method i conditional aggregation:
select card_id,
max(iif(role = 'Reconciler', col, NULL)) as reconciler_col,
max(iif(role = 'Approver', col, NULL)) as approver_col,
max(iif(role = 'Cardholder', col, NULL)) as cardholder_col
from t
group by card_id;
col is a column that you want to pivot. You can add more than one column, just by adding more max(iif . . .) to the select.
Related
suppose I Have below table
A
B
1
one
2
two
1
three
2
four
1
last
for value in A=1
then I need the output as one;three;last
how can I query this in Oracle's SQL?
If you care whether you get the string "one;three;last" or "three;one;last" or some other combination of the three values, you'd need some additional column to order the results by (a database table is inherently unordered). If there is an id column that you're not showing, for example, that could do that, you'd order by id in the listagg.
If you don't care what order the values appear in the result, you could do something like this
select listagg( b, ';' ) within group (order by a)
from your_table
where a = 1
I have a BigQuery table which has data as shown below image
I wish to create a table out of this data which is as shown below image
So here I wish to
remove the email column data
combine the emp_type column values as comma separated value
have just 1 row per id
I tried using STRING_AGG function of BigQuery but was unable to achieve what I specified above.
The table actually has more than 30 columns but for the sake of explaining the issue i reduced it to 7 columns.
How do I combine multiple rows as one in a query?
Consider below approach
select
any_value((select as struct * except(email, emp_type) from unnest([t]))).*,
string_agg(emp_type, ', ') emp_type
from data t
group by to_json_string((select as struct * except(email, emp_type) from unnest([t])))
if applied to sample data in your question - output is
As you can see here - it will work no matter how many columns you have 30+ or 100+ . you done even need to type them at all!
I see two possible options, if you want to have uniqe row per combination of all parameters except email and emp_type:
SELECT id, name, status, `count`, is_hybrid, STRING_AGG(emp_type, ', ')
FROM data
GROUP BY id, name, status, `count`, is_hybrid
If you want to have just one row per id, you can group by id and select arbitrary value(from rows with this id) for other columns:
SELECT id, ANY_VALUE(name), ANY_VALUE(status), ANY_VALUE(`count`), ANY_VALUE(is_hybrid), STRING_AGG(emp_type, ', ')
FROM data
GROUP BY id
I have this table:
INPUT
I wish to transform it into another table, that contains
The Date/Id/Order columns (primary key columns)
A TotalCount column, containing the value of the original table's Count column where all the Cond columns are NULL
One Count column for each CondX column, containing the value of the original table's Count column where CondX = 1 and the rest of the Cond = NULL
One Count column for each combination of non-null (Cond1 OR Cond2 OR Cond3) + (CondA OR CondB), containing the value of the original table's Count column where the two applicable Cond = 1 and the rest = NULL
Example:
So basically, I want my new table to have these columns:
Date, Id, Order, TotalCount
Cond1Count, Cond2Count, Cond3Count, CondACount, CondBCount
Cond1AndCondACount, Cond1AndCondBCount, Cond2AndCondACount, Cond2AndCondBCount...
From the sample image, we'd have these values in the end:
DESIRED OUTPUT
(note: CondBCount = 0 for Order = 2, missed it in the image edition)
I'd show some SQL if I had any, but I'm actually not quite sure where to start with this problem. I could naively do a bunch of different SELECT Count WHERE ..., but I'm wondering if there's a better solution.
Without your table structure. You can sum multiple columns with sum & values, even in combination with CASE
Example:
SELECT *
FROM (
SELECT Date, Id, [Order],
(SELECT SUM(v)
FROM (VALUES (ISNULL(Cond1,0)), (ISNULL(Cond2,0)),...) AS value(v)) Cond1ACount
FROM YourTable
) sub
GROUP BY Date, Id, [Order]
I have a table in which I want to calculate two columns values based on results from multiple rows / multiple columns. The primary key is set on the first two columns (tag,qid).
I would like to set the values of two fields (serial and total).
The "serial" column value is unique for each (tag,qid) so if I have 2 records with same tag, I must have record one with serial# 1 and record two with serial# 2 and so on. The serial must be calculated with accordance to priority field in which higher priority values must start serializing first.
the "total" column is the total number of each tag in the table
I would like to do this in plain SQL instead of creating a stored procedure/cursors, etc...
the table below shows full valid settings.
+----+----+--------+-------+-----+
|tag |qid |priority|serial |total|
+--------------------------------+
|abc | 87 | 99 | 1 | 2 |
+--------------------------------+
|abc | 56 | 11 | 2 | 2 |
+--------------------------------+
|xyz | 89 | 80 | 1 | 1 |
+--------------------------------+
|pfm | 28 | 99 | 1 | 3 |
+--------------------------------+
|pfm | 17 | 89 | 2 | 3 |
+--------------------------------+
|pfm | 64 | 79 | 3 | 3 |
+----+----+--------+-------+-----+
Many Thanks
You can readily return a result set with this information using window functions:
select tag, qid, priority,
row_number() over (partition by tag, qid order by priority desc) as serial,
count(*) over (partition by tag, qid) as total
from table t;
I have a table 'tblBaseResults' setup like this
Column 1 = Name
Column 2 = Activity
The Activity column is supposed to be unique. However there are multiple Name fields. So im not sure how to get the Activity column to unique values and keep just one value from Name.
I would prefer to just keep the values from name that DO NOT have any '?' characters.
The current Count query i have works great. However i need only ONE of the Name fields to carry over with it
SELECT tblBaseResults.Activity, Count(tblBaseResults.Activity) AS CountOfActivity INTO tblCountResults
FROM tblBaseResults
GROUP BY DISTINCT tblBaseResults.Activity;
In Excel if i do a VLookup i get what i need, but want to do it in access.
INSERT INTO tblCountResults (Activity, Name, CountOfActivity)
SELECT Activity, min(Name), Count(tblBaseResults.Activity)
FROM tblBaseResults
WHERE instr(Name, '?') = 0
GROUP BY Activity