Solved

sql query that displays duplicates in an alias

Posted on 2014-04-23
6
135 Views
Last Modified: 2014-04-25
I have a database that records when computers performs a count of specific activities.  The same computer might report several times in a single day.  To simplify, we'll say the data is like this:

computer name, date, count
comp1, mon, 3
comp1, mon, 6
comp2, mon, 4
comp3, mon, 1
comp1, tue, 6
comp3, tue, 2
comp2, wed, 12

What I want is for the data to be displayed in a week grid format, like this:

computer name, mon, tue, wed, thu, fri
comp1, 9, 6, 0, 0, 0
comp2, 4, 0, 12, 0, 0
comp3, 1, 2, 0, 0, 0

I'm not quite sure how to approach this, if it's even possible.  Any help is appreciated!
0
Comment
Question by:bigbigpig
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 40019346
try this.
SELECT *
  FROM test
 PIVOT (SUM([Count]) FOR [Date] IN ([mon],[tue],[wed],[thu],[fri],[sat],[sun])) AS p

Open in new window

http://sqlfiddle.com/#!3/bbb3d/1
0
 
LVL 10

Author Comment

by:bigbigpig
ID: 40020510
That's pretty awesome thanks.  In my attempt to simplify I left out something which now seems important.  The table has an auto-increment primary key for record number.  So it's not adding the values as it should, probably because the record_number value is different.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 40020808
Can you post your table definition, sample data and expected result?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Author Comment

by:bigbigpig
ID: 40021724
Same as before, just with the record_number in front.

record_number,computer name, date, count
1,comp1, mon, 3
2,comp1, mon, 6
3,comp2, mon, 4
4,comp3, mon, 1
5,comp1, tue, 6
6,comp3, tue, 2
7,comp2, wed, 12

Expected results would be the same as your sqlfiddle query you linked to above.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40023070
SELECT
    [computer name],
    SUM(CASE WHEN date = 'Mon' THEN [count] ELSE 0 END) AS [Mon],
    SUM(CASE WHEN date = 'Tue' THEN [count] ELSE 0 END) AS [Tue],
    --...
    SUM(CASE WHEN date = 'Sat' THEN [count] ELSE 0 END) AS [Sat],
    SUM(CASE WHEN date = 'Sun' THEN [count] ELSE 0 END) AS [Sun]
GROUP BY
    [computer name]
ORDER BY
    [computer name]
0
 
LVL 10

Author Closing Comment

by:bigbigpig
ID: 40023537
Perfect - thanks!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now