SSRS Report - Complex Layout? - sql

I need to create a report, but I'm unsure how to get the layout correct.
The way the report should look is Here
The data is currently formatted like so:
Customer No | Sale Price | Cost Price | Margin | Date
Customer A | 200 | 100 | 100 | 1/1/14
Is it possible to design a report with this layout? I suspect so, but I haven't encountered this issue yet.
Any hints and tips to get me on my way?
Thanks!

Since what you want is actually a cross-tab, it is easier if your dataset is structured like this:
Customer No Price Type Date Amount
----------- ---------- ------- ------
Customer A Sale 1/1/14 200
Customer A Cost 1/1/14 100
Customer A Margin 1/1/14 100
...
To achieve this, simply use UNION ALL in the SQL statement of your dataset, like so:
SELECT [Customer No], 'Sale' AS [Price Type], [Date], [Sale Price]
FROM MyTable
UNION ALL
SELECT [Customer No], 'Cost' AS [Price Type], [Date], [Cost Price]
FROM MyTable
UNION ALL
SELECT [Customer No], 'Margin' AS [Price Type], [Date], [Margin]
FROM MyTable
With a dataset like this, it is straightforward to get the report layout you want, for example using the Tablix Wizard.

I managed to get it working. It actually wasn't too difficult, to my surprise. This probably isn't the best way to do things, but it worked well for me!
Query
SELECT sa.[Document No_]
,[sa.Customer No_]
,DATEPART(m, sa.[Posting Date]) AS MonthName
,sa.Quantity
,sa.[Amount (LCY)]
,sa.[Cost (LCY)]
,sa.[Profit (LCY)]
,c.[Salesperson Code]
,c.NAME
FROM [Sales Analysis] AS sa
INNER JOIN [Customer] AS c ON c.[No_] = sa.[customer no_]
WHERE [Posting Date] BETWEEN '2014-01-01' AND '2014-05-31'
AND [Customer No_] IS NOT NULL
AND [Customer No_] <> ''
Matrix Layout
Results
Thanks for the help!

Related

SQL create column for every week (Loop?)

I need to make a report for weekly changes.
This is the code for todays amount
SELECT
[Entry No_],
[Customer No_],
[Posting Date],
[Description],
[Currency Code],
Trans_type = case when [Deposit]=1 then 'Deposit'
when [Imprest]=1 then 'Imprest'
else 'Other' end,
A.Amount
FROM Table1
LEFT JOIN
(
SELECT Distinct [Cust_ Ledger Entry No_],
SUM ([Amount EUR]) as 'amount'
FROM Table2
group by [Cust_ Ledger Entry No_]
having
SUM ([Amount EUR]) <> '0'
)A
on [Entry No_] = A.[Cust_ Ledger Entry No_]
Where
A.Amount is not NULL
Code to generate data for previous week is here (adding only where clause):
SELECT
[Entry No_],
[Customer No_],
[Posting Date],
[Description],
[Currency Code],
Trans_type = case when [Deposit]=1 then 'Deposit'
when [Imprest]=1 then 'Imprest'
else 'Other' end,
A.Amount
FROM Table1
LEFT JOIN
(
SELECT Distinct [Cust_ Ledger Entry No_],
SUM ([Amount EUR]) as 'amount'
FROM Table2
where [posting Date] < '2020-11-23'
group by [Cust_ Ledger Entry No_]
having
SUM ([Amount EUR]) <> '0'
)A
on [Entry No_] = A.[Cust_ Ledger Entry No_]
Where
A.Amount is not NULL
It would be enough to union both queries and then export to Excel and make pivot, but problem is that I need results of last 50 weeks. Is there any smart way to avoid union 50 tables and run one simple code to generate weekly report?
Thanks
it might be easier with sample, but I don't know how to paste table here..
Maybe it is true, i dont need union here, and group by would be enough, but it stills sounds difficult for me :)
Ok. Lets say table has such headers: Project | Country | date | amount
The code below returns amount for todays date
Select
Project,
SUM(amount)
From Table
Group by Project
I actually need todays date and also the results of previous weeks (What was the result on November 22 (week 47), November 15 (week 46) and so on.. total 50 weeks from todays date).
Code for previous week amount is here:
Select
Project,
SUM(amount)
From Table
Where Date < '2020.11.23'
Group by Project
So my idea was to create create 50 codes and join the results together, but i am sure it is a better way to do this. Besides i dont want to edit this query every week and add a new date for it.
So any ideas, to make my life easier?
if I have understood your requirement correctly, all you need to do is extract the week from the date e.g.
Select
Project,
datepart(week, date),
SUM(amount)
From Table
Where Date < '2020.11.23'
Group by Project, datepart(week, date)

MS ACCESS: Unable to retrieve LAST Price Paid based on Maximum Date

OBJECTIVE
Develop a sales catalog for a COMPANY ID based on ITEM ID and latest PRICE paid (based on LAST SHIP DATE).
APPROACH
Pull in CUSTOMER, SALES, ITEM Tables
Run Query Link tables based off of CUSTOMER ID and ITEM to understand purchase history
Export a table showing a COMPANY ID, ITEM ID, LAST SALES PRICE, LAST SHIP DATE
CODE
SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
(CCur([SDLPRC])/10000) AS PRICE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM,
(CCur([SDLPRC])/10000);
ISSUE/QUESTION
CUSTOMER A bought ITEM ABC # 3 different prices on 3 different dates. I've taken the Max of Ship Date in hope to show the LAST PRICE PAID (resulting in one single value for price). However, for some reason, I am still receiving the three different prices on three different dates. How can I have MS Access only display the latest price based off of the latest ship date?
NOTE: SDLPRC = "Sold Price". I have to convert SLDPRC into a Currency and then divide by 1000; this is due to our current database setup. Also, SDAITM is an "Abbreviated Item Number" that is more customer-friendly.
The problem is that you're grouping by your Price variable (CCur([SDLPRC])/10000). When you use GROUP BY, Access/SQL will split the rows by all the variables in the GROUP BY statement. So you need to not group by price.
Change your query to use a subquery that finds the last date of a sale grouped by [Company ID], [Item ID] and Style. The use an outer query to grab the price for that particular record. Something like:
SELECT b.[COMPANY ID], b.[ITEM ID], b.STYLE, b.[LAST SHIP DATE], CCur(a.[SDLPRC])/10000 as PRICE
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)] as a
INNER JOIN
(SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM
) as b
ON a.SDAN8 = b.[COMPANY ID]
and a.SDITM = b.[ITEM ID]
and a.SDAITM = b.STYLE
and a.SDDRQJ = b.[LAST SHIP DATE]

MS Access how to check start date and first sales

I'm having some difficulties trying to figure out when a sales rep makes their first sales.
I have two tables
The first table is the sales rep with their information setup like so
ID | First Name | Last Name | Start Date |
My second table is a table of all the sales combined from every rep like so
Order Number | Order Date | REP ID | Sales Amount |
I'm trying to create a query where I can list the Rep information, and when their first sales date was.
Some help would be great!
Thanks
You have to JOIN the two tables to get the information. Since you are only interested in the First Sale for every employee, you need to make use of the Min. Something like
SELECT
ID,
[First Name],
[Last Name],
[Start Date],
Min([Order Date]) As [First Sale]
FROM
firstTable INNER JOIN secondTable
ON
firstTable.ID = secondTable.[Rep ID]
GROUP BY
ID,
[First Name],
[Last Name],
[Start Date]

Adding New Records from one table with existing and new records to another table in SQL

I'm trying to to append data to a table that contains all the data up to this point. Every week I will be pulling in the new data (which will contain data already existing in the All table) and adding the new records. I added a few test data to the temp table where the generic, material num, etc. are all different but when I run this query it still says it is adding 0 records. Please help.
INSERT INTO ExtWafersAll ( generic, [material number], description, vendor, [net price], [std price], NumberOfDups )
SELECT
ExtWafersTemp.generic,
ExtWafersTemp.[material number],
ExtWafersTemp.description,
ExtWafersTemp.vendor,
ExtWafersTemp.[net price],
ExtWafersTemp.[std price],
ExtWafersTemp.NumberOfDups
FROM ExtWafersTemp
RIGHT JOIN ExtWafersAll
ON (ExtWafersAll.NumberOfDups = ExtWafersTemp.NumberOfDups)
AND (ExtWafersAll.[std price] = ExtWafersTemp.[std price])
AND (ExtWafersAll.[net price] = ExtWafersTemp.[net price])
AND (ExtWafersAll.vendor = ExtWafersTemp.vendor)
AND (ExtWafersAll.description = ExtWafersTemp.description)
AND (ExtWafersAll.[material number] = ExtWafersTemp.[material number])
AND (ExtWafersAll.generic = ExtWafersTemp.generic)
WHERE
ExtWafersTemp.vendor <> ExtWafersAll.vendor
OR ExtWafersTemp.description <> ExtWafersAll.description
OR ExtWafersTemp.[material number] <> ExtWafersAll.[material number]
OR ExtWafersTemp.generic <> ExtWafersAll.generic;
So for example in ExtWafersTemp we have:
Generic Material Number Description Vendor Net Price Std Price
j2151 sjkdga215 xxx125125 TMA 12 14
asdg asgasg aggsggs asg 15 18
And then in ExtWafersAll:
Generic Material Number Description Vendor Net Price Std Price
j2151 sjkdga215 xxx125125 TMA 12 14
I can't figure out how to add the new record thats in the temp to the all file
Maybe this would suit your need:
insert into ExtWafersAll ( generic, [material number], description, vendor, [net price], [std price], NumberOfDups )
select generic, [material number], description, vendor, [net price], [std price], NumberOfDups
from ExtWafersTemp
except
select generic, [material number], description, vendor, [net price], [std price], NumberOfDups
from ExtWafersAll;
In above snippet you add records from ExtWafersTemp table which are not present in ExtWafersAll table. Is this what are you trying to achieve?
About "except" operator you could read here: http://en.wikipedia.org/wiki/Set_operations_%28SQL%29
UPDATE
As it occurred to be MS Access problem you could try to test this:
SELECT
ExtWafersTemp.generic,
ExtWafersTemp.[material number],
ExtWafersTemp.description,
ExtWafersTemp.vendor,
ExtWafersTemp.[net price],
ExtWafersTemp.[std price],
ExtWafersTemp.NumberOfDups
FROM ExtWafersAll RIGHT JOIN ExtWafersTemp
ON (ExtWafersAll.NumberOfDups = ExtWafersTemp.NumberOfDups
AND ExtWafersAll.[std price] = ExtWafersTemp.[std price]
AND ExtWafersAll.[net price] = ExtWafersTemp.[net price]
AND ExtWafersAll.vendor = ExtWafersTemp.vendor
AND ExtWafersAll.description = ExtWafersTemp.description
AND ExtWafersAll.[material number] = ExtWafersTemp.[material number]
AND ExtWafersAll.generic = ExtWafersTemp.generic)
WHERE ExtWafersAll.NumberOfDups is null
AND ExtWafersAll.[std price] is null
AND ExtWafersAll.[net price] is null
AND ExtWafersAll.vendor is null
AND ExtWafersAll.description is null
AND ExtWafersAll.[material number] is null
AND ExtWafersAll.generic is null
Genarally it is a following pattern (in example there is a primary key field - id):
select tt.id
from tableall t right join tabletemp tt
on (t.id = tt.id)
where t.id is null
Hope that it helps.

SQL Result Set Merge

I have a limitation where I can only send one result set to a reporting application at any one time, to produce an end report for a customer.
So a query like this
select
[AGENT],
[TRANSDATE],
[RECIPT NO],
[CUSTOMER NAME],
[ORDER NO] ,
[TRANS NO] ,
QUANTITY,
[AMOUNT COST],
From [Customer] C
However I need lots of totals at the bottom such as this query for some of the columns. I cannot make any changes to front end due to it being a legacy reporting application.
select
Sum ( QUANTITY ) as [SUM OF QUANTITY] ,
Sum ( AMOUNT COST ) AS [SUM OF AMOUNT COST]
From [Customer] C
Obviously I simplified the queries I am using. So the question is how to make 2 results sets one result set in SQL?
Union and union all failed due to date columns being defaulted if you use blank for a column in end application.
Rollup or Pivoting or CTE I kinda thought of but cannot see a solution yet.
what about windowed functions?
like...
select
[AGENT],
[TRANSDATE],
[RECIPT NO],
[CUSTOMER NAME],
[ORDER NO] ,
[TRANS NO] ,
QUANTITY,
[AMOUNT COST],
Sum ( QUANTITY ) over () as [SUM OF QUANTITY] ,
Sum ( [AMOUNT COST] ) over () AS [SUM OF AMOUNT COST]
From [Customer] C