SQL Server 2005 - получение ошибки по неизвестной причине - PullRequest
0 голосов
/ 17 августа 2010

Я пытаюсь создать таблицу с двумя полями, одно из которых представляет статус, а другое - тип оборудования.Затем мне нужен список того, как долго эти конкретные виды оборудования ремонтировались.У каждого оборудования может быть несколько магазинов, поэтому я бы хотел что-то вроде этого:

Equipment Type  |  Status | 0-7 days | 8-15 days | 16-30 days | ...
Type A          |    B    |     3    |     2     |     0      | ...
Type B          |    B    |     1    |     0     |     13     | ...

и т. Д.Вот то, что у меня есть код мудрый в попытке сделать это:

SELECT  EQP_STAT_EQP, EQP_TYP_EQP,
count(case [Days_Outstanding] when 'Less than 7 Days Outstanding     ' then 1 else null end) as [0_to_7_Days_Outstanding],
count(case [Days_Outstanding] when '8 to 14 Days Outstanding         ' then 1 else null end) as [8_to_14_Days_Outstanding],
count(case [Days_Outstanding] when '15 to 30 Days Outstanding        ' then 1 else null end) as [15_to_30_Days_Outstanding],
count(case [Days_Outstanding] when '31 to 60 Days Outstanding        ' then 1 else null end) as [31_to_60_Days_Outstanding],
count(case [Days_Outstanding] when '61 to 90 Days Outstanding        ' then 1 else null end) as [61_to_90_Days_Outstanding],
count(case [Days_Outstanding] when '91 to 120 Days Outstanding       ' then 1 else null end) as [91_to_120_Days_Outstanding],
count(case [Days_Outstanding] when 'Greater than 120 Days Outstanding' then 1 else null end) as [120_Plus_Days_Outstanding]
INTO Repair_Status_Summary
FROM Total_Equipment 
WHERE EQP_STAT_EQP='B'
GROUP BY EQP_STAT_EQP, EQP_TYP_EQP

Однако, это продолжает давать мне ошибку:

'Msg 245, Level 16, State 1, Line 143
Conversion failed when converting the varchar value ''Less than 7 Days Outstanding     '' to data type int.'

Я сделал этот тип таблицы раньше, точновот так, и даже скопировать и вставить это и изменить имена полей, думая, что это может решить проблему безрезультатно, единственное отличие - это предложение WHERE, которое я добавил.Я понимаю, почему он говорит, что это varchar, когда у меня есть Days_Outstanding как char (35).Так что я даже дошел до того, что удалил таблицу Total_Equipment и заново заполнил ее.Так что если у вас есть какие-либо идеи, это приветствуется.

РЕДАКТИРОВАТЬ

@ Марк Баннистер - Спасибо!Я чувствую себя так глупо сейчас, но я просто не видел этого.У меня должно было быть поле [Aging_Bucket], которое я использую [Days_Outstanding] для определения.Извините за потраченное время, я просто этого не видел.

1 Ответ

3 голосов
/ 17 августа 2010
then 1

должно быть

then '1'

1 - это целое число, а '1' - это varchar

SELECT  EQP_STAT_EQP, EQP_TYP_EQP,
count(case [Days_Outstanding] when 'Less than 7 Days Outstanding     ' then '1' else null end) as [0_to_7_Days_Outstanding],
count(case [Days_Outstanding] when '8 to 14 Days Outstanding         ' then '1' else null end) as [8_to_14_Days_Outstanding],
count(case [Days_Outstanding] when '15 to 30 Days Outstanding        ' then '1' else null end) as [15_to_30_Days_Outstanding],
count(case [Days_Outstanding] when '31 to 60 Days Outstanding        ' then '1' else null end) as [31_to_60_Days_Outstanding],
count(case [Days_Outstanding] when '61 to 90 Days Outstanding        ' then '1' else null end) as [61_to_90_Days_Outstanding],
count(case [Days_Outstanding] when '91 to 120 Days Outstanding       ' then '1' else null end) as [91_to_120_Days_Outstanding],
count(case [Days_Outstanding] when 'Greater than 120 Days Outstanding' then '1' else null end) as [120_Plus_Days_Outstanding]
INTO Repair_Status_Summary
FROM Total_Equipment 
WHERE EQP_STAT_EQP='B'
GROUP BY EQP_STAT_EQP, EQP_TYP_EQP
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...