I have the following problem:
I have a table that looks something like this:
ArticleID|Group|Price
1|a|10
2|b|2
3|a|3
4|b|5
5|c|5
6|f|7
7|c|8
8|x|3
Now im trying to get a result like this:
PriceA|PriceRest
13|30
Meaning I want to sum all prices from group a in one column and the sum of everything else in another column.
Something like this doesnt work.
select
sum(Price) as PriceGroupA
sum(Price) as PriceRest
from
Table
where
Group='a'
Group<>'a'
Is there a way to achieve this functionality?
SELECT
sum(case when [Group] = 'a' then Price else 0 end) as PriceA,
sum(case when [Group] <> 'a' then Price else 0 end) as PriceRest
from
Table
Please try:
select
sum(case when [Group]='A' then Price end) PriceA,
sum(case when [Group]<>'A' then Price end) PriceRest
from
Table
SQL Fiddle Demo
You just need two sub-queries:
SELECT (SELECT SUM(PRICE)
FROM Table1
WHERE [Group] ='a') AS PriceGroupA,
(SELECT SUM(PRICE)
FROM Table1
WHERE [Group]<>'a') AS PriceRest
Demo-Fiddle
Related
I have a table contain records as per below image. I want to do a count for each status and am able to do that by selecting each type of status. Which I will needs to exec 4 query to get the result. I would like to know how can I achieve that by using single query statement? Any advices or suggestion is welcome and highly appreciated. Thanks in advance.
WITH CTE(NO,STATUS)AS
(
SELECT 1,'OPEN' UNION ALL
SELECT 2,'OPEN'UNION ALL
SELECT 3,'BILLED'UNION ALL
SELECT 4,'CANCELLED'UNION ALL
SELECT 5,'BILLING'UNION ALL
SELECT 6,'BILLED'UNION ALL
SELECT 7,'CANCELLED'UNION ALL
SELECT 8,'BILLING'UNION ALL
SELECT 9,'CONFIRM'UNION ALL
SELECT 10,'IN PROGRESS'UNION ALL
SELECT 11,'OPEN'UNION ALL
SELECT 12,'CONFIRM'
)
SELECT
SUM(CASE WHEN C.STATUS='BILLED'THEN 1 ELSE 0 END)AS BILLED,
SUM(CASE WHEN C.STATUS='BILLING'THEN 1 ELSE 0 END)AS BILLING,
SUM(CASE WHEN C.STATUS='CANCELLED'THEN 1 ELSE 0 END)AS CANCELLED,
SUM(CASE WHEN C.STATUS NOT IN ('CANCELLED','BILLING','BILLED')THEN 1 ELSE 0 END)AS UNBILL
FROM CTE AS C
CTE is an example you have provided. Please replace reference to it with reference to your table
I'm struggling with a grouping query.
I have simple table named CarParts where some car elements stored in it.
Some of those elements are available (with Type prefix "05") and some are blocked (Type prefix "01").
I want to write select query that would group my table CarParts by SerialNr and Type as shown below on the right side.
Do you want conditional aggregation?
select serialnr, name,
sum(case when type like '%-05' then amount else 0 end) as [05-available],
sum(case when type like '%-01' then amount else 0 end) as [01-blocked]
from carparts
group by serialnr, name;
You can use PIVOT to get your desired result as below-
SELECT SerialNr,
ISNULL([05-Available],0) [05-Available],
ISNULL([01-Available],0) [01-Available],
Name
FROM
(
SELECT SerialNr,Amount,Name,RIGHT( Type,2) +'-Available' AS P_Column
FROM CarParts
) AS P
PIVOT
(
SUM(Amount)
FOR P_Column IN ([01-Available],[05-Available])
) AS PVT
you can use case when
select SerialNr,Name,
sum(case when right([Type],2)='01' then amount else 0 end) as blocked_01
sum(case when right([Type],2)='05' then amount else 0 end) as availabe_05
from tbale_name group by SerialNr,Name
select SerialNr,
sum(case when Type like '%-05' then Amount else 0 end) as '05-available',
sum(case when Type like '%-01' then Amount else 0 end) as '01-blocked',
Name
from carparts
group by SerialNr, Name
I am working on a data that looks like this:
from the table the gender (M,F) has the same globalid, however, I need to sum the distinct globalid's value column based on the gender (total from M and F).
I have tried this code but the query just returned the same data.
select distinct (globalid) globalid, fcname, featureidentifier,
gender, source, wardcode,sum (distinct value)
from public.kano_pp
source= 'Worldpop / ORNL Adjusted'
group by globalid, fcname, featureidentifier,gender, source, wardcode, value
order by globalid;
I think you want something more like this:
select globalid,
sum(case when gender = 'F' then value else 0 end) as female_value,
sum(case when gender = 'M' then value else 0 end) as male_value
from public.kano_pp
where source = 'Worldpop / ORNL Adjusted'
group by globalid
order by globalid;
The more recent versions of Postgres support the filter clause which is a bit more efficient than the sum(case . . ).
I want to add the below queries as the column values without creating a table.
Select 'NetworkKey' as AuthKey
Select count(NetworkSK) as Totalcount from EDW.Fact.AuthorizationRequest
Select count(*) as NUllcount from EDW.Fact.AuthorizationRequest where NetworkSK is NULL
Select count(*) as NotNullcount from EDW.Fact.AuthorizationRequest where NetworkSK is Not NULL
My result should look like this without creating a table physically...
AuthKey Totalcount Nullcount NotNullCount
NetworkKey 100 5 95
YOU WANT TO DO IT LIKE THIS BECAUSE THIS WILL WORK TO SOLVE YOUR PROBLEM.
SELECT 'NetworkKey' AS AuthKey,
COUNT(*) AS TotalCount,
SUM(CASE WHEN NetworkSK IS NULL THEN 1 ELSE 0 END) AS NUllcount,
SUM(CASE WHEN NetworkSK IS NOT NULL THEN 1 ELSE 0 END) AS NotNullcount
FROM EDW.Fact.AuthorizationRequest
Happy holidays.
I am trying to import some data to Excel from a database using SQL.
I am getting the data in the following columns:
Item# Location Qty
but I would like to import it in the following format:
Item# Qty#Location1 Qty#Location2 Qty#Location3
Select Distinct item#,
CASE WHEN location="location1" THEN qty END AS QTY#Location1,
CASE WHEN location="location2" THEN qty END AS QTY#Location2,
CASE WHEN location="location3" THEN qty END AS QTY#Location3
From table1
This, however returns 3 records.Is there any way to merge these to a single row?
You want to do an aggregation:
Select item#,
MAX(CASE WHEN location="location1" THEN qty END) AS QTY#Location1,
MAX(CASE WHEN location="location1" THEN qty END) AS QTY#Location1,
MAX(CASE WHEN location="location1" THEN qty END) AS QTY#Location1
From table1
Group By item#;
Yes, I think agregating the rows will work
Select item#,
Sum(CASE WHEN location="location1" THEN qty else 0 END) AS QTY#Location1,
Sum(CASE WHEN location="location1" THEN qty else 0 END) AS QTY#Location1,
Sum(CASE WHEN location="location1" THEN qty else 0 END) AS QTY#Location1
From table1
group by
item#
assuming that ´location1´ ´location2´ and ´location3´ are constants you can do:
SELECT isnull(isnull(loc1.Item, loc2.Item), loc3.Item) as Item,
loc1.Qty as QTY#Location1, loc2.Qty as QTY#Location2, loc3.Qty as QTY#Location3
FROM
(select Item, Qty from table1 where Location = 'location1') as loc1
FULL JOIN
(select Item, Qty from table1 where Location = 'location2') as loc2 ON loc1.Item = loc2.Item
FULL JOIN
(select Item, Qty from table1 where Location = 'location3') as loc3 ON isnull(loc1.Item, loc2.Item) = loc3.Item
A PIVOT will do this for you quite neatly, if your DBMS supports it. [Here's] an SO question on the subject.