sql query required grouping column in single row [duplicate] - sql

This question already has answers here:
need to return two sets of data with two different where clauses
(2 answers)
Closed 8 years ago.
I have following table structure
TicketID Status Duration
-----------------------------
1234 8 2
1233 8 10
1232 4 5
1231 8 12
1230 4 50
status 8 means Closed
status 4 means Open
It is required to have output in following way. Please do the need ful. If possible I wanted it in a single sql query.
Please help me to produce output in following way.
Row Closed (sum) Open(Sum)
---------------------------------
1 24 55

select 1 as row,
sum(case when status = 8 then 1 else 0 end) as closed,
sum(case when status = 4 then 1 else 0 end) as open
from your_table

select
1 as Row,
sum(case when Status=8 then 1 else 0 end ) as Closed_Sum,
sum(case when Status=4 then 1 else 0 end ) as Open_Sum
from
Mytable

Related

Add flag on SQL Server table, based on pair of features [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 8 months ago.
I have a table in SQL Server with Id, Code and Prod. I want to add a flag and end up like:
Id Code Prod Flag
0 101 A 0
1 101 B 0
2 101 A 1
3 101 A 1
4 101 B 1
5 160 B 0
6 160 A 0
7 160 B 1
8 190 A 0
9 190 B 0
For each pair Code-Prod (eg. 101-A, which has 3 cases), the flag should assume 0 for the first Id, 1 otherwise.
How do I do that?
This is ideally solved with row_number() window function, combined with an updatable CTE:
with upd as (
select *, Row_Number() over(partition by Concat(code,prod) order by id) rn
from t
)
update upd
set flag = case when rn = 1 then 0 else 1 end;

Fetching data from DB and populate a partitioned List

I am confused about this both from front end point of view as well as querying the data from SQLite Database. If you have any idea how to solve either of these please do answer.
SQLite Database
I have a table likes this:
transactionId | productId | quantity
1 2 1
2 4 0
3 1 null
4 3 1
5 9 1
6 6 0
7 1 1
8 7 1
9 8 1
10 2 1
11 0 null
12 3 1
13 5 1
14 7 1
15 1 0
16 2 1
17 9 1
18 0 null
19 2 1
Now I want to display this data in groups of 5 units(i.e. groups till 5 units are completed) in list in my flutter app.
So 1st group will have 8 items,
2nd will have 6 items,
and 3rd group will have 5 items
(and is still incomplete since more items can be added till quantity for that group becomes 5)
Something like this:
Now my App can have multiple groups like this. Also, I don't think Grid view builder can work here since for each group I'll have to display some data for the group as well as accumulated data (which isn't shown in the picture)
Questions:
1) How to query data from SQFLite database?
2) How to display the queried data in my Flutter App front end?
Unfortunately, this type of problem requires a recursive CTE (or other iterative processing).
Assuming that transactionId is consecutive with no gaps:
with recursive cte as (
select transactionId, productId,
coalesce(quantity, 0) as quantity,
1 as bin
from t
where transactionId = 1
union all
select t.transactionId, t.productId,
(case when cte.quantity > 5
then 0 else cte.quantity
end) + coalesce(t.quantity, 0) as quantity,
(case when cte.quantity > 5 then 1 else 0 end) + cte.bin as bin
from cte join
t
on t.transactionId = cte.transactionId + 1
)
select *
from cte;
If transactionId has gaps or other issues, just use row_number() (in another CTE) to create an appropriate column for the where clauses.

TSQL Sum with type [duplicate]

This question already has answers here:
SQL Server dynamic PIVOT query?
(9 answers)
Closed 5 years ago.
I'm writing using translate
Table1
id number type
1 6 111
1 5 111
1 6 113
2 3 112
2 6 111
i need to sum group by value of "type" and "id"
the result I want to see
RESULT Table
id type111 type112 typ113
1 11 0 6
2 6 3 0
Another way to use pivoting:
SELECT id,
ISNULL([111],0) as Type111,
ISNULL([112],0) as Type112,
ISNULL([113],0) as Type113
FROM (
SELECT id,
number,
[type]
FROM Table1
) AS SourceTable
PIVOT (SUM(number) FOR [type] IN ([111], [112], [113])
) AS PivotTable;
Output:
id Type111 Type112 Type113
----------- ----------- ----------- -----------
1 11 0 6
2 6 3 0
(2 rows affected)
In case when the variety of types is high - use dynamic SQL to build and execute query.
Try using SUM with CASE:
SELECT ID,
SUM(CASE WHEN type = 111 THEN number END) AS type111,
SUM(CASE WHEN type = 112 THEN number END) AS type112,
SUM(CASE WHEN type = 113 THEN number END) AS typE113,
FROM TABLE1
GROUP BY ID

SQL rows to column [duplicate]

This question already has answers here:
SQL Rows to Columns
(3 answers)
Closed 7 years ago.
This is my query.
select name, walk_to, count(*) count
from walk
where name='Rizwan'
group by name, walk_t0;
This results in data like:
NAME WALK_TO COUNT
Rizwan Cafe 2
Rizwan Arena 10
Rizwan Outside 20
There are other users' data as well. What I want is:
NAME CAFE ARENA OUTSIDE
Rizwan 2 10 20
John 3 21 90
James 5 5 9
Any idea how to write a query to get this result?
SELECT name,
SUM(CASE WHEN walk_to = 'Cafe' THEN 1 ELSE 0 END) Cafe,
SUM(CASE WHEN walk_to = 'Arena' THEN 1 ELSE 0 END) Arena,
SUM(CASE WHEN walk_to = 'Outside' THEN 1 ELSE 0 END) Outside
FROM walk
GROUP BY name
This is called pivioting for more examples look here SQL Server: Examples of PIVOTing String data
select name,
sum(case when walk_to='Cafe' then 1 else 0 end) as CAFE,
sum(case when walk_to='Arena' then 1 else 0 end) as ARENA,
sum(case when walk_to='Outside' then 1 else 0 end) as OUTSIDE
FROM walk
GROUP BY name

Counting Items/Rows in DB as Columns Grouped by Another Column

What I want to do is basically:
select Type, (Count(*) where Date='')as 10/1, (Count(*) where Date='')as 10/2
from my table
group by Type
What I want it to look like is:
Type 10/1 10/2
1 5 7
2 3 1
3 6 9
4 1 3
5 9 8
However, when I try to run a full select within each count column, I end up getting
Type 10/1 10/2
1 12 15
2 12 15
3 12 15
4 12 15
5 12 15
Any suggestions are appreciated. I'm not sure if I will need to run a pivot or not, but I wouldn't think so. Additionally after I can run that for any specific day, I was thinking about trying to put the date into a variable and trying to run the whole thing for a date range, generating columns dynamically for each day its run. I would probably create a new question for that though.
Try this;
SELECT TYPE
,SUM(CASE WHEN MyDate = '' THEN 1 ELSE 0 END) AS [10/1]
,SUM(CASE WHEN MyDate = '' THEN 1 ELSE 0 END) AS [10/2]
FROM MyTable
GROUP BY TYPE