How can I calculate a column data for different conditions using single query in sql server? - sql-server-2005

I am using below query to count a particular column data based on single condition.
SELECT COUNT(DISTINCT(S.INVOICENO)) AS LESSTWO , YEAR(S.invoicedate) YER,
Month(S.invoicedate) MNTH
FROM SALESDATA S
where S.BILLINGTYPE='INVOICE'
AND (S.invoicedate >='4-1-2009' and S.invoicedate <='4-30-2010')
AND S.TAXABLEAMT <=2000
GROUP BY YEAR(S.invoicedate) ,Month(S.invoicedate)
ORDER BY YEAR(S.invoicedate) ,Month(S.invoicedate)
But I have to calculate for 3 more conditions. Is it possible to count based on 3 conditions(i.e. 2001 - 3000,3001 - 4000, 4001 - 5000,>5000) and I want result set for all the conditions?
Regards,
N.SRIRAM

Untested, but should be about right:
SELECT CASE
WHEN S.TaxableAmt <= 2000 THEN '<=2000'
WHEN S.TaxableAmt <= 3000 THEN '2001-3000'
WHEN S.TaxableAmt <= 4000 THEN '3001-4000'
ELSE '>5000'
END as Bracket,
COUNT(DISTINCT(S.INVOICENO)) AS LESSTWO , YEAR(S.invoicedate) YER,
Month(S.invoicedate) MNTH
FROM SALESDATA S
where S.BILLINGTYPE='INVOICE'
AND (S.invoicedate >='20090401' and S.invoicedate <='20100430')
GROUP BY
CASE
WHEN S.TaxableAmt <= 2000 THEN '<=2000'
WHEN S.TaxableAmt <= 3000 THEN '2001-3000'
WHEN S.TaxableAmt <= 4000 THEN '3001-4000'
ELSE '>5000'
END,
YEAR(S.invoicedate) ,Month(S.invoicedate)
ORDER BY YEAR(S.invoicedate) ,Month(S.invoicedate)
You have to repeat the case expression in the GROUP BY clause (or put it all in a subquery), and I've fixed your date constants so that they're unambiguous.

I think you really need to split up the query into 3 distinct queries - one for each of the conditions you require

Related

How can I create multiple groups within a column using SQL

I have a data set that looks like the below
follower_count
5,689
15,389
215,869
53,389
28,389
9,389
I essentially want to create 3 different groups.
Group 1: Follower_count between 0-25,000 called micro
Group 2: Follower_count between 25,001 to 50,0000 called mid
Group 3: Follower_count between 50,001 - 300,000 called macro
This is the code I have so far
SELECT pic.followers_count, AVG(engagement_rate / 1000000) AS avgER
FROM `public_instagram_channels` AS pic
WHERE pic.followers_count BETWEEN 0 and 25000 AS micro,
pic.followers_count BETWEEN 25001 and 50,000 AS mid,
pic.followers_count BETWEEN 50,001 - 300,000 as macro
GROUP BY micro, mid, macro
You would use a case expression:
select (case when follower_count < 25000 then 'micro'
when follower_count < 50000 then 'mid'
when follower_count < 300000 then 'maccro'
else 'other'
end) as grp, count(*),
avg(engagement_rate / 1000000)
from `public_instagram_channels` p
group by grp;

How to add static values to SQL QUERY Result set in ORACLE

I have a SQL Query that works perfectly. I am using Oracle 11g. The current SQL Query lists upcoming flights for the next 6 months and then list all past flights afterwards. So my query lists all flights for 2018 first and then all old flights after dating back early as 1998. I need to add two static values to this query. The two static values I want to add are "UPCOMING FLIGHTS" and "PAST FLIGHTS" to distinguish which flights are new and old. How can I achieve this? I need to incorporate this added code with the current SQL query that I have below. I have not been able to do this.
Current SQL Query:
SELECT FLIGHT_NMBR, SCHEDULED_LAUNCH_DATE
FROM FLIGHTS
WHERE DATA_VERSION_NAME = 'WORKING'
AND sequence_nmbr >= 0
ORDER BY (CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY')
BETWEEN add_months(trunc(sysdate, 'MON'), 0)
AND add_months(trunc(sysdate, 'MON'), 6) THEN 1 ELSE 2 END),
(CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY')
BETWEEN add_months(trunc(sysdate, 'MON'), 0)
AND add_months(trunc(sysdate, 'MON'), 6) THEN SCHEDULED_LAUNCH_DATE END),
sequence_nmbr;
Results of current SQL query above:
FLIGHT_NMBR SCHEDULED
------------ ---------
SpX-14 26-JAN-18
69P 09-FEB-18
SpX-DM1 09-MAR-18
54S 13-MAR-18
OA-9 14-MAR-18
55S 29-APR-18
SpX-15 06-JUN-18
SpX-DM2 22-JUN-18
70P 27-JUN-18
1A/R 20-NOV-98
2A 04-DEC-98
How can I achieve the desired result set below:
FLIGHT_NMBR SCHEDULED
------------ ---------
UPCOMING FLIHGTS-------------------------------STATIC VALUE I WANT TO ADD
SpX-14 26-JAN-18
69P 09-FEB-18
SpX-DM1 09-MAR-18
54S 13-MAR-18
OA-9 14-MAR-18
55S 29-APR-18
SpX-15 06-JUN-18
SpX-DM2 22-JUN-18
70P 27-JUN-18
PAST FLIGHTS-----------------------------------STATIC VALUE I WANT TO ADD
1A/R 20-NOV-98
2A 04-DEC-98
Want my dropdown to look like this:
you can do it this way,
SELECT 'UPCOMING FLIHGTS' text, NULL sched
FROM dual
UNION ALL
<your select query for upcoming flights>
UNION ALL
SELECT 'PAST FLIGHTS' text, NULL sched
FROM dual
UNION ALL
<your select query for past flights>
What you want to do should be done at the application level. A SQL query returns a result set, which is a table -- consisting of rows with identical columns. Your new rows are not in the same format.
You can simplify the logic and include the flag on each row;
SELECT which, FLIGHT_NMBR, SCHEDULED_LAUNCH_DATE
FROM (SELECT F.*,
(CASE WHEN to_date(scheduled_launch_date, 'DD-MON-YY') BETWEEN add_months(trunc(sysdate, 'MON'), 0) AND add_months(trunc(sysdate, 'MON'), 6)
THEN 'UPCOMING' ELSE 'PAST'
END) as which
FROM FLIGHTS F
) F
WHERE DATA_VERSION_NAME = 'WORKING' AND sequence_nmbr >= 0
ORDER BY (CASE WHEN which = 'UPCOMING' THEN 1 ELSE 2 END),
(CASE WHEN which = 'UPCOMING' THEN SCHEDULED_LAUNCH_DATE END),
sequence_nmbr;

Spooling count of data with a condition in oracle

Kindly note that I have the below table, I am trying to spool the count of the sum of the amtpd by grouping the duedt where it is less than or equal to 31-dec-2013, but the amtpd that has the duedt of 31-dec-2013 and that has trndt between 01-dec-2013 and 31-dec-2013, should be less that 10000, in this case the count should be 3.
|duedt| |amtp| |trndt|
|10/31/2013| |2007.1| |10/27/2013|
|10/31/2013| |8442.9| |12/16/2013|
|11/30/2013| |10450| |12/16/2013|
|12/31/2013| |2107.1| |12/16/2013|
|12/31/2013| |8342.9| |3/23/2014|
|12/31/2013| |0| |5/5/2014|
I am using the below query but it is giving me zero count
SELECT COUNT (SUM (amtpd))
FROM ln02rep2
WHERE trndt <= '31-dec-2013'
AND (
(SELECT SUM (amtpd)
FROM ln02rep2
WHERE trndt BETWEEN '01-dec-2013' AND '31-dec-2013'
AND duedt = '31-dec-2013'
)>='10450.00')
GROUP BY by duedt;

alternatives to "Having"

I have a SELECT statement that counts the number of instances and then saves in a variable. It has a HAVING clause that does a SUM and a COUNT. However since you have to have a GROUP BY in order to use having, the select statement returns 4 lines that are 1 instead of the total being 4. This then doesn't save the count into the variable as 4 but as 1 which obviously is not what I need so I am looking for an alternative work around.
select count(distinct p1.community)
from
"Database".prospect p1
where
p1.visit_date >= '2013-07-01'
and p1.visit_date <= '2013-09-30'
and p1.division = '61'
group By
p1.community
having
sum(p1.status_1) / count(p1.control_code) >= .16;
This is a reasonable alternative:
select count(*)
from (
select p1.community , sum(p1.status_1) / count(p1.control_code) SomeColumn
from
"Database".prospect p1
where
p1.visit_date >= '2013-07-01'
and p1.visit_date <= '2013-09-30'
and p1.division = '61'
Group By
p1.community
) A
where A.SomeColumn >= .16;

SSRS Expression - Subtract SUMS

I need some help with an SSRS expression that sums up amounts and then subtracts sums. I have a dataset that has accounts and corresponding money/amount values. I'm trying to write an expression that sums up the money/amount values from one group of the accounts in a specified range, and then subtracts it from the money/amount total of another range. For example:
(Sum(amt) where acct between 40000 and 49999) -
(Sum(amt) where (acct between 50000 and 59999) or (acct between 66000 and 69999)) -
(Sum(amt) where acct between 76000 and 79825) -
(Sum(amt) where acct between 89000 and 90399)
I could really use some help translating the SQL logic above into an expression to be used for a textbox in SSRS. Any advice would be really helpful! Thanks!
Try this :-
=Sum(
iif(Fields!acct.Value) >= 1 and
Fields!acct.Value) < 4 ,
Fields!amt.Value,0
)
)
-
Sum(
iif(
(Fields!acct.Value>=5 and Fields!acct.Value<10)
or (Fields!acct.Value>=12 and Fields!acct.Value< 15),
Fields!amt.Value,0
)
)
-
Sum(
iif(
Fields!acct.Value) >= 76000 and
Fields!acct.Value) < 79825 ,
Fields!amt.Value,0
)
)
-
Sum(
iif(
Fields!acct.Value) >= 89000 and
Fields!acct.Value) < 90399 ,
Fields!amt.Value,0
)
)