0

At work I am having a production overview board which is showing the parts produced per hour since midnight. It is organized as a table for each line we are running. enter image description here As you can seen from the image it uses different colors to represent wether the goal has been achieved or not. Also another 3 colors are used to display planned events such as Planned Downtime, Planned Maintenance work, Planned Changeover. The screen is being populated from an SQL DB and to display the planned events in colors another SQL table is used. Its structure is like enter image description here
Depending on the event different integer is used to populate the columns from 00 to 23. E.g. if we have planned downtime the number will be 1, planned DT will be 2 and etc. and that will colour the specified cell in picture 1.

I would like to ask if there is way to get 2 dates e.g. start date & end date and populate the hour columns which are within the date/time period? In other words I could not find the SQL query that could let me do it.

John Smith
  • 173
  • 3
  • 13
  • You appear to have two questions here, the colouring question and how to split data into the hours. For the second question, what are you planning to do if a product is started at 05:55 and finished in 06:10? Is that going to appear in hour 5 or 6? – Rich Benner May 31 '16 at 09:24
  • Thanks for your answer Rich! The colouring is done in the html. The code there is checking what is the number of my IBS_PROD_DOWNTIME table. For example: Hour0 = 1 that will be Planned DT Hour1= 2 that will be Planned TPM Hour2= 3 that will be Planned Changeover Currently I have a Java based GUI where I have dropdown box for each hour and text field for the date. I would like to enter start and end dates that will populate the columns for the hours. For example time from 05:00 to 11:00 will be Planned TPM and fills in columns from 5 to 11 with number 2.Thanks! – Ivan Ivanov May 31 '16 at 10:40
  • You shouldn't design your data structures based on how the data is *presented*. If you have multiple pieces of data of the same "type", they all ought to be stored in the *same* column. You'd then have an additional column that stores the "hour number", making that part of the *data* rather than embedded in the column *names*. That would make ad-hoc querying far easier, and you can naturally join data based on hour numbers rather than having to manually compare each named column. Pivotting this data for display is a trivial exercise. – Damien_The_Unbeliever Jun 01 '16 at 08:29

1 Answers1

0

Here's a SELECT - you can wrap an INSERT or UPDATE etc around it to populate your table. I'm sure there are other (maybe better ways) of doing this too :)

DECLARE @START DATETIME = '04:15:00';
DECLARE @END DATETIME = '13:50:00';

DECLARE @VALUE INT = 2;


WITH Numbers(N) AS
(
SELECT 0 N
UNION ALL
SELECT N+1 N FROM Numbers
WHERE N<23
)
SELECT * FROM (
    SELECT CONCAT('Hour',N) Period, CASE WHEN N BETWEEN DATEPART(HOUR, @START) AND DATEPART(HOUR, @END) THEN @VALUE ELSE 0 END VALUE FROM Numbers
    ) SRC
PIVOT 
(
    MAX(VALUE) FOR Period IN ([Hour0],[Hour1],[Hour2],[Hour3],[Hour4],[Hour5],[Hour6],[Hour7],[Hour8],[Hour9],[Hour10],[Hour11],[Hour12],[Hour13],[Hour14],[Hour15],[Hour16],[Hour17],[Hour18],[Hour19],[Hour20],[Hour21],[Hour22],[Hour23])
) Pvt


Hour0       Hour1       Hour2       Hour3       Hour4       Hour5       Hour6       Hour7       Hour8       Hour9       Hour10      Hour11      Hour12      Hour13      Hour14      Hour15      Hour16      Hour17      Hour18      Hour19      Hour20      Hour21      Hour22      Hour23
----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
0           0           0           0           2           2           2           2           2           2           2           2           2           2           0           0           0           0           0           0           0           0           0           0

(1 row(s) affected)

EDIT: Hint on wrapping this with an insert (untested) See Combining INSERT INTO and WITH/CTE

WITH Numbers(N) AS
(
SELECT 0 N
UNION ALL
SELECT N+1 N FROM Numbers
WHERE N<23
)
INSERT INTO MyTable
(
  Hour0,
  Hour1,
   -- etc ...
)
SELECT * FROM (
    SELECT CONCAT('Hour',N) Period, CASE WHEN N BETWEEN DATEPART(HOUR, @START) AND DATEPART(HOUR, @END) THEN @VALUE ELSE 0 END VALUE FROM Numbers
) SRC
PIVOT 
(
    MAX(VALUE) FOR Period IN ([Hour0],[Hour1],[Hour2],[Hour3],[Hour4],[Hour5],[Hour6],[Hour7],[Hour8],[Hour9],[Hour10],[Hour11],[Hour12],[Hour13],[Hour14],[Hour15],[Hour16],[Hour17],[Hour18],[Hour19],[Hour20],[Hour21],[Hour22],[Hour23])
) Pvt

Regarding your comment on compatibility levels and PIVOT:

Is PIVOT supported with compatibility_level 80?

Community
  • 1
  • 1
Liesel
  • 2,929
  • 2
  • 12
  • 18
  • Thanks Les! I will try it and will let you know ASAP! :) – Ivan Ivanov May 31 '16 at 14:44
  • Hi Les, It seems that the compatability level of my SQL Server is not enough to use the PIVOT command. Is there another way to do the pivot bit? Also correct me if I am wrong but I need to change "Numbers" with the name of my table, right? Thanks in advance for your help! – Ivan Ivanov Jun 01 '16 at 07:18
  • What version SQL Server are you using and what's the compatibility set too? Afaik, PIVOT was introduced in 2005. You can fake PIVOT using CASE if you can't raise the level of the DB. BTW Numbers is just a CTE to generate 0-23. – Liesel Jun 01 '16 at 08:07
  • The compatibility level is 80 and I cannot raise it as it is not allowed by the IT team. It will be the first time I use SQL queries more complicated than the usual SELECT,UPDATE,INSERT INTO and I am struggling to understand where do I put the name of the Table I will be writing to or that is done after the PIVOT bit? I will also research on how to use the CASE statement instead of PIVOT. Thanks! – Ivan Ivanov Jun 01 '16 at 09:26
  • See the edit to my answer for a (rough) example of how to do the insert. For CASE instead of PIVOT, see the link I added for level 80. – Liesel Jun 01 '16 at 09:29
  • Thanks for your swift support Les! I will read through everything and will give it a go! I will let you know if it works! Thanks again! – Ivan Ivanov Jun 01 '16 at 09:41
  • Les, I just got it working on another database which is Server 2012. Thanks for your support! – Ivan Ivanov Jun 02 '16 at 07:07