i WANT TO LIST A COUNT OF 'BETWEEN' VALUES. I CAN ONLY DO ONE RANGE AT THE MOMENT.
SELECT COUNT(WLU.BARCODE) "NO_BARCODES BETWEEN 3001-3002"
FROM WMSLOADUNIT WLU
WHERE WLU.BARCODE BETWEEN '3001000000' AND '3002000000';
I WAN TO LIST COUNT FOR BETWEEN '3002000000' AND '3003000000', BETWEEN '3003000000' AND '3004000000' AND SO ON. CAN YOU HELP??
I used CASE statement and used SUM to count the number of instances within each statement:
select
sum(case when wlu.barcode between '3001000000' and '3002000000' then 1 else 0 end)"BARCODES 3001-3002"
,sum(case when wlu.barcode between '3002000000' and '3003000000' then 1 else 0 end)"BARCODES 3002-3003"
,sum(case when wlu.barcode between '3003000000' and '3004000000' then 1 else 0 end)"BARCODES 3003-3004"
,sum(case when wlu.barcode between '3004000000' and '3005000000' then 1 else 0 end)"BARCODES 3004-3005"
,sum(case when wlu.barcode between '3005000000' and '3006000000' then 1 else 0 end)"BARCODES 3005-3006"
,sum(case when wlu.barcode between '3006000000' and '3007000000' then 1 else 0 end)"BARCODES 3006-3007"
,sum(case when wlu.barcode between '3007000000' and '3008000000' then 1 else 0 end)"BARCODES 3007-3008"
from wmsloadunit wlu
;
RESULTS
Related
I need to expand the indicator (currently on daily basis) to a larger group (groups multiple consecutive days into one grp). I have following type of data:
id date grp new_ind traditional_ind
--------------------------------------------------
1 02-01-2021 1 1 0
1 02-02-2021 1 0 1
1 02-03-2021 1 0 0
1 02-04-2021 1 null null
1 02-06-2021 2 0 1
1 02-07-2021 2 0 0
2 02-01-2021 1 null null
where new_ind and traditional_ind are mutually exclusive. With this, I am trying to create new indicator that expands the indicators that are currently on daily level to grp level, that will look like:
id date grp new_ind traditional_ind final_type
----------------------------------------------------------------
1 02-01-2021 1 1 0 new
1 02-02-2021 1 0 1 new
1 02-03-2021 1 0 0 new
1 02-04-2021 1 null null new
1 02-06-2021 2 0 1 traditional
1 02-07-2021 2 0 0 traditional
2 02-01-2021 1 null null none
basically,
if new_ind was ever 1, I want to flag entire grp as 'new'
if new_ind=0 and if traditional_ind is ever 1, flag entire grp as 'traditional'
if both indicators were null, then flag entire grp as 'none'
so that each id and grp can have single value of final_type.
I've tried:
max(case when new_ind = 1 then 'New'
when traditional_ind = 1 then 'Traditional'
else 'None' end) over (partition by id, grp) as final_type
but this wouldn't recognize when new_ind=1 then 'New' and flag all of new_ind = 1 as 'None' (but show traditional correctly):
id date grp new_ind traditional_ind final_type
----------------------------------------------------------------
1 02-01-2021 1 1 0 none
1 02-02-2021 1 0 1 none
1 02-03-2021 1 0 0 none
1 02-04-2021 1 null null none
1 02-06-2021 2 0 1 traditional
1 02-07-2021 2 0 0 traditional
2 02-01-2021 1 null null none
But if I remove else statement and only run:
max(case when new_ind = 1 then 'New'
when traditional_ind = 1 then 'Traditional'
end) over (partition by id, grp) as final_type
then this does accurately expand indicator as I hope, just returns null values (which I need to show as 'None' instead of nulls):
id date grp new_ind traditional_ind final_type
----------------------------------------------------------------
1 02-01-2021 1 1 0 new
1 02-02-2021 1 0 1 new
1 02-03-2021 1 0 0 new
1 02-04-2021 1 null null new
1 02-06-2021 2 0 1 traditional
1 02-07-2021 2 0 0 traditional
2 02-01-2021 1 null null null
Can anyone help identify issue with my max case when statement?
I think something like this should work:
WITH final_types AS (
SELECT
id,
grp,
( case
when bool_or(new_ind = 1) then 'New'
when bool_or(traditional_ind = 1) then 'Traditional'
else 'None'
end
) AS final_type
FROM your_table
GROUP BY id, grp
)
SELECT
t1.*,
t2.final_type
FROM your_table t1
JOIN final_types t2 ON t1.id = t2.id AND t1.grp = t2.grp
My query is:
select urunbolgesi,sube,
count(case etiket when 'BOŞTA' then 1 else null end) as bosta,
count(case etiket when 'ZİMMET' then 1 else null end) as zimmet,
count(case etiket when 'ISKART' then 1 else null end) as iskart
from tblurun
where urunadi='CASPER-11'
group by urunbolgesi,sube,etiket,urunadi
My result is:
urunbolgesi sube bosta zimmet iskart
A qwer 0 1 0
B asdf 5 0 0
B asdf 0 0 1
B asdf 0 1 0
What I want that is:
urunbolgesi sube bosta zimmet iskart
A qwer 0 1 0
B asdf 5 1 1
Can I combine with code above result?
I'm trying to grab information to send to Google Charts for a graph I'm looking at the column Active which can be true/false but is set to varchar in my database.
Using this query:
SELECT
SUM(CASE WHEN CONVERT(int, Active) Active = 1 THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN CONVERT(int, Active) Active = 0 THEN 1 ELSE 0 END) AS Inactive
FROM
Events
throws this error:
Conversion failed when converting the varchar value 'True' to data type int.`
Ints are not booleans.... they are called bit in SQL Server. try;
SELECT
SUM(CASE WHEN CONVERT(bit, Active) = 1 THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN CONVERT(bit, Active) = 0 THEN 1 ELSE 0 END) AS Inactive
FROM Events
Or just test for your String
SELECT
SUM(CASE WHEN Active = 'True' THEN 1 ELSE 0 END) AS Active,
SUM(CASE WHEN Active = 'False' THEN 1 ELSE 0 END) AS Inactive
FROM Events
P.S. Storing Boolean values as String columns is silly... Consider changing the column to be a bit column so you don't even need to convert it
i have a table like this:
date_added owner action
01-02-2016 1 note
04-02-2016 1 call
04-02-2016 1 call
05-02-2016 1 note
05-02-2016 1 meeting
06-02-2016 1 meeting
06-02-2016 1 note
06-02-2016 1 cal
06-02-2016 1 note
10-02-2016 1 call
10-02-2016 1 note
10-02-2016 1 meeting
I need a view like this:
date_added owner note call meeting
01-02-2016 1 1 0 0
04-02-2016 1 0 2 0
05-02-2016 1 0 0 1
06-02-2016 1 2 1 1
10-02-2016 1 1 1 1
How do i create a column with something like
WHERE action LIKE 'note'
?
You could use CASE expression.
Query
select date_added, owner,
sum(case action when 'note' then 1 else 0 end) note,
sum(case action when 'call' then 1 else 0 end) call,
sum(case action when 'meeting' then 1 else 0 end) meeting
from your_table_name
group by date_added, owner;
Find demo here
I have a table with a list of values. -1 is a blank value:
ID FieldType1A FieldType1B FieldType2A FieldType2B Person
1 15 14 10 -1 1
2 16 -1 12 10 1
3 17 -1 5 6 1
4 6 -1 7 -1 2
...
So the result should be:
Person FieldType1 FieldType2
1 4 5
2 1 1
there is a users table with a list of user IDs, would there be a way of iterating over that list of values to generate the person list in the result set (0 for the field types being perfectly valid as it is merely counts)? I think the answer to T-SQL Column Values Count is a step in the direction I'm attempting to go, but unsure how to combine columns that are the same (the A/Bs allow for a list of answers). That and I'm interested in combining all valid values as not attempting to count the number of each valid response.
You can use a CASE expression to change all non-negative-one values to 1, and -1 values to 0, and then sum them up.
SELECT Person,
SUM(CASE WHEN FieldType1A <> -1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN FieldType1B <> -1 THEN 1 ELSE 0 END) AS FieldType1,
SUM(CASE WHEN FieldType2A <> -1 THEN 1 ELSE 0 END) +
SUM(CASE WHEN FieldType2B <> -1 THEN 1 ELSE 0 END) AS FieldType2
FROM YourTable
GROUP BY Person
SELECT Person,
count(nullif(FieldType1A, -1)) + count(nullif(FieldType1B, -1)) as FieldType1,
count(nullif(FieldType2A, -1)) + count(nullif(FieldType2B, -1)) as FieldType2
FROM yourtable
GROUP BY person