Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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
Asp.net SQL SERVER合并两列,将其合并,并对其进行计数_Asp.net_Sql_Sql Server_Vb.net - Fatal编程技术网

Asp.net SQL SERVER合并两列,将其合并,并对其进行计数

Asp.net SQL SERVER合并两列,将其合并,并对其进行计数,asp.net,sql,sql-server,vb.net,Asp.net,Sql,Sql Server,Vb.net,我想通过合并和合并两列来创建条形码。 这是我的桌子: ID | Items1 | Items2 | BArcode 001 | Shirt | CPU | 002 | Shirt | CPU | 001 | Shoes | Monitor | 002 | Jacket | Monitor | 001 | Shoes | CPU |

我想通过合并和合并两列来创建条形码。 这是我的桌子:

 ID   |  Items1 |  Items2   |   BArcode
 001  |  Shirt  |  CPU      |      
 002  |  Shirt  | CPU       |         
 001  |  Shoes  | Monitor   |      
 002  |  Jacket |  Monitor  |      
 001  |  Shoes  |  CPU      |         
 002  |         | Keyboard  |      
 002  |         | Keyboard  |      
 001  |  Shirt  | Keyboard  |         
 002  |  Shirt  |           |      
基于ID+Item1/Items2+计数创建的条形码。计数是通过计算一个ID对同一项进行排序的次数来获得的。我希望它仅显示未生成条形码的数据,因此当条形码列值为空时,从上表中可以看出条形码为空:

 ID   |  Barcode       |  
 001  |  001Shirt1     |       
 002  |  002Shirt1     |          
 001  |  001Shoes1     |       
 002  |  002Jacket1    |        
 001  |  001Shoes2     |          
 001  |  001Shirt2     |          
 002  |  002Shirt2     |  
 001  |  001CPU1       |        
 002  |  002CPU1       |          
 001  |  001Monitor1   |       
 002  |  002Monitor1   |        
 001  |  001CPU2       |          
 002  |  002Keyboard1  |       
 002  |  002Keyboard2  |        
 001  |  001Keyboard1  |          
这是我的第一个代码:

                Dim strcommand As String = "select [ID], ([ID] + [Items1]) 
                as Barcode from tbl_Request where [Items1] != 'null' 
                and Barcode = 'null' union select ([ID] + [Items2]) 
                from tbl_Request where [Items2] != 'Null' and Barcode = 'null'"
它不起作用。它说“使用UNION、INTERSECT或EXCEPT运算符组合的所有查询在其目标列表中必须具有相同数量的表达式”。在我上面的代码中,我还没有实现计数。有人知道怎么做吗


感谢您的改进……

您得到的错误是,您创建的联合联接在第二个select语句中包含的字段与原始select语句中的字段不同,请注意,UNION中的每个SELECT语句必须具有相同的列数

因此,你需要改变

select ([ID] + [Items2])


您可以使用此查询生成
条形码
值:

;with cte as
(select id, item1 item, row_number() over (partition by id, item1 order by getdate()) rn
 from items
 where item1 is not null and barcode is null
 union
 select id, item2 item, row_number() over (partition by id, item2 order by getdate()) rn 
 from items
 where item2 is not null and barcode is null)

 select id, cast(id as varchar) + item + cast(rn as varchar) barcode
 from cte
如果要将其添加到一个新表中,例如带有列
id
barcode
tbl\u barcode
,可以执行以下操作:

;with cte as
(select id, item1 item, row_number() over (partition by id, item1 order by getdate()) rn
 from items
 where item1 is not null and barcode is null
 union
 select id, item2 item, row_number() over (partition by id, item2 order by getdate()) rn 
 from items
 where item2 is not null and barcode is null)

 insert into tbl_barcode (id, barcode)
 select id, cast(id as varchar) + item + cast(rn as varchar) barcode
 from cte

谢谢你的建议。我试过了,但没有显示任何价值。当我删除语法“and Barcode='null'”时。我不返回一个值,但不是全部。它已经显示了一个值,我将条形码='null'更改为条形码为null。。但它仍然返回一些记录。。。它不显示具有相同值的记录。谢谢。如果字段是空的,你就不能比较它。您应该使用的是NOTNULL。@1LASTBR3您是对的。我已经换了。非常感谢。亲爱的史瑞。帕特,谢谢你帮助我。我试过你的代码,它成功了。请问,如何将值插入现有列中。在条形码列中。非常感谢您的帮助。您将遇到一个问题,因为示例中有9行,但此查询输出了15行。你如何决定把哪个放进9?如果我改变条件把它放进另一个表中怎么样。例如,我在语法之前将insert添加到tbl_条形码中;对于cte…对不起,我没有先尝试就这么问了。我的导师现在不在,所以我无法在服务器上创建新表。我将在创建新表后尝试它。但是,我只是想问,有可能做到吗?谢谢,好的。。。。谢谢你帮助我。稍后我将尝试您的代码。我的导师还在外面,不知道他什么时候回来。无论如何,谢谢你的帮助。亲爱的史瑞。帕特,谢谢你帮助我。我试过你的代码,效果很好。非常感谢。亲爱的Mohan,我尝试了你的代码,但它只显示了几个数据。谢谢你的帮助,问题已经解决了。
;with cte as
(select id, item1 item, row_number() over (partition by id, item1 order by getdate()) rn
 from items
 where item1 is not null and barcode is null
 union
 select id, item2 item, row_number() over (partition by id, item2 order by getdate()) rn 
 from items
 where item2 is not null and barcode is null)

 insert into tbl_barcode (id, barcode)
 select id, cast(id as varchar) + item + cast(rn as varchar) barcode
 from cte
declare @x table (ID varchar(20),Items1 VARCHAR(10),Items2 VARCHAR(10),BARCODE INT)

INSERT INTO @x
(ID,Items1,Items2,BARCODE)
VALUES ('001','Shirt','CPU',NULL)
INSERT INTO @x
(ID,Items1,Items2,BARCODE)
VALUES ('001',NULL,'Monitor',NULL)
INSERT INTO @x
(ID,Items1,Items2,BARCODE)
VALUES ('002','TRouser','Monitor',NULL)

select ID,
Case when Items1 IS NOT NULL Then ID +Items1+
CAST(DENSE_RANK()OVER(PARTITION by Items1  order by Items1 desc)AS VARCHAR)
when Items2 IS NOT NULL Then ID +Items2+
CAST(DENSE_RANK()OVER(PARTITION by Items2  order by Items1 desc)AS VARCHAR)
 ELSE '' END AS Barcode from @x