如何在SQL SERVER中为此场景进行此查询?

时间:2016-11-23 06:16:19

标签: sql sql-server

我有两个名为<!DOCTYPE html> <html> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>jQuery UI Datepicker - Default functionality</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> $( function() { $( "#datepicker" ).datepicker({ dateFormat: 'yy/mm/dd' }); } ); </script> <script language="JavaScript"> TargetDate ="2016/11/25"; CountActive = true; CountStepper = -1; LeadingZero = true; DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds."; FinishMessage = "Expired!"; </script> <body> <h1>My First JavaScript</h1> <p>Date: <input type="text" id="datepicker"></p> <script language="JavaScript" src="//scripts.hashemian.com/js/countdown.js"></script> </body> </html>的表,下面是我的查询

TransactionItem, ItemRelation

计算

我查询两个表的结果 Result of my query of two tables 正如您可以在图像上看到的结果,现在我只想将第一个结果的数量值除以第二个结果的大小。例如

     select sum(dqty) as QUANTITY FROM [HQMatajer].[dbo].[TransactionItem] 
    where ItemLookupCode='306519P0001' group by Tyear, Tmonth

 select 
    ItemLookupCode, convert(INT,SUBSTRING(ItemLookupCode,8,4)) as SIZE from 
    [HQMatajer].[dbo].[ItemRelation] where ItemLookupCode='319348P0001' or 
    ChildItemLookupCode1='319348P0001' or ChildItemLookupCode2='319348P0001' or 
    ChildItemLookupCode='319348P0001'  

如何在单个查询中执行此操作?

4 个答案:

答案 0 :(得分:0)

您可以计算尺寸并将其放入变量中。然后,您可以在第二个查询中使用相同的变量进行除法。

declare @size int;

set @size= (Select top 1 convert(INT,SUBSTRING(ItemLookupCode,8,4)) as SIZE from [HQMatajer].[dbo].[ItemRelation] 
where ItemLookupCode='319348P0001' or ChildItemLookupCode1='319348P0001' or ChildItemLookupCode2='319348P0001' or ChildItemLookupCode='319348P0001')

select sum(dqty)/@size as QUANTITY FROM [HQMatajer].[dbo].[TransactionItem] 
where ItemLookupCode='306519P0001' 
group by Tyear, Tmonth 

答案 1 :(得分:0)

    CREATE TABLE #Tbl1(Qty INT)
CREATE TABLE #Tbl2(Size INT)

INSERT INTO #Tbl1(Qty)
SELECT 6  UNION ALL
SELECT 64 UNION ALL
SELECT 167

INSERT INTO #Tbl2(Size)
SELECT 48

SELECT Qty / (SELECT Size FROM #Tbl2)
FROM #Tbl1

答案 2 :(得分:0)

这个怎么样:

SELECT QUANTITY / SIZE QUANTITY FROM (
    select sum(dqty) as QUANTITY
    FROM [HQMatajer].[dbo].[TransactionItem]
    where ItemLookupCode='306519P0001'
    group by Tyear, Tmonth
) A
CROSS JOIN (
    select ItemLookupCode, convert(INT,SUBSTRING(ItemLookupCode,8,4)) as SIZE
    from [HQMatajer].[dbo].[ItemRelation]
    where
        ItemLookupCode='319348P0001'
        or ChildItemLookupCode1='319348P0001'
        or ChildItemLookupCode2='319348P0001'
        or ChildItemLookupCode='319348P0001'
) B

答案 3 :(得分:0)

使用变量存储总和。那么我们可以在分割下一个查询时使用这个变量。

Declare @QUANTITY float;

select @QUANTITY=sum(dqty) 
FROM [HQMatajer].[dbo].[TransactionItem] 
where ItemLookupCode='306519P0001'
group by Tyear, Tmonth 


select ItemLookupCode
,convert(INT,SUBSTRING(ItemLookupCode,8,4)) as SIZE, convert(INT,SUBSTRING(ItemLookupCode,8,4))/@QUANTITY
from [HQMatajer].[dbo].[ItemRelation] 
where (ItemLookupCode='319348P0001' or ChildItemLookupCode1='319348P0001' or ChildItemLookupCode2='319348P0001' or ChildItemLookupCode='319348P0001')