我有一个具有以下结构的表:
#include <stdio.h>
#include <stdlib.h>
typedef struct celltag{
char name[11];
double time;
struct celltag *next;
} celltype;
int compare (const void *ap, const void *bp)
{
celltype *a = *((celltype * const *)ap);
celltype *b = *((celltype * const *)bp);
return (a->time > b->time) - (a->time < b->time);
}
int main (void) {
celltype c1 = { .name = "three", .time = 127.21 },
c2 = { .name = "one", .time = 127.1 },
c3 = { .name = "two", .time = 127.19 },
*pc[] = { &c1, &c2, &c3 };
size_t n = sizeof pc / sizeof *pc;
qsort (pc, n, sizeof *pc, compare);
for (size_t i = 0; i < n; i++)
printf ("%-5s %6.2f\n", pc[i]->name, pc[i]->time);
}
我需要将这些值转换为以下格式:
$ ./bin/qsort_ptp_struct
one 127.10
two 127.19
three 127.21
答案 0 :(得分:1)
您无需使用 PIVOT ,您可以使用一个简单的查询即可。
SELECT t1.refno,
t1.code AS Code1,
t2.code AS Code2,
t1.trantype,
t1.amount
FROM @table t1
INNER JOIN @table t2
ON t1.refno = t2.refno
AND T1.code < T2.code
答案 1 :(得分:0)
您可以尝试以下查询。
;WITH Tab (RefNo,Code,TranType,Remarks,Amount,rowno)
AS
(SELECT T.RefNo
, T.Code
,TranType
,Remarks
,Amount
, RN = ROW_NUMBER() OVER (PARTITION BY T.RefNo ORDER BY T.Code )
FROM Table1 T)
SELECT RefNo,Code1 = MAX( CASE WHEN N.rowno=1 THEN N.Code ELSE 0 END ),
Code2 = MAX( CASE WHEN N.rowno=2 THEN N.Code ELSE 0 END ) ,
TranType,Remarks,Amount FROM Tab n
GROUP BY N.RefNo,TranType,Remarks,Amount