Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating multiple columns in a view

Status
Not open for further replies.
Feb 6, 2012
11
US
SQL 2012 Sample data:

Table labeled Data-

User Item Date
Ann shoe 01022015
Beth coat 02182015
Carl pants 01242015
Dave coat 03072015

I would like to create a view that would show the number of items sold per month in the following layout

View labeled Results-

Jan Feb Mar
2 1 1

This is what I have right now

create view [dbo].[View]
as
Select Count(*) AS Jan from
OPENQUERY(Table1,'Select * From DATA where DATE >= 01012015 and DATE <= 01312015')
UNION ALL
Select Count(*) AS Feb from
OPENQUERY(Table1,'Select * From DATA where DATE >= 02012015 and DATE <= 02282015')
UNION ALL
Select Count(*) AS Mar from
OPENQUERY(Table1,'Select * From DATA where DATE >= 03012015 and DATE <= 03312015')

which yields these results-

Jan
2
1
1

Is there a way to do what I'm trying to do?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top