Solved

sql query that displays duplicates in an alias

Posted on 2014-04-23
6
136 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now