多列上的动态数据透视表

时间:2017-09-21 06:29:23

标签: sql sql-server sql-server-2008

我有一个表(称为事务),其中包含与事务相关的信息。交易可以有多个评论和附带的详细信息(详细信息不是强制性的),但有些交易不会对它们发表任何评论。对于针对事务做出的每个注释,在表中创建的新记录包含同一事务的所有记录的某些字段(日期,时间,数量,类型)的相同数据,而记录中的某些字段仅涉及该特定事务。评论和细节。

请参阅以下示例数据:

+-----------+---------+---------+------+----------------+-----------------+
| Date      | Time    | Amount  | Type | Comment        | Detail          |
+-----------+---------+---------+------+----------------+-----------------+
| 13/9/2017 | 9:00AM  | $12.10  | Cash |                |                 |
+-----------+---------+---------+------+----------------+-----------------+
| 13/9/2017 | 9:30AM  | $22.45  | Card |                |                 |
+-----------+---------+---------+------+----------------+-----------------+
| 13/9/2017 | 9:30AM  | $22.45  | Card | Delivery       | deliver to rear |
+-----------+---------+---------+------+----------------+-----------------+
| 13/9/2017 | 9:30AM  | $22.45  | Card | ReturnCustomer | yes             |
+-----------+---------+---------+------+----------------+-----------------+
| 13/9/2017 | 9:45AM  | $-34.00 | Cash | Refund         |                 |
+-----------+---------+---------+------+----------------+-----------------+
| 13/9/2017 | 10:00AM | $17.67  | Card |                |                 |
+-----------+---------+---------+------+----------------+-----------------+
| 13/9/2017 | 10:00AM | $17.67  | Card | Deposit        | 10%             |
+-----------+---------+---------+------+----------------+-----------------+

我需要为每个特定事务返回一行,如果对事务做出任何评论/详细信息,则将它们转移到自己的列中(1表示注释,1表示详细信息)。对于发表了多条评论的交易,它们需要是他们自己的新专栏和详细信息。

所以上面的数据需要像这样返回:


+-----------+---------+---------+------+----------+-----------------+----------------+---------+
| Date      | Time    | Amount  | Type | Comment1 | Detail1         | Comment2       | Detail2 |
+-----------+---------+---------+------+----------+-----------------+----------------+---------+
| 13/9/2017 | 9:00AM  | $12.10  | Cash |          |                 |                |         |
+-----------+---------+---------+------+----------+-----------------+----------------+---------+
| 13/9/2017 | 9:30AM  | $22.45  | Card | Delivery | deliver to rear | ReturnCustomer | yes     |
+-----------+---------+---------+------+----------+-----------------+----------------+---------+
| 13/9/2017 | 9:45AM  | $-34.00 | Cash | Refund   |                 |                |         |
+-----------+---------+---------+------+----------+-----------------+----------------+---------+
| 13/9/2017 | 10:00AM | $17.67  | Card | Deposit  | 10%             |                |         |
+-----------+---------+---------+------+----------+-----------------+----------------+---------+

理论上,每笔交易的评论数量不受限制,但实际上它不会超过5条。

1 个答案:

答案 0 :(得分:2)

这是另一种方式(用于多列旋转)

// Starts an AVAudio Session
    NSError *error;
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
    [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

    // Starts a recognition process, in the block it logs the input or stops the audio
    // process if there's an error.
    recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
    AVAudioInputNode *inputNode = audioEngine.inputNode;
    recognitionRequest.shouldReportPartialResults = YES;
    recognitionTask = [speechRecognizer recognitionTaskWithRequest:recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
        BOOL isFinal = NO;
        if (result) {
            // Whatever you say in the microphone after pressing the button should be being logged
            // in the console.
            NSLog(@"RESULT:%@",result.bestTranscription.formattedString);
            self.inputToolbar.contentView.textView.text = result.bestTranscription.formattedString;
            self.inputToolbar.contentView.rightBarButtonItem.enabled = YES;
            isFinal = !result.isFinal;
        }
        if (error) {
            if (audioEngine != NULL) {
                [audioEngine stop];
                [inputNode removeTapOnBus:0];
                recognitionRequest = nil;
                recognitionTask = nil;
            }
        }
    }];

    // Sets the recording format
    AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0]; //[[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100 channels:1];
    [inputNode installTapOnBus:0 bufferSize:1024 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) {
        [recognitionRequest appendAudioPCMBuffer:buffer];
    }];

    // Starts the audio engine, i.e. it starts listening.
    [audioEngine prepare];
    [audioEngine startAndReturnError:&error];
    NSLog(@"Say Something, I'm listening");

如果你有未知的数据集

,它可以很容易地转换为动态查询