Solved

sql query that displays duplicates in an alias

Posted on 2014-04-23
6
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 41

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 41

Expert Comment

by:Sharath
ID: 40020808
Can you post your table definition, sample data and expected result?
0
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 
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:
Scott Pletcher 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

691 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