If I have a table look like in this page and I wanna sum the orderPrice by each customer.
Then I wanna show the result show in one row look like this.
How can I write the query.
Ps. Sorry for show everything on web.
Since you did not put any restrictions on how that information should be produced, you can do the following:
Select Sum( Case When Customer = 'Hansen' Then OrderPrice End ) As sumHansen
, Sum( Case When Customer = 'Nilsen' Then OrderPrice End ) As sumNilsen
, Sum( Case When Customer = 'Jensen' Then OrderPrice End ) As sumJensen
From CustomerTotals
If your'e using SQL Server you can use PIVOT statement.
Related
How to use CASE to show 'debtor' if at least one of the invoices is not paid (Invoice_Status = 1)?
*(A customer has multiple invoices)
Here's the query to show the an Id_Customer and the status of his multiple invoices, but this repeats the Id_Customer for each invoice he has
SELECT Id_Customer,
CASE Invoice_Status
WHEN 1 THEN 'Debtor'
ELSE 'No debts'
END AS Status
FROM Tb_Invoices
ORDER BY Id_Customer
GO
You need to use an aggregate (min, max, count etc) on Invoice_Status, then map that value via you case statement. You haven't told us about your schema or data so we can't really help you more than that. I.e. something like this:
SELECT CASE
WHEN count(Invoice_Status = 1) > 0 THEN 'Dector'
ELSE 'No debts'
END
FROM Tb_Invoices
ORDER BY Id_Customer
I would like to group by with the case statements in SQL Server. I created cases that's my revenue range. I'd like to calculate the number of sales orders within all these revenue ranges(cases)
Below please find my queries:
Select
sum(OrderQuantity) as Orders,
case when (sum(SalesAmount-TaxAmt-Freight)<100 and sum(SalesAmount-TaxAmt-Freight)>=0) then '$0-$100'
when (sum(SalesAmount-TaxAmt-Freight)>=100 and sum(SalesAmount-TaxAmt-Freight)<500) then '$100-$500'
when (sum(SalesAmount-TaxAmt-Freight)>=500 and sum(SalesAmount-TaxAmt-Freight)<1000) then '$500-$1000'
when (sum(SalesAmount-TaxAmt-Freight)>=1000 and sum(SalesAmount-TaxAmt-Freight)<2500) then '$1000-$2500'
when (sum(SalesAmount-TaxAmt-Freight)>=2500 and sum(SalesAmount-TaxAmt-Freight)<5000) then '$2500-$5000'
when (sum(SalesAmount-TaxAmt-Freight)>=5000 and sum(SalesAmount-TaxAmt-Freight)<10000) then '$5000-$10000'
when (sum(SalesAmount-TaxAmt-Freight)>=10000 and sum(SalesAmount-TaxAmt-Freight)<50000) then '$10000-$50000'
when (sum(SalesAmount-TaxAmt-Freight)>=50000 and sum(SalesAmount-TaxAmt-Freight)<100000) then '$50000-$100000'
when (sum(SalesAmount-TaxAmt-Freight)>=100000) then '>$100000'
end as SalesAmountCategory
From
dbo.FactResellerSales
group by
SalesAmountCategory;
I expect to get the result like:
result example
I keep getting errors when i try to group by based on the case statements. The error is "Invalid column name 'SalesAmountCategory'". How can i do that? Many thanks in advance!
It looks like you want to know number of orders in each category
So, you need first to map each order to category and then group by it, like that:
select SalesAmountCategory, count(*) from
(
Select case
when ((SalesAmount-TaxAmt-Freight)>=100000) then '>$100000'
when ((SalesAmount-TaxAmt-Freight)>=50000) then '$50000-$100000'
when ((SalesAmount-TaxAmt-Freight)>=10000) then '$10000-$50000'
when ((SalesAmount-TaxAmt-Freight)>=5000) then '$5000-$10000'
when ((SalesAmount-TaxAmt-Freight)>=2500) then '$2500-$5000'
when ((SalesAmount-TaxAmt-Freight)>=1000) then '$1000-$2500'
when ((SalesAmount-TaxAmt-Freight)>=500) then '$500-$1000'
when ((SalesAmount-TaxAmt-Freight)>=100) then '$100-$500'
when ((SalesAmount-TaxAmt-Freight)<100) then '$0-$100'
end as SalesAmountCategory
From dbo.FactResellerSales
) as t
group by SalesAmountCategory
I got a simple thing to do.
Well, maybe not, but someone somewhere surely can help me out : P
I got a simple data structure that contains
expedition date
delivery date
transaction type
I would need to create a query which could
order the rows by a date specific to the transaction type.
(ie : using the expedition date for transaction of type "selling", and delivery date for transaction of type "purchasing")
I was wondering if there was a more efficient way to do this than
by fetching 2 times the same data with different clause where(while adding a column used to order them(tempDate)) and then using another select to encompass these 2 queries to which I would add the order clause on the tempDate.
--> the initial fetching I would do 2 times works on many tables(many, many, many joins)
Basically my current solution is :
Select * from
(
Select ...
date_exp as dateTemp;
from ...
where conditions* And dateRelatedCondition
UNION
Select ...
date_livraison as dateTemp;
from ...
Where conditions* And NOT(dateRelatedCondition)
) as comboSelect
Order By MIN(comboSelect.dateTemp)
OVER(PARTITION BY(REF_product)),
(REF_product),
comboSelect.dateTemp asc;
*
->Those conditions are the same in both inner Select query
Thank you for your time.
Without the UNION:
dateRelatedCondition should be removed from WHERE and put to the SELECT like:
CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END as dateTemp
Without the subquery:
in ORDER BY you need the same expression in the window function:
Order By MIN(CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END)
OVER(PARTITION BY(REF_product)),
(REF_product),
dateTemp asc
You mean like this?:
ORDER BY CASE
WHEN TransactionType = 'Selling' THEN ExpeditionDate
WHEN TransactionType = 'purchasing' THEN DeliveryDate
END
I have the following query where if brand1/camp1 taken individually, query returns the correct value but if I specify more than one brand or campaigns, it returns some other number and I am not sure what the math is behind that. It is not the total of the two either.
I think it is IN operator that is specifying OR with "," as opposed to what I require it to do which is consider AND
select campaign,
sum(case when campaign in ('camp1', 'camp2') and description in ('brand1', 'brand2') then orders else 0 end) as brand_convs
from data.camp_results
where campaign in ('camp1', 'camp2') and channel='prog' and type='sbc'
group by campaign
having brand_convs > 0
order by brand_convs desc;
Any thoughts?
The problem is in the IN part as you suspected: The two IN operators do not affect eachother in any way, so campaign can be camp1 while description is brand2.
If your DBMS supports multiple columns in an IN statement, you use a single IN statement:
SELECT campaign, SUM(
CASE WHEN (campaign, description) IN (
('camp1', 'brand1'),
('camp2', 'brand2')
) THEN orders ELSE 0 END
) [rest of query...]
If not, you're probably going to have to use ANDs and ORs
SELECT campaign, SUM(
CASE WHEN
(campaign='camp1' AND description='brand1')
OR (campaign='camp2' AND description='brand2')
THEN orders ELSE 0 END
) [rest of query...]
i've ID_NOVEO/MKP_Number - MKP_Customer - DoubleCheck
in SSRS i whant the last column is calculated value.
I wanna check if ID_NOVEO is more than 1.
Than it put 1 in "DoubleCheck" field.
Thanks
PS: in sql is this:
You can create a group on ID_Noveo, which you can make hidden.
On the detail your expression should look like the one below
= Iif(CountRows("table1_YourGroupName")>1,1,0)
Can you not just do this in SQL and pass the results to the report. Something like
SELECT DISTINCT
ID_NOVEO, MKP_CustomerKey
, CASE WHEN COUNT(*) OVER(PARTITION BY ID_NOVEO) >1 THEN 1 ELSE 0 END AS MoreThanOne
FROM YourTable