How to pass dynamic parameter in postgress crosstab function - crosstab

I have a subquery in crosstab function and I need to filter result based in where condition.
for example
......some queries
WHERE customer_id = some dynamic customer id goes here

Related

The sqliite db query is not working in postgresql db

i am having a query which is working correctly in SQLite. but its giving error in PostgreSQL.
SELECT decks.id, decks.name, count(cards.id)
from decks
JOIN cards ON decks.id = cards.did
GROUP BY cards.did
above query is giving error in postgresql.
ERROR: column "decks.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT decks.id, decks.name, count(cards.id) FROM decks JOIN...
You can't have columns in the SELECT list, that are not used in an aggregate function or part of the GROUP BY. The fact that SQLite accepts this, is a bug in SQLite. The fact that Postgres rejects this, is correct.
You need to rewrite your query to:
SELECT decks.id, decks.name, count(cards.id)
from decks
JOIN cards ON decks.id = cards.did
GROUP BY decks.id, decks.name;
If decks.id is the primary key, you can shorten the grouping to GROUP BY decks.id

getting an error as more than one row returned by a subquery used as an expression when trying to insert more than one rows in table

I am trying to insert multiple values into a table from different table in postgresql and encountering an error as [21000]: ERROR: more than one row returned by a subquery used as an expression
INSERT INTO coupon (id,entityid)
values
(select nextval('seq_coupon')),(select entityid from card where country in ('China')));
This query [select entityid from card where country in ('China'))] has multiple rows.
Any help is much appreciated.
If you want to insert rows that come from a SELECT query, don't use the values clause. The SELECT query you use for the second column's value returns more than one row which is not permitted in places where a single value is required.
To include a constant value for all newly inserted rows, just add it to the SELECT list of the source query.
INSERT INTO coupon (id, entityid, coupon_code)
select nextval('seq_coupon'), entityid, 'A-51'
from card
where country in ('China');
As a side note: when using nextval() there is no need to prefix it with a SELECT, even in the values clause, e.g.
insert into coupon (id, entityid)
values (nextval('some_seq'), ...);

Is distinct function deterministic? T-sql

I have table like below. For distinct combination of user ID and Product ID SQL will select product bought from store ID 1 or 2? Is it determinictic?
My code
SELECT (DISTINCT CONCAT(UserID, ProductID)), Date, StoreID FROM X
This isn't valid syntax. You can have
select [column_list] from X
or you can have
select distinct [column_list] from X
The difference is that the first will return one row for every row in the table while the second will return one row for every unique combination of the column values in your column list.
Adding "distinct" to a statement will reliably produce the same results every time unless the underlying data changes, so in this sense, "distinct" is deterministic. However, it is not a function so the term "deterministic" doesn't really apply.
You may actually want a "group by" clause like the following (in which case you have to actually specify how you want the engine to pick values for columns not in your group):
select
concat(UserId, ProductID)
, min(Date)
, max(Store)
from
x
group by
concat(UserId, ProductID)
Results:
results

ssrs date filter expression for startdate/enddate declared variables

I should create ssrs report and I have following scenario:
I have sql query with median, q25, q75 functions.
The query is grouped by geography. Date, which is not grouped, should be filtered in ssrs in order to calculate aggregations for certain time period and they must be grouped only by geography dimension. Bouth variables startdate and enddate are declared and implemented in to where clause and assigned to date column.
what I need in ssrs is expression for parameters startdate and enddate in the dataset filter. Since I do not select Date in the query, i do not have any generated expression in the ssrs filter.
Can I link report parameters to the query variables or does somebody know any other workaround for this?
I would really appreciate any help!
Your query should look something like this:
SELECT Geography, SUM(Amount) AS Amount
FROM MyTable
WHERE DateField >= #StartDate AND DateField <= #EndDate
GROUP BY Geography
On your dataset's property page, click the Parameters tab and make sure that the date parameters are mapped to the variables you use in your query.

Grouping in SSRS 2008 - Filtering

I'm new to SSRS. I want to group a transactions table by customerid and count how many transactions per customerid. I was able to do that.
But then I want to sort by that count, and/or filter by that count. How do you that?
Thanks!
To set up sorting and filtering on row groups, right click on the row group.
You can access the Group Sorting and Filtering properties here. They should both allow you to set up rules based on the name of your count column.
Option 1
If you have no need to show the transactions in the report then the aggregation should be performed at the database level in the query, not by SSRS. You'll get the benefits of:
Faster rendering.
You'll be sending less data over the network.
There'll be less data for the SSRS engine to process, therefore any ordering can be performed quicker.
Your data set can be 'pre-ordered' by putting the most common/expected values in the ORDER BY clause of the underlying query.
Thereby giving any rendering a speed boost, also.
Any filters can be applied directly against the aggregated data returned by the query without having to try and do complex expressions in SSRS.
This will also give a performance boost when rendering.
You could have a "filter" parameter that could be used in the HAVING clause of an aggregate query
Again, a performance boost due to less data across the network, and to be processed.
Gives a level of interactivity to your reports as opposed to trying to pre-define user tastes and having filter conditions set on expressions or a 'best-guess'.
Example
-- Will filter out any customers who have 2 or fewer transactions
DECLARE #Filter AS int = 2
;
SELECT
CustomerId
,COUNT(TransactionId)
FROM
Transactions
GROUP BY
CustomerId
HAVING
COUNT(TransactionId) > #Filter
Option 2
If you still need to show the transactions, then add an additional column to your query that performs the Count() using the OVER clause and PARTITION BY customerid, like so:
COUNT(transactions) OVER (PARTITION BY customerid) AS CustomerTransactionCount
Assuming a very simple table structure you'll end up with a query structure like so:
SELECT
CustomerId
,TransactionId
,TransactionAttribute_1
,TransactionAttribute_2
,TransactionAttribute_3
.
.
.
,TransactionAttribute_n
,COUNT(TransactionId) OVER (PARTITION BY CustomerId) AS CustomerTransactionCount
FROM
Transactions
You'll be able to use CustomerTransactionCount as a filter and sorting column in any row/column groups within SSRS.
Drawback of this approach
Window functions, i.e. using the OVER (PARTITION BY...) cannot be used in HAVING clauses as no GROUP BY clause is used. This means any filtering will have to be carried out by SSRS.
Workaround options
We take the query above and wrap a CTE around it. This will allow us to filter based on the aggregate results.
Put the aggregate in a derived table.
CTE Example
--Filter variable
DECLARE #Filter AS int = 2
;
WITH DataSet_CTE AS
(
-- Build the data set with transaction information and the aggregate column
SELECT
CustomerId
,TransactionId
,TransactionAttribute_1
,TransactionAttribute_2
,TransactionAttribute_3
.
.
.
,TransactionAttribute_n
,COUNT(TransactionId) OVER (PARTITION BY CustomerId) AS CustomerTransationCount
FROM
Transactions
)
-- Filter and return data
SELECT *
FROM DataSet_CTE
WHERE CustomerTransationCount > #Filter
Derived Table Example
--Filter variable
DECLARE #Filter AS int = 2
;
SELECT
*
FROM
(
-- Build the data set with transaction information and the aggregate column
SELECT
CustomerId
,TransactionId
,TransactionAttribute_1
,TransactionAttribute_2
,TransactionAttribute_3
.
.
.
,TransactionAttribute_n
,COUNT(TransactionId) OVER (PARTITION BY CustomerId) AS CustomerTransationCount
FROM
Transactions
) AS DataSet
WHERE
DataSet.CustomerTransationCount > #Filter