SQLITE: Query to get data in a table format - sql

I have the data in this format:
Prod Name Qty
-------------------
Ink Joe 100
Pen Joe 10
Ink Jay 50
Pen Jay 5
I would like to write an SQL query which returns the data in this format:
Prod Joe Jay
---------------------
Ink 100 50
Pen 10 5
Please note that both Prod and Name, are dynamic(The number of Prod entries and Name entries keep varying)
Appreciate any help on this.

With conditional aggregation:
select prod,
max(case name when 'Joe' then Qty end) Joe,
max(case name when 'Jay' then Qty end) Jay
from tablename
group by prod
See the demo

Related

SQL query count rows with the same entry

Given a dataset Roster_table as such:
Group ID
Group Name
Name
Phone
42
Red Dragon
Jon
123455678
32
Green Lizard
Liz
932143211
19
Blue Falcon
Ben
134554678
42
Red Dragon
Reed
432143211
42
Red Dragon
Brad
231314155
19
Blue Falcon
Chad
214124412
How do I get the following query output combining rows with the same Group ID from the dataset, and the new column Count in descending order:
Group ID
Group Name
Count
42
Red Dragon
3
19
Blue Falcon
2
32
Green Lizard
1
SELECT * FROM Roster_table
Please try this where alias tot_count is used in ORDER BY clause.
-- PostgreSQL(v11)
SELECT Group_ID
, MAX(Group_Name) Group_Name
, COUNT(1) tot_count
FROM Roster_table
GROUP BY Group_ID
ORDER BY tot_count DESC;
Please check from url https://dbfiddle.uk/?rdbms=postgres_11&fiddle=b66f9f0d40e804e89be12e3530fe00a0
Based on Rahul Biswas's answer:
Solution without using Max function
SELECT Group_ID, Group_Name, COUNT(*)
FROM Roster_table
GROUP BY Group_ID, Group_Name
ORDER BY COUNT(*) DESC
Credit goes to Eric S.

How to query: "for which do these values apply"?

I'm trying to match and align data, or resaid, count occurrences and then list for which values those occurrences occur.
Or, in a question: "How many times does each ID value occur, and for what names?"
For example, with this input
Name ID
-------------
jim 123
jim 234
jim 345
john 123
john 345
jane 234
jane 345
jan 45678
I want the output to be:
count ID name name name
------------------------------------
3 345 jim john jane
2 123 jim john
2 234 jim jane
1 45678 jan
Or similarly, the input could be (noticing that the ID values are not aligned),
jim john jane jan
----------------------------
123 345 234 45678
234 123 345
345
but that seems to complicate things.
As close as I am to the desired results is in SQL, as
for ID, count(ID)
from table
group by (ID)
order by count desc
which outputs
ID count
------------
345 3
123 2
234 2
45678 1
I'll appreciate help.
You seem to want a pivot. In SQL, you have to specify the number of columns in advance (unless you construct the query as a string).
But the idea is:
select ID, count(*) as cnt,
max(case when seqnum = 1 then name end) as name_1,
max(case when seqnum = 2 then name end) as name_2,
max(case when seqnum = 3 then name end) as name_3
from (select t.*,
row_number() over (partition by id order by id) as seqnum -- arbitrary ordering
from table t
) t
group by ID
order by count desc;
If you have an unknown number of columns, you can aggregate the values into an array:
select ID, count(*) as cnt,
array_agg(name order by name) as names
from table t
group by ID
order by count desc
the query would look similar to this if that's what you're looking for.
SELECT
name,
id,
COUNT(id) as count
FROM
dataSet
WHERE
dataSet.name = 'input'
AND dataSet.id = 'input'
GROUP BY
name,
id

sum positive and negative values separately

I have a table:
NAME MONEY
Jane 100
Chris -100
Jane 50
Ann -10
Jane -25
Ann 17
And i want to write a query to sum data, in one column should be only positive amount od money in another column only negative. Output should look like this:
NAME SUM_POSITIVE SUM_NEGATIVE
Jane 150 -25
Chris 0 -100
Ann 17 -10
query:
select name, sum(money) from TABLE where money>0 group by name
union
select name, sum(money) from TABLE where money<0 group by name;
shows nearly what i want, but result has duplicate names and two columns instead of three:
NAME SUM
Ann -10
Ann 17
Jane -25
Jane 150
Chris -100
Please help me rewrite my query to correct output.
use case when
select name, sum(case when money>0 then money end) SUM_POSITIVE
,sum(case when money<0 then money end) SUM_NEGATIVE
from TABLE group by name
You are getting duplicate name becase union operator merge only those rows where all column values are same, as Ann contain -10 and 17 which is distinct so its make duplicate
You can do conditional aggregation instead :
select name,
sum(case when money > 0 then money end) as SUM_POSITIVE,
sum(case when money < 0 then money end) as SUM_NEGATIVE
from TABLE
group by name;

How to insert multiple columns value under a single column and separate the column name?

In the database there is a table
Name | id | Yearly_Profit | Yearly_Loss | Monthly_Profit | Monthly_loss
Alex 1 10 20 30 40
Ben 2 100 200 300 400
The output table will be like this
Name | id | Profit | Loss | Type
Alex 1 10 20 Yearly
Ben 2 100 200 Yearly
Alex 1 30 40 Monthly
Ben 2 300 400 Monthly
How can I do this?
Is this something like pivot or other?
You could use unpivot, but union all would be the simplest solution for you.
select Name, id, Yearly_Profit as Profit, Yearly_Loss as Loss, 'Yearly' as Type
from your_table
union all
select Name, id, Monthly_Profit , Monthly_loss, 'Monthly'
from your_table
This query would work for you, in this specific scenario.
SELECT
Name,
ID,
Yearly_Profit AS 'Profit',
Yearly_Loss AS 'Loss',
'Yearly' AS 'Type'
FROM Table
UNION ALL
SELECT
Name,
ID,
Monthly_Profit AS 'Profit',
Monthly_Loss AS 'Loss',
'Monthly' AS 'Type'
FROM Table
ORDER BY 5
But, if you'd have multiple columns in your table then you'd probably have to use a UNPIVOT.

SQL Sum count based on an identifier

Assume I have a table with the following data:
Name TransID Cost
---------------------------------------
Susan 1 10
Johnny 2 10
Johnny 3 9
Dave 4 10
I want to find a way to sum the Costs per name (assume the Names are unique) so that I get a table like this:
Name Cost
---------------------------------------
Susan 10
Johnny 19
Dave 10
Any help is appreciated.
This is relatively straightforward: you need to use a GROUP BY clause in your query:
SELECT Name,SUM(Cost)
FROM MyTable
GROUP BY Name