Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在我的数据库中以字符串形式给出所有月份的数字,当用户选择任何月份时,下一行将自动填充_Sql_Sql Server_Database_Tsql - Fatal编程技术网

Sql 在我的数据库中以字符串形式给出所有月份的数字,当用户选择任何月份时,下一行将自动填充

Sql 在我的数据库中以字符串形式给出所有月份的数字,当用户选择任何月份时,下一行将自动填充,sql,sql-server,database,tsql,Sql,Sql Server,Database,Tsql,当用户在上一个字段中发送或插入数据时,我想让这个“月号”字段自动填充,而上一个字段是月的,作为字符串格式的月份,我想自动归档月号。 这是我的表,名为tbl_transactions,其中包含SQL Server中的列 Category (string) for_month (string) month_no (int) *<--new column, I want this to be autofilled when the user selects the data in the pre

当用户在上一个字段中发送或插入数据时,我想让这个“月号”字段自动填充,而上一个字段是月的,作为字符串格式的月份,我想自动归档月号。 这是我的表,名为
tbl_transactions
,其中包含SQL Server中的列

Category (string)
for_month (string)
month_no (int) *<--new column, I want this to be autofilled when the user selects the data in the previous row*
Vehicle_No (string)
amount (float)
插入html代码如下所示

<div class="forMonthOf">
 <label for="forMonthOf" class="label-forMonthOf">
   <p>For the Month</p>
   <input id="forMonthOf" name="forMonthOf" type="text" autocomplete="off"/>
 </label>
</div>

当月


为什么不在
选择中使用一个简单的
案例
语句?您可以在SSMS中运行以下操作

/* MOCK-UP SRUCTURE AND DATA */

DECLARE @tbl_transactions table (
    category varchar(50), for_month varchar(15), vehicle_no varchar(50), amount decimal(18,2)
);

INSERT INTO @tbl_transactions VALUES
    ( 'Registration', 'April', 123, 350 ),
    ( 'Registration', 'April', 456, 450 ),
    ( 'Registration', 'April', 739, 295 ),
    ( 'Maintenance', 'April', 123, 1500 ),
    ( 'Maintenance', 'April', 456, 1050 ),
    ( 'Maintenance', 'April', 789, 650 ),
    ( 'Fuel', 'April', 123, 2009 ),
    ( 'Fuel', 'April', 456, 1520 ),
    ( 'Fuel', 'April', 789, 1675 ),
    ( 'Registration', 'May', 123, 250 ),
    ( 'Registration', 'May', 456, 375 ),
    ( 'Registration', 'May', 789, 460 ),
    ( 'Maintenance', 'May', 123, 1200 ),
    ( 'Maintenance', 'May', 456, 950 ),
    ( 'Maintenance', 'May', 789, 750 ),
    ( 'Fuel', 'May', 123, 1225 ),
    ( 'Fuel', 'May', 456, 1000 ),
    ( 'Fuel', 'May', 789, 1650 ),
    ( 'Registration', 'June', 123, 325 ),
    ( 'Registration', 'June', 456, 428 ),
    ( 'Registration', 'June', 789, 165 ),
    ( 'Maintenance', 'June', 123, 1850 ),
    ( 'Maintenance', 'June', 456, 2509 ),
    ( 'Maintenance', 'June', 789, 1600 ),
    ( 'Fuel', 'June', 123, 1225 ),
    ( 'Fuel', 'June', 456, 1055 ),
    ( 'Fuel', 'June', 789, 275 );

/* Select data and determine the month number by month name */

SELECT
    category,
    for_month,
    CASE LOWER( LEFT ( for_month, 3 ) )
        WHEN 'jan' THEN 1
        WHEN 'feb' THEN 2
        WHEN 'mar' THEN 3
        WHEN 'apr' THEN 4
        WHEN 'may' THEN 5
        WHEN 'jun' THEN 6
        WHEN 'jul' THEN 7
        WHEN 'aug' THEN 8
        WHEN 'sep' THEN 9
        WHEN 'oct' THEN 10
        WHEN 'nov' THEN 11
        WHEN 'dec' THEN 12
        ELSE 0
    END AS month_no,
    vehicle_no,
    amount
FROM @tbl_transactions;
返回

+--------------+-----------+----------+------------+---------+
|   category   | for_month | month_no | vehicle_no | amount  |
+--------------+-----------+----------+------------+---------+
| Registration | April     |        4 |        123 | 350.00  |
| Registration | April     |        4 |        456 | 450.00  |
| Registration | April     |        4 |        739 | 295.00  |
| Maintenance  | April     |        4 |        123 | 1500.00 |
| Maintenance  | April     |        4 |        456 | 1050.00 |
| Maintenance  | April     |        4 |        789 | 650.00  |
| Fuel         | April     |        4 |        123 | 2009.00 |
| Fuel         | April     |        4 |        456 | 1520.00 |
| Fuel         | April     |        4 |        789 | 1675.00 |
| Registration | May       |        5 |        123 | 250.00  |
| Registration | May       |        5 |        456 | 375.00  |
| Registration | May       |        5 |        789 | 460.00  |
| Maintenance  | May       |        5 |        123 | 1200.00 |
| Maintenance  | May       |        5 |        456 | 950.00  |
| Maintenance  | May       |        5 |        789 | 750.00  |
| Fuel         | May       |        5 |        123 | 1225.00 |
| Fuel         | May       |        5 |        456 | 1000.00 |
| Fuel         | May       |        5 |        789 | 1650.00 |
| Registration | June      |        6 |        123 | 325.00  |
| Registration | June      |        6 |        456 | 428.00  |
| Registration | June      |        6 |        789 | 165.00  |
| Maintenance  | June      |        6 |        123 | 1850.00 |
| Maintenance  | June      |        6 |        456 | 2509.00 |
| Maintenance  | June      |        6 |        789 | 1600.00 |
| Fuel         | June      |        6 |        123 | 1225.00 |
| Fuel         | June      |        6 |        456 | 1055.00 |
| Fuel         | June      |        6 |        789 | 275.00  |
+--------------+-----------+----------+------------+---------+

如果需要,您也可以使用此功能进行
插入

您的屏幕未被截取。在此网站上,您可以尝试一下,并向我们展示您的尝试。此外,请将样本数据和预期结果显示为格式化文本,而不是图像。另外,我建议不要使用
float
来计算金额,您可以选择
int
decimal
。Float存储为近似值,在某些情况下(例如求和)会导致问题。如果存储月数,然后在查询时将其转换为字符串,不是更好吗?而不是储存两次?即使您不交换它们,也不应该存储它们,您应该在运行查询时计算它们。@DaleK,谢谢您的回复。注意你的建议。实际上,我已经尝试使用变量实现所需的结果,但无法实现相同的结果。自动填充意味着插入,所以请向我们显示您正在使用的插入。
+--------------+-----------+----------+------------+---------+
|   category   | for_month | month_no | vehicle_no | amount  |
+--------------+-----------+----------+------------+---------+
| Registration | April     |        4 |        123 | 350.00  |
| Registration | April     |        4 |        456 | 450.00  |
| Registration | April     |        4 |        739 | 295.00  |
| Maintenance  | April     |        4 |        123 | 1500.00 |
| Maintenance  | April     |        4 |        456 | 1050.00 |
| Maintenance  | April     |        4 |        789 | 650.00  |
| Fuel         | April     |        4 |        123 | 2009.00 |
| Fuel         | April     |        4 |        456 | 1520.00 |
| Fuel         | April     |        4 |        789 | 1675.00 |
| Registration | May       |        5 |        123 | 250.00  |
| Registration | May       |        5 |        456 | 375.00  |
| Registration | May       |        5 |        789 | 460.00  |
| Maintenance  | May       |        5 |        123 | 1200.00 |
| Maintenance  | May       |        5 |        456 | 950.00  |
| Maintenance  | May       |        5 |        789 | 750.00  |
| Fuel         | May       |        5 |        123 | 1225.00 |
| Fuel         | May       |        5 |        456 | 1000.00 |
| Fuel         | May       |        5 |        789 | 1650.00 |
| Registration | June      |        6 |        123 | 325.00  |
| Registration | June      |        6 |        456 | 428.00  |
| Registration | June      |        6 |        789 | 165.00  |
| Maintenance  | June      |        6 |        123 | 1850.00 |
| Maintenance  | June      |        6 |        456 | 2509.00 |
| Maintenance  | June      |        6 |        789 | 1600.00 |
| Fuel         | June      |        6 |        123 | 1225.00 |
| Fuel         | June      |        6 |        456 | 1055.00 |
| Fuel         | June      |        6 |        789 | 275.00  |
+--------------+-----------+----------+------------+---------+