I would like to get all the groups that have a distinct price of 0 from my table, IE only if all prices are 0 in that group should it be returned.
My query & table look something like this.
tab:([]grp:`a`b`c`c`a`a`a;price:0 20 0 1 0 0 0)
select grp from tab where distinct price = 0
The output should only be `a since `a is the only group where all prices are 0.
Using an fby is one way to achieve the result here.
q)tab:([]grp:`a`b`c`c`a`a`a;price:0 20 0 1 0 0 0)
q)select from tab where 0=(max;abs price)fby grp
grp price
---------
a 0
a 0
a 0
a 0
q)distinct exec grp from tab where 0=(max;abs price)fby grp
,`a
Another approach:
q)where exec all 0=price by grp from tab
,`a
Related
I have a table with a shipment_id, no_of_boxes, and no_of_pallets as shown below.
shipment_id
no_of_boxes
no_of_pallets
1
23
0
1
45
0
1
0
1
2
3
0
2
165
0
2
0
10
I want to sum the no_of_boxes, and no_of_pallets columns against their respective shipment_id. The columns no_of_boxes, and no_of_pallets are COUNT derived columns (calculated from a different table with JOINS).
I tried writing a subquery for this but didn't help. Below subquery is for no_of_boxes, a similar query was written for no_of_pallets.
SELECT SUM(no_of_boxes)
FROM (SELECT COUNT(si.shipment_item_id) AS no_of_boxes
FROM shipment_item AS si
JOIN shipment_item AS si
ON si.shipment_order_systemid = sho.system_id
JOIN shipping_unit AS su
ON su.system_id = si.shipping_unit_systemid
WHERE su.unit LIKE 'BOX'
GROUP BY si.shipment_item_id,
su.unit) t
My desired result is:
shipment_id
no_of_boxes
no_of_pallets
1
68
1
2
168
10
To get the result you want, use the following query:
SELECT shipment_id, sum(no_of_boxes), sum(no_of_pallets)
FROM shipments
GROUP BY shipment_id;
I have a table such as the following
SP MA SL NG
jame j001 1 20200715 |
jame j001 -1 20200715 | -> count is 0
pink p002 3 20200730 }
pink p002 -3 20200730 } => count is 0
jack j002 12 20200731 | => count is 1
jack j002 -2 20200731 |
jack j002 12 20200801 } => count is 1
I want to count record and I want a result like:
SP count
jame 0
pink 0
jack 2
I could do with some help, please. Thanks you!
How the result is to be reached:
If SP, MA ,NG is the same then sum to SL.
Sum is 0 then count is 0,SUM is not 0 then count is 1.
If NG, SP is not the same then count is 1.
As i understood your requirements
If SP, MA, NG is the same then sum to SL.
List item Sum is 0 then count is 0 SUM is not 0 then count is 1.
If NG, SP is not the same then count is 1.
Try below Query:
with cte as (
select sp,ma,ng,sum(sl) from example group by sp,ma,ng having sum(sl)>0
),
cte1 as (
select distinct sp from example
)
select
t1.sp,
sum(case when sum>0 then 1 else 0 end)
from cte1 t1 left join cte t2 on t1.sp=t2.sp
group by t1.sp
Demo on Fiddle
I have a table
t:flip `dt`id`data ! (`d1`d1`d2`d2`d3`d3; 0 1 0 1 0 1; 100 200 100 300 0 200)
and from some other query, I have a table
s:flip `dt`id ! (`d1`d2`d2`d3; 0 0 1 1)
How can I select from t such that it returns all entries where the combination of dt and id are in s, so return
flip `dt`id`data ! (`d1`d2`d2`d3; 0 0 1 1; 100 100 300 200)
You can use in on table to table ops so just create a table from your required columns in t and use in to search s for the corresponding records. As long as the table columns and types from the left argument and right argument are the same, then in will produce a boolean list as expected.
q)select from t where ([]dt;id) in s
dt id data
----------
d1 0 100
d2 0 100
d2 1 300
d3 1 200
We are trying to output for each customer column should have 12 budgeted row entries for every month.
Scenario 1:
ie. Turn table data :
Name BudMnt1|BudMnt2|BudMnt3
cust1 0 0 0
cust2 0 0 0
cust3 2418 0 0
cust4 0 416 198
into this :
Name cust1| cust2| cust3| cust4
BudMnt1 0 0 24180 0
BudMnt2 0 0 0 416
BudMnt3 0 0 0 198
Scenario 2:
Includes Scenario 1 column as Budget+ additional column is sales solumn here
so, it becomes two column budget ,sales which needs to unpivot on single query structure.
ie. Turn table data :
Name JanSales|FebSales|MarSales
cust1 0 0 0
cust2 0 0 0
cust3 0 0 3
cust4 2 0 0
into this :
Name cust1|cust2|cust3|cust4
JanSales 0 0 0 2
Feb Sales 0 0 0 0
Mar Sales 0 0 3 0
Any HELP would be much appreciated !
This link may help you as a reference to your question:
https://social.msdn.microsoft.com/Forums/office/en-US/04346f7c-0923-432d-83c3-22bf759dea22/transpose-data-from-columns-into-rows-using-sql
This code works fine. Tasted it on sql server 2012.
SELECT NAME ,
budmtn ,
cust
INTO #temptable
FROM ( SELECT *
FROM tbl1
) AS result UNPIVOT ( cust FOR budmtn IN ( budmtn1, budmtn2, budmtn3 ) ) AS unpivotedtable
SELECT budmtn ,
cust1 ,
cust2 ,
cust3 ,
cust4
FROM #temptable PIVOT( SUM(cust) FOR NAME IN ( cust1, cust2, cust3, cust4 ) ) AS result
I have the following situation
ID Value
1 50
1 60
2 70
2 80
1 0
2 50
I need to run a query that would return summed value, grouped by ID. The catch is if the value is 0, then the entire sum should be 0.
Query results would be
ID Value
1 0
2 200
I tried
select ID, case
when Value> 0 then sum(Value) * 1
when Value= 0 then sum(value) * 0
end
from table
but that did not work.
select ID,
sum(value)*sign(min(abs(value))) as [sum(value)]
from YourTable
group by ID
With a case if you like:
select ID,
case sign(min(abs(value)))
when 0 then 0
else sum(value)
end as [sum(value)]
from YourTable
group by ID