Pivoting Flashcards

1
Q

What is it called when you turn rows into columns in your query result?

A

Pivoting

Last Revised: 4/2/21, 4.3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the process that gives you more control of the granularity of a query result and which columns are returned?

A

Pivoting

Last Revised: 4/2/21, 4.3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False: Pivoting is particularly useful for data models where each row of a table can be a different attribute, but you need those attributes as columns in a query result.

A

True

Last Revised: 4/2/21, 4.3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What should you do before you begin pivoting?

A

Determine what you want each column in you SELECT clause to return. Some columns may need pivoting, some may not.

(Last Revised: 4/2/21, 4.5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

One way to pivot data is by using the _______ statement inside of an aggregate function.

A

CASE

Last Revised: 4/2/21, 4.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will the syntax for each pivoted column in the SELECT clause look like?

A

AGGREGATE (CASE WHEN condition THEN true_value
ELSE other_value END) column_alias

(Last Revised: 4/2/21, 4.5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What will the aggregate function be when pivoting aggregate functions with the CASE statement?

A

The same as the original aggregate function.
• You can determine this by defining what you want to return.

(Last Revised: 4/2/21, 4.6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will the condition be when pivoting aggregate functions with the CASE statement?

A

An = comparison with what is in the original GROUP BY clause.

(Last Revised: 4/2/21, 4.6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will the true_value be when pivoting aggregate functions with the CASE statement?

A

It will be what is in the original aggregate function
• It will be the column_name

(Last Revised: 4/2/21, 4.6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will the other_value be when pivoting aggregate functions with the CASE statement?

A

NULL

Last Revised: 4/2/21, 4.6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can you use DISTINCT if you’re pivoting on the count aggregate function? If so, what will the syntax look like?

A

Yes
• DISTINCT would be added inside the parentheses just before CASE

(Last Revised: 4/2/21, 4.6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can you use CASE to pivot aggregate functions and non-aggregated data?

A

Yes

Last Revised: 4/2/21, 4.5, 4.8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What will the aggregate function be when pivoting non-aggregate data with the CASE statement?

A

MAX

Last Revised: 4/2/21, 4.10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will the condition be when pivoting non-aggregate data with the CASE statement?

A

The = comparison that is in the original query

Last Revised: 4/2/21, 4.10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will the true_value be when pivoting non-aggregate data with the CASE statement?

A

It will be what is in the SELECT clause
• In the example on 4.10 cte.Value is the true_value because those are the values of the columns we are pivoting.

(Last Revised: 4/2/21, 4.10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What will the other_value be when pivoting non-aggregate data with the CASE statement?

A

NULL

Last Revised: 4/2/21, 4.10

17
Q

True or False: When pivoting aggregate functions, the GROUP BY clause may not be necessary if you’re only interested in grand totals.

A

True

Last Revised: 4/2/21, 4.10

18
Q

Do you need to have a GROUP BY clause when pivoting non-aggregate data?

A

Yes
• When pivoting non-aggregated data, you need to add the MAX aggregate function and therefore you need to have a GROUP BY clause.

(Last Revised: 4/2/21, 4.10)

19
Q

There are two additional things to watch out for when pivoting non-aggregate data vs. pivoting aggregate functions. What are they?

A
  1. The GROUP BY clause
  2. LEFT OUTER JOINS

(Last Revised: 4/2/21, 4.10)

20
Q

True or False: An alternative to a GROUP BY clause and a lot of aggregating in the main query is to do all of your pivoting in a subquery and then join to that subquery.

A

True

Last Revised: 4/2/21, 4.11

21
Q

What is an alternative to pivoting using CASE statements?

A

Join to additional copies of a table

Last Revised: 4/2/21, 4.15

22
Q

True or False: to pivot aggregate functions using copies of a table, you can use the same condition in your joins that you would otherwise use in the CASE statement.

A

True
Join to the table using a left outer join on the primary key and the condition

(Last Revised: 4/2/21, 4.15)

23
Q

When pivoting aggregate functions using copies of a table how will the SELECT clause change?

A

The SELECT clause remain essentially the same, but needs to be duplicated for each set of conditions and tweaked to reference the correct table aliases.

(Last Revised: 4/2/21, 4.16)

24
Q

True or False: to pivot non-aggregate functions using copies of a table, you can use the same condition in your joins that you would otherwise use in the CASE statement.

A

True

Last Revised: 4/2/21, 4.18

25
Q

True or False: When pivoting non-aggregated data, you need to add the MAX aggregate function and therefore you need to have a GROUP BY clause.

A

True

Last Revised: 4/2/21, 4.18

26
Q

True or False: Pivoting non-aggregated data require multiple copies of a table?

A

False
• Pivoting non-aggregated data can be accomplished with grouping and aggregate function on CASE statements.

(Last Revised: 4/2/21, 4.21)

27
Q

Which aggregate functions can be used for pivoting the COUNT aggregate function?

A

COUNT or SUM

Last Revised: 4/2/21, 4.21

28
Q

Which aggregate function is typically used for pivoting non-aggregated data?

A

MAX
• MIN would also work. AVE would also work for numeric values.

(Last Revised: 4/2/21, 4.21)