Sorry i know merge is something completely different in Sql Server but i couldn't think what else to call it.
I have a User-Defined Table Type which looks like
-------------------------------------------------------------------
| Id | Foreign Key | Height | Weight | Width | Length |
-------------------------------------------------------------------
| 01 | 1256 | 12.2 | 15.8 | 14.5 | 15 |
| 02 | 1256 | 18.2 | 15.8 | 25.8 | 28 |
| 03 | 1258 | 14.5 | 11.3 | 56.6 | 32 |
| 04 | 1258 | 14.5 | 1.85 | 32.9 | 64 |
| 05 | 1216 | 25.3 | 16.2 | 12.5 | 86 |
-------------------------------------------------------------------
And I want to be able to do a query or something that gives me the foreign key with the lowest Height, Weight, Width and Length associated with it so I'd have something like
------------------------------------------------------------
| Foreign Key | Height | Weight | Width | Length |
------------------------------------------------------------
| 1256 | 12.2 | 15.8 | 14.5 | 15 |
| 1258 | 14.5 | 1.85 | 32.9 | 32 |
| 1216 | 25.3 | 16.2 | 12.5 | 86 |
------------------------------------------------------------
Is there any functions in Sql Server to achieve this, or can any one point me to any resources that may help?
Thanks
Based on your expected output, this should do the trick:
SELECT [Foreign Key], MIN(Height) AS Height, MIN(Weight) AS Weight,
MIN(Length) AS Length
FROM #YourTableVar
GROUP BY [Foreign Key]
It is straightforward to select the minimum of a column:
select
[Foreign Key],
MIN(Height) AS MinHeight,
MIN(Weight) AS MinWeight,
MIN(Length) AS MinLength
FROM
Table
GROUP BY
[Foreign Key]
Related
I am using Postgresql 9.4.
I have this table recorded:
Colonne | Type | Modificateurs
---------+-----------------------+---------------
noemp | integer | non NULL
nomemp | character varying(15) |
emploi | character varying(14) |
mgr | integer |
dateemb | date |
sal | real |
comm | real |
nodept | integer |
Which has those values inside:
noemp | nomemp | emploi | mgr | dateemb | sal | comm | nodept
-------+-----------+----------------+------+------------+------+------+--------
7369 | SERGE | FONCTIONNAIRE | 7902 | 1980-12-07 | 800 | | 20
7499 | BRAHIM | VENDEUR | 7698 | 1981-02-20 | 1600 | 300 | 30
7521 | NASSIMA | VENDEUR | 7698 | 1981-02-22 | 1250 | 500 | 30
7566 | LUCIE | GESTIONNAIRE | 7839 | 1981-04-02 | 2975 | | 20
7654 | MARTIN | VENDEUR | 7698 | 1981-09-28 | 1250 | 1400 | 30
7698 | BENJAMIN | GESTIONNAIRE | 7839 | 1981-05-01 | 2850 | | 30
7782 | DAYANE | GESTIONNAIRE | 7839 | 1981-06-09 | 2450 | | 10
7788 | ARIJ | ANALYSTE | 7566 | 1982-12-09 | 3000 | | 20
7839 | MAYAR | PRESIDENT | | 1981-11-17 | 5000 | | 10
7844 | ROI | VENDEUR | 7698 | 1981-09-08 | 1500 | 0 | 30
7876 | VIRGINIE | FONCTIONNAIRE | 7788 | 0983-01-12 | 1100 | | 20
7902 | ASMA | ANALYSTE | 7566 | 1981-12-03 | 3000 | | 20
7934 | SIMONE | FONCTIONNAIRE | 7782 | 1982-01-23 | 1300 | | 10
7900 | LYNA | FONCTIONNAIRE | 7698 | 1981-12-03 | 950 | | 30
(14 lignes)
When I make a function to count the number of "nodept" with an asked value like this one:
CREATE OR REPLACE FUNCTION depcount(integer)RETURNS integer AS $$
DECLARE
somme integer;
BEGIN
SELECT DISTINCT(COUNT(*)) FROM EMP WHERE nodept=$1 INTO somme ;
RETURN somme;
END$$
LANGUAGE plpgsql;
with a SELECT depcount(30) FROM EMP;
I get this answer:
----------
6
6
6
6
6
6
6
6
6
6
6
6
6
6
(14 lignes)
14 results, as I should normally have only one.
I have to specify that I'm doing this for a course and I can't change the postgresql version, which must be 9.4.
If you have any idea why I get 14 results instead of one ?
thank you.
You're executing the function once per row, running the SELECT COUNT(*) 14 times and getting the result once for each row.
You probably want SELECT depcount(30) (without aFROM clause), to run the function only once.
On a side note, using a function for this sort of query is a bit overkill in most case in my opinion. You also don't need to use plpgsql, language sql would be enough here (though your function may be a bit more complicated than in your example). Using DISTINCT(COUNT(*)) doesn't really make sense either.
I am having a hard time trying to wrap my head around the pivot/unpivot concepts and hoping someone can help or give me some guidance on how to approach my problem.
Here is a simplified sample table I have
+-------+------+------+------+------+------+
| SAUID | COM1 | COM2 | COM3 | COM4 | COM5 |
+-------+------+------+------+------+------+
| 1 | 24 | 22 | 100 | 0 | 45 |
| 2 | 34 | 55 | 789 | 23 | 0 |
| 3 | 33 | 99 | 5552 | 35 | 4675 |
+-------+------+------+------+------+------+
The end result I am looking for a table result similar below
+-------+-----------+-------+
| SAUID | OCCUPANCY | VALUE |
+-------+-----------+-------+
| 1 | COM1 | 24 |
| 1 | COM2 | 22 |
| 1 | COM3 | 100 |
| 1 | COM4 | 0 |
| 1 | COM5 | 45 |
| 2 | COM1 | 34 |
| 2 | COM2 | 55 |
| 2 | COM3 | 789 |
| 2 | COM4 | 23 |
| 2 | COM5 | 0 |
| 3 | COM1 | 33 |
| 3 | COM2 | 99 |
| 3 | COM3 | 5552 |
| 3 | COM4 | 35 |
| 3 | COM5 | 4675 |
+-------+-----------+-------+
Im looking around but most of the examples seem to use pivot but having a hard time trying to wrap that around my case as I need the values all in one column.
I hoping to experiment with some hardcoding to get fimilar with my example but my actual table columns are ~100 with varying #s of SAUID per table and looks like it will require dynamic sql?
Thanks for the help in advance.
Use UNPIVOT:
SELECT u.SAUID, u.OCCUPANCY, u.VALUE
FROM yourTable t
UNPIVOT
(
VALUE for OCCUPANCY in (COM1, COM2, COM3, COM4, COM5)
) u;
ORDER BY
u.SAUID, u.OCCUPANCY;
Demo
I'm working with Jaspersoft Studio.
I've a crosstab similar to this one:
+---------+------+------+
| FRUIT | 2014 | 2015 |
+---------+------+------+
| apples | 12 | 85 |
+---------+------+------+
| peaches | 74 | 36 |
+---------+------+------+
| bananas | 54 | 82 |
+---------+------+------+
and I would like to obtain something like this:
+---------+------+------+---------------------+
| FRUIT | 2014 | 2015 | Difference |
| | | | between 2015/2014 |
| | | | (delta) |
+---------+------+------+---------------------+
| apples | 12 | 85 | 73 |
+---------+------+------+---------------------+
| peaches | 74 | 36 | -38 |
+---------+------+------+---------------------+
| bananas | 54 | 82 | 28 |
+---------+------+------+---------------------+
Is it possible?
I mean, it's there a way to intercetpt the value of a field in the single period (e.g. 2014) and use it for a custom calculation?
I am new to Tableau and I have requirements as below:
I need to create a dashboard with a filter on Paywave or EMV and show count of Confirmed and Probable on a geo map.
When I select EMV from the quick filter, it should show a count of confirm & probable for that city. I should be able to drill down and see a count of confirm and probable for zip codes as well.
I am not sure how to achieve the above requirements.
As shown below I have fields like:
EMV Paywave
mrchchant_city, mrch_zipcode confirm probable confirm probable
A 1001 10 15 20 18
B 1005 34 67 78 12
C 2001 24 56 76 45
C 2001 46 19 63 25
Please let me know if any information required from my side.
This will be a lot easier on you if you restructure your data a bit. More often than not, the goal in Tableau is to provide an aggregated summary of the data, rather than showing each individual row. We'll want to group by dimensions (categorical data like "EMV"/"Paywave" or "Confirm"/"Probable"), so this data will be much easier to work with if we get those dimensions into their own columns.
Here's how I personally would go about structuring your table:
+----------------+--------------+---------+----------+-------+-----+
| mrchchant_city | mrch_zipcode | dim1 | dim2 | count | ... |
+----------------+--------------+---------+----------+-------+-----+
| A | 1001 | Paywave | confirm | 20 | ... |
| A | 1001 | Paywave | probable | 18 | ... |
| A | 1001 | EMV | confirm | 10 | ... |
| A | 1001 | EMV | probable | 15 | ... |
| B | 1005 | Paywave | confirm | 78 | ... |
| B | 1005 | Paywave | probable | 12 | ... |
| B | 1005 | EMV | confirm | 34 | ... |
| B | 1005 | EMV | probable | 67 | ... |
| C | 2001 | Paywave | confirm | 76 | ... |
| C | 2001 | Paywave | probable | 45 | ... |
| C | 2001 | EMV | confirm | 24 | ... |
| C | 2001 | EMV | probable | 56 | ... |
| C | 2001 | Paywave | confirm | 63 | ... |
| C | 2001 | Paywave | probable | 25 | ... |
| C | 2001 | EMV | confirm | 46 | ... |
| C | 2001 | EMV | probable | 19 | ... |
| ... | ... | ... | ... | ... | ... |
+----------------+--------------+---------+----------+-------+-----+
(Sorry about the dim1 and dim2, I don't really know what those dimensions represent. You can/should obviously pick a more intuitive nomenclature.)
Once you have a table with columns for your categorical data, it will be simple to filter and group by those dimensions.
I have a table in which there many redundant points, I want to select distinct points using (distinct) and to select the average of some row (eg. rscp).
Here we have an example :
| id | point | rscp | ci
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| 1 | POINT(10.1192 36.8018) | 10 | 701
| 2 | POINT(10.1192 36.8018) | 11 | 701
| 3 | POINT(10.1192 36.8018) | 12 | 701
| 4 | POINT(10.4195 36.0017) | 30 | 701
| 5 | POINT(10.4195 36.0017) | 44 | 701
| 6 | POINT(10.4195 36.0017) | 55 | 701
| 7 | POINT(10.9197 36.3014) | 20 | 701
| 8 | POINT(10.9197 36.3014) | 22 | 701
| 9 | POINT(10.9197 36.3014) | 25 | 701
What i want to get is this table below : (rscp_avg is the average of rscp of the redundant points)
| id | point | rscp_avg | ci
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| * | POINT(10.1192 36.8018) | 11 | *
| * | POINT(10.4195 36.0017) | 43 | *
| * | POINT(10.9197 36.3014) | 22.33 | *
I tried this, but it gave me a false average !!!!
select distinct on(point)
id,st_astext(point),avg(rscp) as rscp_avg,ci
from mesures
group by id,point,ci;
Thanks for your help (^_^)
Hamdoulah ! Thanks God !
I find the solution just now :
select on distinct(point)
id,st_astext(point),rscp_avg,ci
from
(select id,point,avg(rscp) over w as rscp_avg,ci
from mesures
window w as (partition by point order by id desc)
) ss
order by point,id asc;
The websites that help me are :
http://www.postgresql.org/docs/9.1/static/tutorial-window.html
http://www.w3resource.com/PostgreSQL/postgresql-avg-function.php