sql server - Select statement with a where clause and add 'comment' in another column -
i need add ‘comment’ in additional column select statement clause.
current code
first statement
select * [table] [dep] = 'm' , ([join m] <> [join b] or [resign m] <> [resign date beneficiary]) go
second statement
select * [table] [join b] < [resign b] , ([join m] > [join b] or [resign m] < [resign b]) go
this 2 different select statements, combine both outputs 1 table.
wrong code - (example explain)
first statement
select * [table] [dep] = 'm' , ([join m] <> [join b] or [resign m] <> [resign date beneficiary]) set [comment] = 'main' go
second statement
select * [table] [join b] < [resign b] , ([join m] > [join b] or [resign m] < [resign b]) set [comment] = 'date' go
the requirement add additional column output , add 'comment' in field, combine both outputs in 1 table.
output
first statement output
[number] |[dep] |[join m] |[join b] |[resign m ]|[resign b] 10003 |m |20160101 |20160201 |20160301 |20160301 10004 |m |20160101 |20160201 |20160301 |20160401
second statement output
[number] |[dep] |[join m] |[join b] |[resign m ]|[resign b] 10000 |m |20160201 |20160201 |20160131 |20160430 10002 |m |20160501 |20160430 |20160430 |20160430
required output
[number] |[dep] |[join m] |[join b] |[resign m ]|[resign b]|[comment] 10000 |m |20160201 |20160201 |20160131 |20160430 |'date' 10002 |m |20160501 |20160430 |20160430 |20160430 |'date' 10003 |m |20160101 |20160201 |20160301 |20160301 |'main' 10003 |m |20160101 |20160201 |20160301 |20160401 |'main'
use union all
combine multiple (compatible) result sets, , can add expressions in select
clause
select [number],[dep],[join m],[join b],[resign m ],[resign b],'main' comment [table] [dep] = 'm' , ([join m] <> [join b] or [resign m] <> [resign date beneficiary]) union select [number],[dep],[join m],[join b],[resign m ],[resign b],'date' comment [table] [join b] < [resign b] , ([join m] > [join b] or [resign m] < [resign b])
alternatively, if these rows represent entire contents of table
, want classify rows either main
or date
, use case
expression:
select [number],[dep],[join m],[join b],[resign m ],[resign b], case when [dep] = 'm' , ([join m] <> [join b] or [resign m] <> [resign date beneficiary]) 'main' else 'date' end comment [table]
Comments
Post a Comment