Hi all am trying to build a sql query to display some information on a dashboard.
I have a table in which i store my sales data.My query needs to find total sales in last 7 days,average and group it based on region(which is present in user table.) .I created a temp table to get the item sales.DashBoard_Items table contains particular item that needs to be shown on dashboard.Now my problem is i need to get the store count of each region to find average sales.Can some one please help
Declare #TableTest
table(itemid int,itemname varchar(100),itemdescription varchar(100),id int,itemidd int,userid int,orderdate varchar(40),qty int)
insert into #TableTest
select * from DashBoard_Items join
SalesQTY on SalesQTY.OrderDate>= CONVERT(varchar(10) , DATEADD(DAY,-7,GETDATE()),126)
and OOS_DashBoard_CoreItems.itemid=SalesQTY.itemid
select distinct t.userid,u.region from #TableTest t join users u on t.userid=u.userid and region is not null
above select query returns
how can i get region count from the above select query
region count
5 - SUN WEST 2
2 - LONG ISLAND 3
You need to group by region and then use cont(*)
SELECT region, count(*)
FROM #TableTest
GROUP BY region;
Please try:
SELECT
Region,
COUNT(*) AS [Count]
FROM YourTable
GROUP BY Region
OR
SELECT
DISTINCT Region,
COUNT(*) OVER (PARTITION BY Region) AS [Count]
FROM YourTable
SELECT u.region, COUNT(*)
FROM #TableTest t JOIN users u ON t.userid=u.userid AND u.region IS NOT NULL
GROUP BY u.region
try
SELECT region, count( * )
FROM mytable
GROUP BY region
Related
The result is one line, but there are several people with the same value of the bonus field
SELECT Name, bonus
From employees
ORDER by bonus
Limit 1
result
Ivan 100
but it is required that it was
Ivan 100
Petr 100
did this, but it seems very confusing to me:
SELECT Name, bonus
From employees
Where bonus= (SELECT id From employees ORDER by bonus Limit 1)
In SQLite you can achieve that by using RANK or dense_rank window function
select name, bonus
from (select name, bonus, dense_rank() over(order by bonus desc) dns_rnk from employees)
where dns_rnk = 1;
In sub-query it will rank employees on bonus and the outer query will filter out unneeded lines.
I've got 4 tables that I want data from.
Trip_T
tripID (PK)
userID (FK)
...
User_T
userID (PK)
username
...
Excursion_T
excursionID (PK)
tripID (FK)
...
POI_T
poiID (PK)
excursionID (FK)
...
I want to create a table with one row for each trip in the db.
Each row should include the tripID, title, the user's name associated with the trip, the number of excursions made on the the trip and the number of poi (points of interest) associated with those excursions.
I'm using the following query:
SELECT Trip_T.tripID, Trip_T.title, User_T.username
COUNT(DISTINCT Excursion_T.excursionID) AS numExcursions,
COUNT(DISTINCT POI_T.poiID) AS numPOI
FROM Trip_T
INNER JOIN User_T ON User_T.userID = Trip_T.userID
INNER JOIN Excursion_T ON Excursion_T.tripID = Trip_T.tripID
INNER JOIN POI_T ON POI_T.excursionID = Excursion_T.excursionID
Even though I have multiple trips in the db, each with multiple excursions and pois, the query returns 1 row with what looks like the total number of excursions and total number of pois for all trips.
Any help is appreciated.
You forgot to add grouping to your query:
GROUP BY Trip_T.tripID, Trip_T.title, User_T.username
This way the counters correspond to each triplet of (Trip_T.tripID, Trip_T.title, User_T.username)
I'm curious if there is a way within Oracle SQL Developer to expand the rows contained within a Group By Query whether in a report or just in the data grid. For example, let's say I have a single Payment Transaction with a payment amount. I have grouped these fields and I have the SUM of the items which is the total payment amount. It would be very nice if I there was a feature that would allow me to view the items within the transaction that make up the total payment amount without having to remove the Group By fields and the Aggregate SUM function.
Using the data below as an example to show what I am looking for. Only two tables in the below sample data. pymt_transactions and transaction_items.
WITH pymt_transactions AS
(
SELECT 1 AS tran_id FROM dual UNION
SELECT 2 AS tran_id FROM dual UNION
SELECT 3 AS tran_id FROM dual UNION
SELECT 4 AS tran_id FROM dual UNION
SELECT 5 AS tran_id FROM dual
) /* END pymt_transactions CTE */
--SELECT * FROM pymt_transactions;
, transaction_items AS
(
SELECT 1 AS tran_id, 'T-Shirt' AS retail_item, 15 AS amt FROM dual UNION
SELECT 1 AS tran_id, 'Shoes' AS retail_item, 50 AS amt FROM dual UNION
SELECT 1 AS tran_id, 'Pants' AS retail_item, 40 AS amt FROM dual UNION
SELECT 1 AS tran_id, 'Comb' AS retail_item, 3 AS amt FROM dual UNION
SELECT 2 AS tran_id, 'Sweater' AS retail_item, 15 AS amt FROM dual UNION
SELECT 2 AS tran_id, 'Belt' AS retail_item, 12 AS amt FROM dual UNION
SELECT 2 AS tran_id, 'Pants' AS retail_item, 40 AS amt FROM dual UNION
SELECT 2 AS tran_id, 'Watch' AS retail_item, 23 AS amt FROM dual
) /* END transaction_items CTE */
SELECT pt.tran_id, ti.retail_item, ti.amt
FROM pymt_transactions pt
LEFT JOIN transaction_items ti ON ti.tran_id = pt.tran_id
ORDER BY pt.tran_id
;
The above produces the below result set.
Now for the Grouped By version. Using the same two CTE's in the above example:
SELECT pt.tran_id, COUNT(ti.retail_item) AS num_retail_items,
NVL(SUM(ti.amt),0) AS payment_amount
FROM pymt_transactions pt
LEFT JOIN transaction_items ti ON ti.tran_id = pt.tran_id
GROUP BY pt.tran_id
ORDER BY pt.tran_id
;
The above Grouped By Query produces the below results and groups by tran_id:
Again, without my having to write two different queries is there a way I can simply have a Group By query and provide the ability to just click to expand in order to view the retail_items? I've used reports and other systems that have this feature. In both transaction 1 and transaction 2 there are four Retail Items. I want to view these items without having to remove the Group By Clause and the Aggregate SUM Function that is producing the total in the payment_amount column.
Try building a report.
View > Reports.
User Defined Reports.
Add a new report.
Provide the main SQL, then go and add a child report, and tie the data together with a :BIND.
Then click on a row up top, and get the detail data you want below.
You can start to have fun with Charts if you'd like too.
Having access to all the values at once, maybe this can be a solution?
SELECT pt.tran_id
, ti.retail_item
, ti.amt
,COUNT(ti.retail_item) over (Partition by pt.tran_id)
,SUM(ti.amt) over (Partition by pt.tran_id)
FROM pymt_transactions pt
LEFT JOIN transaction_items ti ON ti.tran_id = pt.tran_id
ORDER BY pt.tran_id
;
Ok lets try to build a drill down report.
First build a user defind report calcualting the sum adding a filter (I beleive capital letters are important here)
Then add another report with your initial sql.
And add a reference to the sumcnt report
Now running the top report and right click it you will get this:
good luck
How do I select by column value count? In SQL query it would be something like this: select * from band inner join bandsinger on band.id = bandsinger.bandid inner join singer on singer.id = bandsinger.singerid group by band.id having count(singerid=6)>0 and count(singerid=4)>0 if SQLite function count() could accept a function as a parameter, but it doesn't.
The point is to select two bands, where two singers with known IDs sing.
I found the solution. In this case a query should be: select * from band inner join bandsinger on band.id = bandsinger.bandid inner join singer on singer.id = bandsinger.singerid where dinger.id = 6 or singer.id=4 group by band.id having count(*)=x where x is number of given IDs to count.
I am trying to use an aggregate function, COUNT, in a join. However, there seems to be a problem with what I've written, and I keep getting error messages. Here is what I have so far:
select a.firstname, a.lastname, iddonor, count(idpledge)
from dd_donor a inner join dd_pledge b
using(iddonor)
group by (iddonor);
I want to count the number of pledges made by each donor, and I want to group it by the donor's ID. How can I do this?
You need to add the other non-summary fields to the GROUP BY:
select a.firstname, a.lastname, iddonor, count(idpledge)
from dd_donor a inner join dd_pledge b
using(iddonor)
group by (FIRSTNAME, LASTNAME, iddonor)
SQLFiddle here
Share and enjoy.