I have data like so:
NAME TITLE SALARY HIREDATE
-------------------------------
HANK BOSS 100 1/1/2016
JOHN JERK 100 1/1/2015
MIKE PUNK 200 1/1/2014
We want to show this data as such:
NAME HANK JOHN MIKE
-------------------------------
TITLE BOSS JERK PUNK
SALARY 100 100 200
HIREDATE 1/1/16 1/1/15 1/1/14
This is how my client needs to see the data, unfortunately.
How can this be done with SSRS (sql 2012)?
I tried to create a Matrix, I was able to get the names as columns on top.
But when I tried to do the rest of it, no luck.
Thanks!
You can use a matrix with column groups to pivot your data, you will need the settings of your matrix look similar to this:
Just add NAME in the Column Groups pane, then right click the first row and select Insert Row - inside the group, do that three times each for TITLE, SALARY and HIREDATE.
For SALARY use =SUM(Fields!SALARY.Value). Also you may want to hide the last empty row, so select the entire row, right click it and go to Row Visibility, select Hide.
You will get:
Let me know if this helps.
Related
I have a classic report driven by a SQL query.
How can I dynamically set column headings, based on the value of another column?
For example, my SQL returns columns A, B, VERSION. I'd like the classic report column heading for SQL column A to be 'Foo' if VERSION is 1, but 'Bar' if VERSION is 2.
I don't know what you meant by saying that the query is constrained by primary key.
Anyway, here's a suggestion which might (or might not) help.
Based on sample SCOTT schema, I created a simple classic report as
select e.ename, e.job
from emp e
where e.deptno = :P42_DEPTNO
I also created a P42_DEPTNO item which - kind of - constrains the result to just one department. For example, if you enter 10 into P42_DEPTNO, you'll get employees that work in the ACCOUNTING department.
Furthermore, I created a hidden item P42_DNAME whose souce is a SQL query
select dname
from dept
where deptno = :P42_DEPTNO
and it returns department name for the P42_DEPTNO value. Its "Used" property is set to "Always, replacing any existing value in session state". This item (P42_DNAME) will be used as a custom heading for the ENAME column returned by the report.
In order to do that, open ENAME column's properties and put this into the "Name" property: &P42_DNAME. (literally ampersand + item name + dot - don't forget the trailing dot!).
That's all;
run the report
enter 10 into P42_DEPTNO item
press ENTER key
report will display employees that work in department 10, and ENAME column's heading will be ACCOUNTING
Update queries have never been my strong point, I am hoping someone can help me make a more efficient query?
I am trying to update a table with the total sales for a given product, for a given customer.
The table I am looking to update is the sales column of the below 'Estimate' table:
ID Customer Product Estimate Sales
--------------------------------------------
1 A 303 100 20
2 A 425 20 30
3 C 1145 500 250
4 F 801 25 0
The figure I am using to update is taken from the 'Sales' view:
Product Customer Actual
------------------------------
303 A 30
500 C 2
425 A 88
1145 C 700
The query I have written is:
UPDATE estimate e
SET e.sales = (SELECT s.actual FROM sales s
WHERE e.customer = s.customer and e.product = s.product)
WHERE EXISTS (SELECT 1 sales s
WHERE e.customer = s.customer and e.product = s.product)
An added complication is that 'estimates' exists between a range of dates and need to be updated for sales during that period only. My 'sales' view above take care of that, but I have left this out of the example for simplicity sake.
I initially ran the query using test data of only around 20 records and it ran in around 3 /4 seconds. My actual data is 7,000+ records and when I run the query here, my browser times out before I get any results.
I suspect that the query is updating the whole table for every record in the view or vice versa?
Any help much appreciated.
Cheers
Andrew
Try a merge instead:
merge into estimate tgt
using sales src
on (tgt.customer = src.customer and tgt.product = src.product)
when matched then
update tgt.sales = src.actual;
By doing the merge instead of an update, you negate the need to repeat the query in the set clause in the where clause, which ought to speed things up a bit.
Another thing to check is how many indexes do you have on the estimate table that has the src column in it? If you have several, it might be worth reducing the number of indexes. Each index that has to be updated is an overhead when you update the row(s) in the table.
Also, do you have triggers on the estimate table? Those might be slowing things down as well.
Or maybe you're missing an index on the sales table - an index on (customer, product and sales) ought to help, as the query should then be able to avoid going to the table at all since the data it needs from that table would all be in the index.
Another argument you could have is to not do the update at all. If the information is available in the Sales table, why do you need to bother updating the estimate table at all? You could do that as a join when querying the estimate table. Of course, it depends on how often you'd be querying for the estimate vs actual sales information vs how often they'd be updated. If you update frequently and read infrequently, then I'd skip the update and just query the two tables directly.
With data given
Id sdate sales
1 15.03.2015 150
2 16.03.2015 170
where id+date is unique combination
one could easily find the best date, or best item to sale.
Select max(date) keep(dense_rank last order by sales) from data.
So far so good. But suppose we have data like following:
Id sdate sales
1 15.03.2015 150
2 16.03.2015 170
1 15.03.2015 117
2 16.03.2015 97
… some other dates with worst sale sums than 15.03.2015 and 16.03.2015
Now I want to know the best DATES to sale
Select max(sdate) keep(dense_rank last order by sum(sales)) from data group by sdate.
Hey! It shows only 15.03.2015. But I want to see it both – 15.03.2015 and 16.03.2015.
LISTAGG doesn’t help here too. Only
Select sdate from data group by sdate
Order by sum(sales) DESC FETCH FIRST ROW WITH TIES
Returns me both dates. So, bye KEEP DENSE_RANK? Meet FETCH FIRST?
What is your opinion , respective all?
They're doing different things. keep can only return one row for each group. As you want to see tied values, you can't use keep, but you could do this with an inline view:
select sdate
from (
select sdate, dense_rank() over (order by sum(sales) desc) as rnk
from data
group by sdate
)
where rnk = 1;
Which is essentially what fetch first rows with ties is doing in 12c in this example.
There are situations where keep is appropriate, and others where an inline view or fetch first rows is appropriate, and some where either would work.
Having a scenario where you can't use keep to get the result you want doesn't mean you should never use it. Your first simpler query could use either approach; if you wanted other information then keep would come into its own (like the examples in the documentation for first). There are a lot of tools available and you need to pick the best one for what you're trying to achieve.
I am using Oracle SQL Plus.
Please refer to screenshot for reference.
After deletion of data of column sno=4, I want the output of sno to be displayed in serial order ie 1,2,3,4 instead of 1,2,3,5.
Please suggest the SQL query to achieve same.
try:
select ROWNUM as SNO, NAME,DOJ from company order by SNO
If you want the numbering to be persistent, you will need to update the SNO for all the remaining records in the table.
What you have done, by deleting the row is say that "row number 4 no longer exists". If you want to shuffle the remaining rows up, you will need to perform the following:
DELETE FROM company WHERE name='Flipkart';
UPDATE company SET sno=sno-1 WHERE sno>=4;
An alternative, if you don't want to alter the contents of the table itself.. But you want it to display correctly (specifically for this example)
SELECT
CASE
WHEN sno <= 4 THEN sno
ELSE sno-1
END AS sno,
name,
doj
FROM company
ORDER BY sno;
I will try to put forward this question in a simple way. Consider that I have a table with two columns (Name, Contact_No). We can have the same name but with different Contact No in the table. All I want to know is to find out which name is NOT repeated in the entire table. Or in other words, which name is unique in this table and has appeared only once.. This is just an example, the actual scenario is quite different but if anyone can help me with this example, I'll be able to handle the actual scenario.
Here is an example
Name Contact_No
A 123
A 124
B 125
C 126
C 127
All I want is to find (B) which is not repeated in the entire table. Thanks
You can simply do this:
SELECT name FROM tbl_name GROUP BY name HAVING COUNT(name) = 1
Check Out SQLFIDDLE