Microsoft SQL Server使用PiVOT行转换为列转置

时间:2017-05-24 13:12:47

标签: sql-server tsql pivot dynamic-sql

我正在使用MS-SQLServer-2016,并要求将行转置为列。 我正在使用Pivot和动态SQL选项来执行此操作,因为行数是动态的。 图1是我目前得到的输出。但是,客户端不希望显示这些NULL。他只希望显示Not-Null日期。 有没有办法摆脱那些空值并只显示不同的日期?

Figure1-Transpose_Output

关心,

坦哈

DECLARE @columns AS NVARCHAR(MAX), @sql AS NVARCHAR(MAX), @var1 AS 
VARCHAR(MAX); 

SELECT @columns = stuff((select DISTINCT ',' + quotename(replace(replace(replace(n.action_note,' ','<>'),'><',''),'<>',' ') )
                         from engagement_action n, action_party m, personal p
                         where n.action_id = m.action_id
                           and p.party_id = m.party_id
                           and n.action_note like 'XXX'
                           and m.system_name = 'XXXXXXXXXXXX'
                           and p.customer_number = 'XXXXXXX' FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)'),1,1,'')

SELECT @sql =

              'select "RANK_IN_PROP" ,
               customer_number, 
               customer_shortname, 
               system_name,
               '+ @columns + '
               from
                   (SELECT RANK() OVER (PARTITION BY convert(date,m.created_when) ORDER BY convert(date,m.created_when))"RANK_IN_PROP" ,
                          p.customer_number,
                          p.customer_shortname, 
                          m.system_name,
                          m.created_when ,
                          convert(date,m.created_when) as created_when, 
                          replace(replace(replace(n.action_note,'' '',''<>''),''><'',''''),''<>'','' '') as action_note1 
                   FROM engagement_action n, action_party m, personal p
                  WHERE n.action_id = m.action_id
                    AND p.party_id = m.party_id
                    AND n.action_note like ''%XXX%'' 
                    AND m.system_name = ''XXXXXXXXXXX''
                    AND p.customer_number = ''XXXXXXXX'' ) d
                  PIVOT 
                     (max(created_when) for action_note1 in ( ' + @columns + ' ))p order by created_when desc'

execute(@sql);`

1 个答案:

答案 0 :(得分:0)

你可以尝试这个,只是在where子句中添加(选择不同的action_note engagement_action,其中action_note不为null)。因为我无法创建测试数据,因此无法真正执行查询

override func awakeFromNib() {
    super.awakeFromNib()
    for i in 0 ... 6 {
        items.append(i)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    carousel.type = .custom
}

func numberOfItems(in carousel: iCarousel) -> Int {
    return items.count
}


func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
    var label: UILabel
    var itemView: UIImageView

    if let view = view as? UIImageView {
        itemView = view
        label = itemView.viewWithTag(1) as! UILabel
    } else {

        itemView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

        itemView.image = UIImage(named: "page.png")
        itemView.layer.cornerRadius = 5

        itemView.contentMode = .center

        label = UILabel(frame: itemView.bounds)
        label.backgroundColor = .clear
        label.textAlignment = .center
        label.font = label.font.withSize(50)
        label.tag = 1
        itemView.addSubview(label)
    }

    label.text = "\(items[index])"

    return itemView
}

func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {

    switch option {
        case .visibleItems:
        let elemento:Int = self.carousel.currentItemIndex as Int
        print(elemento)
        if elemento == 0 {
            return 2;
        }else if elemento == items.count-1 {
            return 2;

        }else {
            return 3;
        }

    default:
        return value
    }
}

func carousel(_ carousel: iCarousel, itemTransformForOffset offset: CGFloat, baseTransform transform: CATransform3D) -> CATransform3D {


    let offsetFactor:CGFloat = self.carousel(carousel, valueFor: .spacing, withDefault: 1.25) * carousel.itemWidth

    let  zFactor: CGFloat = 400.0
    let  normalFactor: CGFloat = 0
    let  shrinkFactor: CGFloat = 100.0

    let  f: CGFloat =  sqrt(offset * offset + 1)-1

    var trans = transform

    trans = CATransform3DTranslate(trans, offset * offsetFactor, f * normalFactor, f * (-zFactor))
    trans = CATransform3DScale(trans, 1 / (f / shrinkFactor + 1.0), 1 / (f / shrinkFactor + 1.0), 1.0 )


    return trans
}

func carousel(_ carousel: iCarousel, shouldSelectItemAt index: Int) -> Bool {
    return true
}