I am trying to run two queries one that selects the first 22 rows and one that selects the remaining rows of the query. I am able to select the top 22 rows. But now I need to select the next 22 rows. Basically I have 2 reports in access, one that displays the first 22 rows, and the next would display the next 22 rows.Any help would be greatly appreciated. Does anyone know a function that could send me in the right direction?
Here is my query so far that select the top 22 rows:
SELECT TOP 22[UB-04_line_items].client_id, [UB-04_line_items].revenue_code, Revenue_Codes.rev_code_desc, [UB-04_line_items].total_chgs, [UB-04_line_items].cpt_code, [UB-04_line_items].service_units, [UB-04_line_items].service_date, [UB-04_line_items].total_chgs, Sum(IIf(IsNull([reason_code])=False,[disputed_amount],0)) AS [AMT DISPUTED], GetList("Select Distinct reason_code From [Itemized_statements] As T1 Where t1.reason_code <> NULL AND t1.client_id = " & [Itemized_statements].client_id & " AND T1.revenue_code = " & [Itemized_statements].revenue_code & "","",", ") AS [Err Code]
FROM [UB-04_line_items] INNER JOIN (Itemized_Statements LEFT JOIN Revenue_Codes ON Itemized_Statements.revenue_code = Revenue_Codes.revenue_code) ON [UB-04_line_items].client_id = Itemized_Statements.client_id
GROUP BY [UB-04_line_items].client_id, [UB-04_line_items].revenue_code, Revenue_Codes.rev_code_desc, [UB-04_line_items].cpt_code, [UB-04_line_items].service_units, [UB-04_line_items].service_date, [UB-04_line_items].total_chgs, [UB-04_line_items].total_chgs, Itemized_Statements.client_id, Itemized_Statements.revenue_code
HAVING ((([UB-04_line_items].client_id)=[Itemized_Statements].[client_id]) AND ((Itemized_Statements.client_id)=[forms]![frmClients]![client_ID]) AND ((Itemized_Statements.revenue_code)=[UB-04_line_items].[revenue_code]))
ORDER BY Itemized_Statements.client_id;
Consider using a calculated row number by running count of client_id and revenue_code (assuming this pair is unique in the aggregate query). Then, use this subquery in WHERE clause to condition the needed range. Be sure to remove the TOP clause from stored query.
Top 22 (substitute Qry for actual stored query name)
SELECT (SELECT Count(*)
FROM Qry sub
WHERE sub.client_id < Qry.client_id
OR (sub.client_id = Qry.client_id
AND sub.revenue_code <= Qry.revenuce_code)) As RowNo, *
FROM Qry
WHERE (SELECT Count(*)
FROM Qry sub
WHERE sub.client_id < Qry.client_id
OR (sub.client_id = Qry.client_id
AND sub.revenue_code <= Qry.revenuce_code)) <= 22
Next 22 (substitute Qry for actual query name)
SELECT (SELECT Count(*)
FROM Qry sub
WHERE sub.client_id < Qry.client_id
OR (sub.client_id = Qry.client_id
AND sub.revenue_code <= Qry.revenuce_code)) As RowNo, *
FROM Qry
WHERE (SELECT Count(*)
FROM Qry sub
WHERE sub.client_id < Qry.client_id
OR (sub.client_id = Qry.client_id
AND sub.revenue_code <= Qry.revenuce_code)) BETWEEN 23 AND 44
Related
I have created a joined table in SQL Server MS and there are several duplicate lines in it. Now, I need to make a wise selection out of this table, so that there would be unique line for each (Item, Recall_Date) pair based on a specific selection criteria:
Here is the visual clarification of what I need as pick criteria:
Basically, my selection criteria should be as below:
If there are lines as PingPong_FE = 1 & PingPong_Replen = 1, Then pick
this,
Else if there are lines as PingPong_FE = 0 & PingPong_Replen = 1,Then
pick this,
Else if there are lines as PingPong_FE = 1 & PingPong_Replen = 0,Then
pick this,
Else if there are lines as PingPong_FE = 0 & PingPong_Replen = 0,Then
pick this
Into the output table.
How should be my SQL query look like?
You can merge PingPong_Replen and PingPong_FE columns and get the row which has the max value.
Try this;
select * from
(
SELECT t.item, t.recall_date, t.fe_date , max(t.PingPong_Replen + t.PingPong_FE) AS maxPPval
FROM tableInput t
GROUP BY t.item, t.recall_date, fe_date) t1,
tableInput t2
where t2.item = t1.item
and t2.recall_date = t1.recall_date
and t1.fe_date = t2.fe_date
and t1.maxPPval = (t2.t.PingPong_Replen + t2.PingPong_FE)
May Be like this:
WITH CTE AS (
SELECT Item, Recall_Date, PingPongFE, PinPongReplen , Row_number() over
(PARTITION BY Item, Recall_Date ORDER BY Item, Recall_Date) ROW
FROM Yourtable)
SELECT * FROM CTE WHERE ROW=1;
I am joining two tables: breeds + breed_characteristics (bc)
But I'm getting the following error:
PG::UndefinedColumn: ERROR: column "val" does not exist LINE 11
I'm not sure what's wrong, here is my SQL:
SELECT
breeds.*,
CASE bc.user_val
WHEN NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END AS val
FROM
breed_characteristics bc
INNER JOIN breeds ON breeds.id = bc.breed_id
WHERE bc.characteristic_id = 45
AND val BETWEEN 4 AND 5
ORDER BY val DESC
(Executing this query on Postgres through Active Record)
You can't use expression alias val in where clause like that.
It's because there is an order in which SQL is executed specified in the SQL standard. Here, the WHERE clause is evaluated before SELECT and hence, the WHERE clause is not aware of the alias you created in the SELECT. The ORDER BY comes after the SELECT and hence can utilize aliases.
Just replace the alias with the actual case expression like this:
SELECT
breeds.*,
CASE bc.user_val
WHEN NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END AS val
FROM
breed_characteristics bc
INNER JOIN breeds ON breeds.id = bc.breed_id
WHERE bc.characteristic_id = 45
AND CASE WHEN bc.user_val is NULL THEN bc.value
ELSE (bc.value + (bc.user_val/2))/2
END BETWEEN 4 AND 5
ORDER BY val DESC
However, you can use alias in order by clause.
One option to avoid restating the CASE expression in multiple places is to use a subquery:
SELECT *
FROM
(
SELECT b.*,
bc.characteristic_id,
CASE WHEN bc.user_val IS NULL THEN bc.value
ELSE (bc.value + (bc.user_val / 2)) / 2
END AS val
FROM breed_characteristics bc
INNER JOIN breeds b
ON breeds.id = bc.breed_id
) t
WHERE t.characteristic_id = 45 AND
t.val BETWEEN 4 AND 5
ORDER BY t.val DESC
I've been working on a system service for a client for a while and need some help, I need to merge two sql queries into one. The first part of the query is to look at the master sequence number and count it, after which the query must update a field. I have the two queries below if anyone can help with this problem.
Count query
SELECT master_seq, count(master_seq) as NofH
FROM [ZS_CS_EVO_Integration].[dbo].[CS_Consolidation]
where delivery_date = '2016-07-01'
GROUP BY master_seq
order by master_seq
Update Query
(" UPDATE [dbo].[CS_Consolidation]"
+ " SET [split_dlv] = 1"
+ " FROM [dbo].[CS_Consolidation]"
+ " WHERE"
+ " [master_seq] <> 0 AND CONVERT(DATE,delivery_date) = '" + yesterday + "'", IntConnect);
You can put the first part into CTE, then join and UPDATE:
DECLARE #delivery_date DATE = '2016-07-01'
;WITH cte AS (
SELECT master_seq
FROM [ZS_CS_EVO_Integration].[dbo].[CS_Consolidation]
where delivery_date = #delivery_date and [master_seq] <> 0
GROUP BY master_seq
HAVING count(master_seq) > 1
)
UPDATE c
SET [split_dlv] = 1
FROM [dbo].[CS_Consolidation] c
INNER JOIN cte t
ON t.master_seq = c.master_seq and c.delivery_date = #delivery_date
I'm struggling here trying to write a script that finds where an order was returned multiple times by the same associate (count greater than 1). I'm guessing my syntax with the subquery is incorrect. When I run the script, I get a message back that the "SELECT failed.. [3669] More than one value was returned by the subquery."
I'm not tied to the subquery, and have tried using just the group by and having statements, but I get an error regarding a non-aggregate value. What's the best way to proceed here and how do I fix this?
Thank you in advance - code below:
SEL s.saletran
, s.saletran_dt SALE_DATE
, r.saletran_id RET_TRAN
, r.saletran_dt RET_DATE
, ra.user_id RET_ASSOC
FROM salestrans s
JOIN salestrans_refund r
ON r.orig_saletran_id = s.saletran_id
AND r.orig_saletran_dt = s.saletran_dt
AND r.orig_loc_id = s.loc_id
AND r.saletran_dt between s.saletran_dt and s.saletran_dt + 30
JOIN saletran rt
ON rt.saletran_id = r.saletran_id
AND rt.saletran_dt = r.saletran_dt
AND rt.loc_id = r.loc_id
JOIN assoc ra --Return Associate
ON ra.assoc_prty_id = rt.sls_assoc_prty_id
WHERE
(SELECT count(*)
FROM saletran_refund
GROUP BY ORIG_SLTRN_ID
) > 1
AND s.saletran_dt between '2015-01-01' and current_date - 1
Based on what you've got so far, I think you want to use this instead:
where r.ORIG_SLTRN_ID in
(select
ORIG_SLTRN_ID
from
saletran_refund
group by ORIG_SLTRN_ID
having count (*) > 1)
That will give you the ORIG_SLTRN_IDs that have more than one row.
you don't give enough for a full answer but this is a start
group by s.saletran
, s.saletran_dt SALE_DATE
, r.saletran_id RET_TRAN
, r.saletran_dt RET_DATE
, ra.user_id RET_ASSOC
having count(distinct(ORIG_SLTRN_ID)) > 0
this does return more the an one row
run it
SELECT count(*)
FROM saletran_refund
GROUP BY ORIG_SLTRN_ID
Probably it has been asked before but I cannot find an answer.
Table Data has two columns:
Source Dest
1 2
1 2
2 1
3 1
I trying to come up with a MS Access 2003 SQL query that will return:
1 2
3 1
But all to no avail. Please help!
UPDATE: exactly, I'm trying to exclude 2,1 because 1,2 already included. I need only unique combinations where sequence doesn't matter.
For Ms Access you can try
SELECT DISTINCT
*
FROM Table1 tM
WHERE NOT EXISTS(SELECT 1 FROM Table1 t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source)
EDIT:
Example with table Data, which is the same...
SELECT DISTINCT
*
FROM Data tM
WHERE NOT EXISTS(SELECT 1 FROM Data t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source)
or (Nice and Access Formatted...)
SELECT DISTINCT *
FROM Data AS tM
WHERE (((Exists (SELECT 1 FROM Data t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source))=False));
your question is asked incorrectly. "unique combinations" are all of your records. but i think you mean one line per each Source. so it is:
SELECT *
FROM tab t1
WHERE t1.Dest IN
(
SELECT TOP 1 DISTINCT t2.Dest
FROM tab t2
WHERE t1.Source = t2.Source
)
SELECT t1.* FROM
(SELECT
LEAST(Source, Dest) AS min_val,
GREATEST(Source, Dest) AS max_val
FROM table_name) AS t1
GROUP BY t1.min_val, t1.max_val
Will return
1, 2
1, 3
in MySQL.
To eliminate duplicates, "select distinct" is easier than "group by":
select distinct source,dest from data;
EDIT: I see now that you're trying to get unique combinations (don't include both 1,2 and 2,1). You can do that like:
select distinct source,dest from data
minus
select dest,source from data where source < dest
The "minus" flips the order around and eliminates cases where you already have a match; the "where source < dest" keeps you from removing both (1,2) and (2,1)
Use this query :
SELECT distinct * from tabval ;