Solved

sql query that displays duplicates in an alias

Posted on 2014-04-23
6
139 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

732 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