I'm new to using views, and I'm not exactly sure if what I want to do is possible using a view.
The first table is my original data file that I have imported into SQL. I created a view with only the fruit and amount_from_us columns, and I'm having trouble figuring out how to include the amount in there. Normally, I'd use a where clause, but I don't know how I can do that at the same time as selecting the other data.
Here is what I have so far:
CREATE VIEW fruit_summary AS
SELECT fruit
, SUM(amount) AS amount
FROM original_table
WHERE bought_from_us = 'yes'
GROUP BY fruit
This gets me the fruit column and the amount_from_us column. I am however lost on how to get the date and total amount in there. Is this even possible using views or should I just create a table and use joins?
Try:
SELECT fruit,
[date],
SUM(amount) AS amount,
SUM(case when bought_from_us = 'yes' then amount else 0 end)
AS amount_from_us
FROM original_table
GROUP BY fruit, [date]
create table #original_table
(
[date] datetime,
fruit varchar(50),
amount money,
bought_from_us char(3)
)
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/18/2012', 'Apple', 10, 0);
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/18/2012', 'Apple', 25, 'yes');
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/18/2012', 'Orange', 32, 0);
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/18/2012', 'Banana', 8, 0);
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/18/2012', 'Banana', 235, 'yes');
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/17/2012', 'Apple', 65, 0);
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/17/2012', 'Apple', 4, 'yes');
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/17/2012', 'Orange', 56, 0);
insert #original_table([date], fruit, amount, bought_from_us)
values ('01/17/2012', 'Orange', 95, 0);
What you've asked for in the result is quite complex. To get the last record, Jan-17 Banana 0 0, you need something like this:
with date_fruit_table as
(
select date_table.[date], fruit_table.fruit
from
(select distinct fruit from #original_table) as fruit_table,
(select distinct [date] from #original_table) as date_table
)
select date_fruit_table.[date], date_fruit_table.fruit,
SUM(isnull(#original_table.amount, 0)) as amount,
SUM(case #original_table.bought_from_us when 'yes' then #original_table.amount else 0 end) as amount_from_us
from date_fruit_table
left outer join #original_table on #original_table.fruit = date_fruit_table.fruit
and #original_table.[date] = date_fruit_table.[date]
group by date_fruit_table.[date], date_fruit_table.fruit
order by date_fruit_table.[date] desc
SELECT fruit
, SUM(amount) AS amount
, SUM(amount_from_us) AS amount_from_us
, [Date]
FROM original_table
WHERE bought_from_us = 'yes'
GROUP BY fruit, [Date]
sorry about my first answer, now I realize what you need:
with CTE_fruit as (
SELECT fruit, date, SUM(amount) AS amount, NULL AS amountUS
FROM original_table
GROUP BY fruit, date
union
SELECT fruit, date, NULL AS amount, SUM(amount) AS amountUS
FROM original_table
WHERE bought_from_us = 'yes'
GROUP BY fruit, date
)
select fruit, date, sum(amount), sum(amountUS) from CTE_fruit
GROUP BY fruit, date
I think this will work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.